This is a biolerplate project, it acts as a starting point for your backend and is based on python fast-api. It comes with user authentication with JWT tokens, logging and test infrastructure so you can get started right away making your application tables and and routes. Protect them using dependancy injection as shown in the get-user route.
You will need to setup a local postgres database instance for the application you can do this easily using the the included docker-compose but you will need to make sure you have docker desktop installed first.
- https://docker.com
Before you run pytest, make sure you have a database already created in your docker postgresdb called test-db you can change the nasme of this in the config.py or even setup the docker-compose to spin that up for you automatically but you only need to do it once so I normally just use phadmin to do it manually.
- After cloneing the project, create a virtual environment with:
python -m venv .venv
- next install all the dependancies with
python -m pip install -r requirements.txtpython -m pip install -r requirements-dev.txt
- now setup setup your
.envin the root of the project:ENV_STATE=global DB_NAME=api-db DB_HOST=localhost DB_PORT=5432 DB_SSL=prefer DB_USER=postgres DB_PASSWORD=pa55word FRONTEND_URL=http://127.0.0.1:8080 JWT_SECRET=1234abcd - make sure docker desktop is running then start the database via docker-compose:
docker-compose up -d- Note: this will also start up a docker version of the application, you can use this to test against as well.
- start the local dev environment with (note that the port is 8001 because docker will run on 8000)
uvicorn api.main:app --reload --port 8001
Create database tables in the database.py using SQL Alchemy. These tables will be created automatically when the application starts.
Models are used when handling data that is going to and from the users of the API. Create new models in the /api/models folder.
There is a startup script already setup in this project called startup.sh you can use this as the startup command for an azure app service host environment. the .env example above also makes it easy for you to setup your environment variables the same way in the app service settings. Also make a FRONTEND_URL setting so your front end can get through CORS.
- have this project running locally in both local dev and azure, make sure all your tests pass then push this into your github repo.
- Next, create an azure app service with webapp + database and link to this project via github - Azure will create the necessary pipeline file for github actions to use.
- setup the app service config with:
- environment variables (they should be the same as your .env but use the values of the database created with the app service app)
- startup.sh script start up command in the settings
- test the api works using the register user route