Backend for the Gestion du Temps de Travail (GTT) Tool.
To install this project:
Clone the project into your local workspace:
git clone https://github.com/cbn-alpin/gtt-api.gitNavigate to the cloned project folder:
cd gtt-api/Install Python dependencies defined in pyproject.toml:
Install uv. See: https://docs.astral.sh/uv/getting-started/installation/
Synchronise the app (install the .venv and dependencies):
# Install applications dependencies
uv sync
# Install development dependencies
uv sync --extra devNote: we don't use requirements.txt file in this project.
Then, run command inside the .venv with:
uv run <command>Create a .env configuration file and adapt it to your configuration:
cp .env.sample .envIf you change the location of the .env file, you must specify the path to the new location using the GTT_CONFIG_PATH environment variable.
We use Postgresql and you need to add a new user and crate a new database. Connect to Psql terminal with a superadmin user:
sudo -u postgres psqlIn Psql terminal, create a new user (<user-name>) with password (<user-password>) and a new database (<new-database-name>):
CREATE USER <user-name> WITH ENCRYPTED PASSWORD '<user-password>';
CREATE DATABASE <new-database-name> WITH TEMPLATE template0 OWNER <user-name>;
GRANT ALL PRIVILEGES ON DATABASE <new-database-name> TO <user-name> ;The database content is installed by default when the Docker container is launch in production if it doesn't exist. The same applies to the migrations.
In development, use the command: flask db upgrade
Run Flask development server with :
uv run flask runYou will see:
INFO in __init__: No previous migrations found. Running all migrations...
INFO in __init__: Database is up to date
If a manual update is required, use the following command:
uv run alembic upgrade head
# If you had change the .env location used:
# GTT_CONFIG_PATH=/new/location/.env alembic upgrade headNote: we used pyproject.toml instead of alembic.ini. See Using pyproject.toml for configuration. So, the project has no alembic.ini file.
Alembic was originaly initialize with this command :
alembic init --template pyproject migrationsWith the database created, you must add at least one admin user:
-- Switch to the new database:
\c <new-database-name>
INSERT INTO "user" (last_name, first_name, email, is_admin, "password")
SELECT
'<LASTNAME>',
'<Firstname>',
'<me@example.com>',
TRUE,
md5('<password>') ;Then, you can link the users with the default global project (id=0) with :
INSERT INTO user_action (id_user, id_action)
SELECT
u.id_user,
a.id_action
FROM public."user" AS u
CROSS JOIN public."action" AS a
WHERE a.id_project = 0
AND NOT EXISTS (
SELECT 1
FROM public."user_action" ua
WHERE ua.id_user = u.id_user
AND ua.id_action = a.id_action
);Launch the Flask framework in development mode:
uv run flask runNote: For production, we will use Gunicorn and Nginx within a Docker container.
ur runn pytestTo generate a new Alembic revision with the message <my-revision-message> :
# Create new Alembic revision:
uv run alembic revision --autogenerate -m "<my-revision-message>"This project is configured to be used with Docker and Docker Compose.
You can build the image :
docker build -t gtt-api:develop --target development .Then, from the root directory of the project, you can run the application with:
docker run --rm -it -e GTT_DATABASE_IP="host.docker.internal" -e GTT_APP_PORT=5000 -e FLASK_APP=gtt.main:api --volume .:/home/app/web/ gtt-api:developYou can override the config file .env paremeters used by the Flask application with env variables using the same name prefixed by GTT_. Ex.: GTT_DATABASE_IP.
Also, you can use:
GTT_API_HOST_PORT: to change the port of the API on the host machine.GTT_CONFIG_PATH: to change the path of the.envfile.GTT_APP_PORT: to change the default port5001of the API.
To easily launch the application in a development environment, use Docker Compose. This will build the development image and run it with your local source code mounted for hot-reloading.
- Make sure you have a
.envfile (you can copy.env.sample). - Run the following command:
# Build and start the container in the background
docker-compose up --build -dThe API will be available on your local machine at the port specified by GTT_API_HOST_PORT in your docker-compose.yml (default: http://localhost:5001). Use the .env file to change the port.
To view the logs: docker-compose logs -f
To stop the container: docker-compose down
To locally build the production-ready image:
docker build -t gtt-api:production --target production .To locally run the production image in host network:
docker run --rm -it -e GTT_APP_PORT=5000 -e FLASK_APP=gtt.main:api -e FLASK_ENV=production --volume .env:/home/app/web/.env --network host gtt-api:production