REST API written in the FastAPI framework for message persistence with authentication built-in.
- Docker
- docker-compose
This app is built using FastAPI framework https://fastapi.tiangolo.com.
It consists of the Starlette ASGI framework and Pydantic library.
Because this app requires a PostgreSQL database instance running alongside, the easiest way to run it is to use docker-compose.
docker-compose up
This docker-compose config uses volumes for PostgreSQL container, so it should retain data between restarts.
The following script will run tests in an isolated docker environment, so you don't have to set up a database yourself.
./scripts/test-docker.sh
HTTP method | Authentication required | Path | Description | Possible HTTP codes |
---|---|---|---|---|
GET | NO | /api/v1/messages/{id} |
Get message by id | 200, 400, 401, 404 |
POST | YES | /api/v1/messages |
Create new message | 200, 400, 401 |
PUT | YES | /api/v1/messages/{id} |
Update existing message | 200, 400, 401, 404 |
DELETE | YES | /api/v1/messages/{id} |
Delete message | 200, 400, 401, 404 |
https://evox.rasztabiga.me/docs
It also allows users to test the API using a simple interface.
https://evox.rasztabiga.me/redoc
Both documentations include examples of request and response bodies.
This REST API uses JSON data type for request and response bodies.
Example of request body:
{
"content": "New message"
}
Example of response body:
{
"content": "New content",
"views_count": 5,
"id": 3
}
Persistence is provided by SQLAlchemy ORM library and PostgreSQL database.
For SQL migrations I'm using Alembic. They are located under alembic
directory.
HTTP Header:
Authorization: ca9c1d4509ec6c9b9550fecfab3817a9b87e5e06bcbfe94298b12ce14ca8e428
This key works for evox.rasztabiga.me
server. It's being set by environmental variable passed to docker container running the app.
This API is covered with both unit and integration tests.
Unit tests are testing message service and cover mostly business logic. They are executed by the pytest library.
Integration tests are executed with FastAPI test HTTP client and call API endpoints, verifying their correctness.
This app is deployed on my private Kubernetes cluster. Configuration used for the deployment is located in the k8s
directory.
Currently, it's running 3 replicas of backend pods, so it's easily scalable.
App is listening on the following URL: https://evox.rasztabiga.me/api/v1/messages
SSL certificate and DDoS protection is provided by Cloudflare.
I'm using GitHub Actions job to execute linter and tests on each push to GitHub. It also builds and pushes Docker image of the app to DockerHub.
You can locate it here: https://hub.docker.com/r/navareth/evox
This app uses the following environment variables:
- API_KEY - API key used to authenticate requests
- POSTGRES_SERVER - hostname of PostgreSQL database
- POSTGRES_USER - username for PostgreSQL database
- POSTGRES_PASSWORD - password for PostgreSQL database
- POSTGRES_DB - database name for PostgreSQL database
- BACKEND_CORS_ORIGINS - allowed origins for CORS mechanism
Following examples use the demo server https://evox.rasztabiga.me
API_KEY=ca9c1d4509ec6c9b9550fecfab3817a9b87e5e06bcbfe94298b12ce14ca8e428
You will have to replace MESSAGE_ID
placeholder if you want to run these examples by yourself.
curl --location --request POST 'https://evox.rasztabiga.me/api/v1/messages' \
--header 'Authorization: ca9c1d4509ec6c9b9550fecfab3817a9b87e5e06bcbfe94298b12ce14ca8e428' \
--header 'Content-Type: application/json' \
--data-raw '{
"content": "New message"
}'
curl --location --request GET 'https://evox.rasztabiga.me/api/v1/messages/{MESSAGE_ID}'
curl --location --request PUT 'https://evox.rasztabiga.me/api/v1/messages/{MESSAGE_ID}' \
--header 'Authorization: ca9c1d4509ec6c9b9550fecfab3817a9b87e5e06bcbfe94298b12ce14ca8e428' \
--header 'Content-Type: application/json' \
--data-raw '{
"content": "New content"
}'
curl --location --request DELETE 'https://evox.rasztabiga.me/api/v1/messages/{MESSAGE_ID}' \
--header 'Authorization: ca9c1d4509ec6c9b9550fecfab3817a9b87e5e06bcbfe94298b12ce14ca8e428'