Microservices on FastAPI
- Convert Line Endings Run dos2unix to ensure that the scripts have the correct line endings:
dos2unix -R ./scripts/*.sh
- Add Environment Variable Add the following line to ./app/microservices/media/env.py:
YANDEX_DISK_API_KEY=...
Else media will be stored in filesystem in folder data/media/
./scripts/run.sh
sudo docker-compose up --build -d
First, you need to retrieve the environment variables:
./scripts/get_env.sh
To generate the nginx.conf based on the environment variables:
./scripts/gen_nginx_conf.sh
To set up self-signed SSL certificates:
./scripts/setup_ssl_mock.sh
If Docker is not yet installed on the host, run the script to install it via dcpkg (or install it manually):
./scripts/install_docker.sh
Now you can start the project using Docker Compose:
sudo docker-compose up --build -d
- Ensure that ports 80 and 443 on the host are not being used by other processes
- It sometimes necessary restart ./scripts/setup_ssl_mock.sh
- When writing nginx.conf just copy final result to nginx.conf.template and replace your host (e.g. "localhost") with $SERVER_NAME
- You have db-migrate service and corresponding script in ./scripts/pg_migrate.py. It just runs all alembic migrations under the migrations/ folder. If anything went wrong you can: check logs/rerun script/run migrations manually following [ migr by hand ] topic in docs/save.txt
-
Select a Similar Microservice
Choose an existing microservice as a analogue. You can copy "media" based on Postgres or "news" based on MongoDB. -
Copy the File Structure
Use the following command to copy the file structure from the old service to the new service:python ./scripts/copymicro.py <old_service> <new_service>
Note: This script simply replaces occurrences of
<old_service>
with<new_service>
, so it is best suited for microservices where the object names match the service name (e.g., "AuthModel" == "auth", but "MatchModel" != "matches"). -
Database Migration (if using Postgres)
If you are using Postgres, create a migrations directory for your new service:- Create the directory:
migrations/<new_service>
based on your analogue - Update
script_location
inalembic.ini
to point to the new migrations directory. - Import the necessary models and
declarative_base
inenv.py
.
- Create the directory:
-
Update
docker-compose.yml
Add the new service to yourdocker-compose.yml
file based on your analogue -
Update Nginx Configuration
Add the new service tonginx.conf.template
based on your analogue. After making the changes, run:./scripts/gen_nginx_conf.sh