A starter pack for building microservices with Python, Flask, and SQLAlchemy. This project provides a development-ready structure with JWT authentication, database management, logging, and more.
- Python 3: Leverages the latest features of Python.
- Flask: A lightweight WSGI web application framework.
- SQLAlchemy: SQL toolkit and Object-Relational Mapping (ORM) library.
- JWT Authentication: Secure authentication mechanism.
- Docker: Containerization for consistent development and deployment environments.
- Alembic: Database migrations tool.
- Docker: Ensure Docker is installed on your system. You can download it from the official website.
-
Clone the Repository:
git clone https://github.com/stefanFramework/microservice-starter.git cd microservice-starter -
Configure Environment Variables:
-
Duplicate the
.env.examplefile and rename the copy to.env:cp .env.example .env
-
Open the
.envfile with a text editor and set the environment variables as needed. Below is a description of the key variables:ENVIRONMENT: The environment in which the app is running; typicallydevelopmentorproduction.JWT_SECRET_KEY: A private key that will be used to sign the token used for authentication.SQLALCHEMY_DATABASE_URI: The database connection URL. For example, for a PostgreSQL database:postgresql://user:password@localhost:5432/database_name.
Ensure that the
SQLALCHEMY_DATABASE_URImatches the database configuration specified in thedocker-compose.ymlfile if you're using Docker for the database service.
-
-
Start the Application with Docker:
-
Build and start the Docker containers:
docker-compose up --build
This command will build the Docker images and start the services defined in the
docker-compose.ymlfile. By default, this includes the Flask application and a PostgreSQL database. -
To stop the services:
docker-compose down
This command stops and removes the containers defined in the
docker-compose.ymlfile.
-
microservice-starter/
βββ app/
β βββ api/
β βββ __init__.py
β βββ config.py
β βββ main.py
β βββ models.py
β βββ ...
βββ migrations/
βββ tests/
βββ .env.example
βββ .gitignore
βββ Dockerfile
βββ docker-compose.yml
βββ requirements.txt
βββ README.md
app/: Contains the main application code.migrations/: Directory for Alembic database migrations.tests/: Contains test cases for the application..env.example: Example environment variables file.Dockerfile: Docker configuration for the Flask application.docker-compose.yml: Docker Compose configuration for multi-container applications.requirements.txt: Python dependencies.
To handle database migrations with Alembic:
-
Generate a New Migration:
docker-compose exec microservice_app alembic revision --autogenerate -m "Migration message"
-
Apply Migrations:
docker-compose exec microservice_app alembic upgrade head
To run tests:
docker-compose exec microservice_app pytestContributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
This project is licensed under the AGPL-3.0 License. See the LICENSE file for details.