-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Quickstart #1
base: fork
Are you sure you want to change the base?
Quickstart #1
Conversation
- maker-bundle to easily create Entities - validator and annotations to enforce requirements on Entities - migrations - behat for E2E testing (needs some adjustements to make it work) ``` docker-compose exec php composer require symfony/maker-bundle --dev docker-compose exec php composer require symfony/validator doctrine/annotations docker-compose exec php composer require migrations docker-compose exec php composer require --dev behat/behat docker-compose exec php vendor/bin/behat --init ```
``` docker-compose exec php composer require symfony/translation docker-compose exec php composer require symfony/apache-pack ```
Makefile target to create app ``` provision/api/app: heroku apps:create \ --team $(HEROKU_TEAM) \ --region $(HEROKU_REGION) \ --remote $(DEPLOYMENT_ENV) \ --buildpack https://github.com/negativetwelve/heroku-buildpack-subdir \ --addons heroku-postgresql:hobby-dev \ $(API_APP_NAME) ``` Makefile target to setup environment ``` provision/api/environment: provision/api/app heroku config:set -a $(API_APP_NAME) \ APP_ENV=prod \ APP_SECRET=$(shell openssl rand -base64 32) \ CORS_ALLOW_ORIGIN=$(ADMIN_URL) \ TRUSTED_HOSTS=$(API_HOST) ```
Mostly inspired from [doc](https://api-platform.com/docs/core/jwt/). - use Lexik for JWT authentication - use FOSUserBundle for User entity - FOSUserBundle config depends on SwiftMailer - add doctrine fixtures to provision users ``` docker-compose exec php composer require "lexik/jwt-authentication-bundle" docker-compose exec php composer require friendsofsymfony/user-bundle docker-compose exec php composer require swiftmailer-bundle docker-compose exec php composer require --dev doctrine/doctrine-fixtures-bundle ```
- table is `fos_user` to avoid conflict w/ Postgres `user` - enable users by default - add ACLs on User - fixtures for admin & user
Issue is that JWT keys cannot be store on the filesystem. Proposal is to read them from ENV.
Start on localhost ``` make start fixtures [email protected] make token [email protected] make token ``` Provision & Deploy ``` JWT_DIR=provision/jwt JWT_PASSPHRASE=$(openssl rand -base64 32) make provision # to provision deployment env make deploy ``` Cleaning ``` make clean make clean/all ``` Destroy (requires confirmation) ``` make destroy/api # interactive confirmation FORCE="--force" make destroy/admin # requires --force to confirm ```
Using subtree:
https://brettdewoody.com/deploying-a-heroku-app-from-a-subdirectory/ |
Secured Infra 🔒How to Provision 🔥
Detailed Procedure 🔍The following security measure can be added to your deployment:
Below you can find a draft of Makefile targets that can do 80% of the job + manual instructions for the remaining 20%. Create Security GroupUsed to define the ingress rules for the DB access
Create RDS/Parameter GroupUsed to add custom params to the DB config (require SSL)
Create Database Instance
Recommandation for prod instance specifications:
When created & available, you can test the connection:
Related doc: Provision SSL CertificateUse Let's Encrypt and certbot
Then add you custom domain:
And add the related CNAME record in AWS/Route53 See this post Update Heroku config
Cloudfront distributionCreate a Web distribution
Once created, configure custom error response DNS Record for CloudfrontWait for the Cloudfront distribution to be provisionned. Known issues
Makefile adjustments
|
Security 🔒Require ROLE_USER to access API doc
|
Purpose
Deploy a JWT-secured JSON-LD Rest API + React admin backoffice in
<1h20 minutes 🚀See it live:
Just Do It 🔥
Context
This quick start is based on API Platform, adding the following features:
This is mainly based on Getting Started with API Platform and related documentations. API Platform has a 🕷as a logo and is built on top of Symfony. In their own word
API
Among other things, we have the following features out-of-the-box:
Admin Backoffice
The admin app is particularly interesting. It leverages the JSON-LD exposed by the API to auto-discover resources and expose them using React-admin and Create React App.
How To
Below you will find links to commits from this fork. In each commit & commit message you will find more details about what commands to type, doc to read, changes to implement.
Fast Foward ⏩
If you just want to go straight to the point, clone the fork and run
make
.Clone the fork
Start on localhost
make start && make fixtures
When the containers are up, you should now have access to:
apphome page on http://localhostAnd you should be able to connect with fixture'd credentials:
To get a JWT token
Provision & Deploy
Provision Heroku app and AWS/S3 bucket & Deploy
JWT_DIR=provision/jwt JWT_PASSPHRASE=$(openssl rand -base64 32) make provision make deploy
Test Live
Create a user
If you want to grant
ROLE_ADMIN
role to your user:heroku run ./api/bin/console fos:user:promote $EMAIL ROLE_ADMIN
You should now be able to connect to the API hosted on Heroku or directly to the admin backoffice hosted on AWS/S3:
Detailed Steps 🔍
If you want to understand the different points addressed on top of API Platform distribution, you can reproduce this step-by-step guide.
Prerequisites ✅
The following commands should succeed
For deployment:
Checkout API Platform distribution 🎉
We suppose we will work in a
quickstart
folder.Checkout the latest stable version of API Platform.
You can also download an archive of any release here.
If you plan to use this as a boilerplate for your own project and don't care about upstream updates, you can squash all commits or reset git setup:
You can also consider adding it as a sub-folder of your already-existing project with git-subtree.
Build 🐳
Build it... and go take a coffee ☕️
When the containers are up, you should now have access to:
apphome page on http://localhostAPI entities 🗃
Deployment 🚀
Security 🔒
Deployment automation ♻️ 🚀
Next Steps 🤔
Add your own models 🗃
Leverage the Symfony MakerBundle to quickly create your own models:
See:
Generate a client app 🎨
API Platform also has on-the-shelf tooling to setup an Client app boilerplate
docker-compose exec client generate-api-platform-client
🐛 It fails due to
Error: Cannot find module 'lodash'
See https://github.com/api-platform/client-generator/issues/108
I cannot remember how I made it work on api-platform/api-platform@6f55803 🙈. If you do, then, as explained by the console output, import and inject the generated routes and reducers in the App. See api-platform/api-platform@7978c52
To Do 👷
Models
/users/me
route api-platform/api-platform@6a9056e /Features
update-deps.sh
Deployment
Misc
alias pg='psql -U $POSTGRES_USER $POSTGRES_DB'
in db container