To run this project in a production
, it is necessary to install and configure DockerSwarm
, as well as to create a Node
.
Create a network with the driver overlay
:
docker network create --driver overlay mt-local
docker network create --driver overlay mt-public
CD to the path docker/Dockerfile
and run the following commands to create images:
docker-compose build memory_tracker-fastapi
docker-compose build memory_tracker-nginx
To run in the development mode, please run the following command, otherwise, continue the steps to the Run
section.
docker compose -f docker/docker-compose-dev.yml up
curl -X GET http://localhost:8000/memory-logs/
For more information about the api, see 'endpoints' section.
Create an environment variable with a username (you will use it for the HTTP Basic Auth for Traefik), for example:
export USERNAME=hadi
export PASSWORD=do
Use openssl to generate the "hashed" version of the password and store it in an environment variable:
export HASHED_PASSWORD=$(openssl passwd -apr1 $PASSWORD)
CD to the path memory_tracker/docker
and run the following commands to create and run stack:
docker stack deploy -c docker-compose-[deploy|dev].yml memory-tracker
docker-compose-deploy.yml
This section provides the Traefik labels configuration used for various purposes such as basic authentication, rate limiting, routing, and load balancing.
-
traefik.http.middlewares.api-auth.basicauth.users=${USERNAME?Variable not set}:${HASHED_PASSWORD?Variable not set}
- Specifies basic authentication middleware for API access. Requires username and hashed password.
-
traefik.http.middlewares.api-ratelimit.ratelimit.burst=5
- Sets the burst rate for rate limiting to 5 requests.
-
traefik.http.middlewares.api-ratelimit.ratelimit.period=1m
- Sets the period for rate limiting to 1 minute.
-
traefik.http.routers.memory_tracker-http.rule=Host('logger.hadido.ir')
- Sets the rule for the HTTP router to route requests with the host 'logger.hadido.ir'.
-
traefik.http.routers.memory_tracker-http.middlewares=api-auth,api-ratelimit
- Specifies the middlewares to be applied to the HTTP router, including basic authentication and rate limiting.
-
traefik.http.services.memory_tracker.loadbalancer.server.port=80
- Sets the port for the load balancer server to 80 for the memory tracker service.
http://logger.hadido.ir/memory-logs/?limit=10&skip=3
-
Headers (production)
Authorization
:Basic aGFkaTpkbw==
- {username: hadi, password: do}
-
Parameters
limit
(optional, integer,10
) ... Number of records to return from the end. Must be greater than 0 and less than 100,000.skip
(optional, integer) ... Number of records to skip. Must be greater than 0.
-
Response 200 (application/json)
- Body
[ { "timestamp": 1647033600, "total": 8192, "free": 2048, "used": 6144 }, { "timestamp": 1647033700, "total": 8192, "free": 1024, "used": 7168 } ]
- Body
-
Response 422 (application/json)
- Body
{ "detail": [ { "type": "int_parsing", "loc": [ "query", "limit" ], "msg": "Input should be a valid integer, unable to parse string as an integer", "input": "ten", "url": "https://errors.pydantic.dev/2.6/v/int_parsing" } ] }
- Body
-
Response 404 (application/json)
- Body
{ "detail": "Not Found." }
- Body
-
Response 405 (application/json)
- Body
{ "detail": "Method Not Allowed." }
- Body
-
Response 500 (application/json)
- Body
{ "detail": "Internal server error." }
- Body
memory_tracker/memory_tracker/cron/cronjobs
memory_tracker/memory_tracker/cron/tasks.py
* * * * * bash /srv/memory_tracker/memory_tracker/cron/run_tasks.sh