Ready-to-go micro-service template based on FastAPI
Just set configs, add endpoints and deploy using docker
Deploy FastService in local development environment using docker-compose in just a few steps:
- Modify environment configs in local_config directory as required.
- Start DB and other external services as required.
- Build service using
sudo docker-compose build. - Start service using
sudo docker-compose up. - Add test cases for your endpoints in
app/app/tests/test_api.py. - Start adding API endpoints in
app/app/src/public_api.pyandapp/app/src/private_api.py. - Service automatically refreshes for any changes made in the code in the development environment.
Dockerfile and docker-compose.yml are used as configuration files for local deployment.
Dockerfile_Prod and fastservice_stack.yml are used as configuration files for production deployment.
- A sample CI/CD config file for gitlab
.gitlab-ci.ymlis provided. Replace with configuration of CI/CD provider of your choice. - Add any service prestart script in
app/prestart.shas required. - Create configs and secrets for your service in your deployment environment.
- Push and deploy to your environment.
-
Authentication and Authorization
- Authentication and authorization modules here use Auth0. Make relevent changes for using any other auth service.
- All auth related code are in
app/app/dependency/authentication.py requires_auth()function is used for validating access token for every request coming to all private endpoints.requires_scope()andrequires_permission()functions are used to check authorization for protected endpoints. Their usage in given as sample endpoints inapp/app/src/private_api.py
-
Private API endpoints - Any endpoints defined in
app/app/src/private_api.pyusing@privateAPIdecorator is accessible only with a valid access token in the request header -
Public API endpoints - Any endpoints defined in
app/app/src/public_api.pyusing@publicAPIdecorator is accessible without any authentication -
Context - Context available throughout the request lifecycle along with a request id generated at very start of an incoming request
-
User id - User id is automatically extracted from access token and added to context for private API endpoints
-
Logging - Two log cofiguration files
app\app\dependency\app_logger\log_config_file.jsonandapp\app\dependency\app_logger\log_config_stdout.jsonare provided to configure logging to rotating file and stdout respectively. Select the logging configuration to use usingLOG_TYPEconfig. -
DB Connection - Database connection is provided using encode/databases. Sample connection is given in
app/app/src/public_api.py -
Test Framework - Test are defined using pytest. Sample test cases present in
app/app/tests/test_api.py -
Healthcheck - A heartbeat endpoint is used for checking health of service instance.
-
Access token for M2M calls - To retrieve access token for M2M calls to private endpoints of other services use the
get_access_token()function inapp/app/dependency/access_token.py