Skip to content

Commit aa2a5f6

Browse files
authored
Merge pull request #18 from ArielMAJ/release/0.4.1
Release/0.4.1
2 parents e9059fd + e809bda commit aa2a5f6

3 files changed

Lines changed: 155 additions & 12 deletions

File tree

Makefile

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ run: ## Run the project.
88
poetry run python -m api
99

1010
.PHONY: install
11-
install: ## Install Python requirements.
11+
install: ## Install Python package dependencies.
1212
python -m pip install --upgrade pip setuptools wheel poetry
1313
poetry lock
1414
poetry install --no-root
1515
poetry run pre-commit install
1616

1717
.PHONY: test
18-
test: ## Run tests.
18+
test: ## Run automated tests.
1919
ENVIRONMENT=test poetry run pytest --cov
2020

2121
.PHONY: up-database
@@ -26,28 +26,36 @@ up-database: ## Start database container.
2626
down: ## Stop all containers.
2727
docker compose down
2828

29+
.PHONY: revision
30+
revision: ## Create a new database revision following the repository's models.
31+
poetry run alembic revision --autogenerate -m "$(MESSAGE)"
32+
2933
.PHONY: migrate
3034
migrate: ## Run database migrations.
3135
poetry run alembic upgrade head
3236

33-
.PHONY: revision
34-
revision: ## Create a new database migration.
35-
poetry run alembic revision --autogenerate -m "$(MESSAGE)"
37+
.PHONY: downgrade
38+
downgrade: ## Undo last database migration.
39+
poetry run alembic downgrade -1
3640

3741
.PHONY: docker-rm
38-
docker-rm: ## Remove all containers.
42+
docker-rm: ## Remove all docker containers.
3943
docker rm -f $$(docker ps -a -q)
4044

4145
.PHONY: docker-rmi
42-
docker-rmi: ## Remove all images.
46+
docker-rmi: ## Remove all downloaded docker images.
4347
docker rmi -f $$(docker images -q)
4448

49+
.PHONY: export-requirements
50+
export-requirements: ## Export poetry managed packages to a requirements.txt (needed by Vercel).
51+
poetry export -f requirements.txt --output requirements.txt --without-hashes
52+
4553
.PHONY: pre-commit
4654
pre-commit: ## Run pre-commit checks.
4755
poetry run pre-commit run --config ./.pre-commit-config.yaml
4856

4957
.PHONY: patch
50-
patch: ## Bump project version to next patch (bugfix release/chores).
58+
patch: ## Bump project version to next patch (bugfix release).
5159
poetry version patch
5260

5361
.PHONY: minor

README.md

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,140 @@
1-
# fastapi-backend-template
1+
# FastAPI Backend Template
22

3-
A FastAPI backend template for a head start on new projects.
3+
A FastAPI backend template for a kickstart on new projects.
44

5-
See `Makefile` for examples on how to run the project.
5+
## Features
6+
7+
- **FastAPI**: A modern, fast web framework for building APIs with Python.
8+
- **Swagger UI**: FastAPI automatically generates interactive API documentation.
9+
- **SQLAlchemy**: Database toolkit and ORM for SQL databases.
10+
- **Alembic**: Lightweight database migration tool for SQLAlchemy.
11+
- **Pytest**: Testing framework.
12+
- **Poetry**: Dependency management and packaging.
13+
- **Pre-commit**: Framework for managing and maintaining multi-language pre-commit hooks.
14+
- **Makefile**: Simplified commands for development tasks.
15+
- **Docker Compose**: Containerized development environment for Postgres database.
16+
- **.env**: Environment variables management.
17+
- **GitHub Actions**: CI/CD workflows for automated testing.
18+
19+
## Installation and Setup
20+
21+
### Prerequisites
22+
23+
These can be installed using package managers like `apt`, `brew`, or `choco`. See the respective websites (or Google Search) for installation instructions.
24+
25+
- Python 3.9+ (required; use `pyenv` for managing Python versions);
26+
- pyenv (optional, for managing Python versions);
27+
- Docker Compose (optional, highly recommended, for easily running database locally);
28+
- Poetry (optional, highly recommended, for dependency management);
29+
- Makefile (optional, highly recommended, for simplified commands);
30+
- Git (optional, for version control).
31+
32+
### Steps
33+
34+
See Makefile for a list of available commands and their descriptions. You can also run `make help` or, optionally just `make`, to see the available commands.
35+
36+
These steps assume you have `make` installed. If you don't have it, copy and run the commands in your terminal instead.
37+
38+
1. **Clone the Repository**
39+
40+
```bash
41+
git clone https://github.com/ArielMAJ/FastAPI-backend-template.git
42+
cd FastAPI-backend-template
43+
```
44+
45+
2. **Set Up Environment Variables**
46+
47+
Copy the example environment file and modify it as needed. If you use docker compose for running the database locally, you can keep the default database values. See [this link](https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/#handle-jwt-tokens) for more information on setting up JWT `SECRET_KEY`.
48+
49+
```bash
50+
cp .env.example .env
51+
```
52+
53+
3. **Install Dependencies**
54+
55+
- Using Poetry:
56+
57+
```bash
58+
make install
59+
```
60+
61+
- Using pip:
62+
63+
```bash
64+
pip install -r requirements.txt
65+
```
66+
67+
4. **Database Setup**
68+
69+
Start the Postgres database using Docker Compose:
70+
71+
```bash
72+
make up-database
73+
```
74+
75+
Run migrations to set up the database:
76+
77+
```bash
78+
make migrate
79+
```
80+
81+
5. **Run the Application**
82+
83+
Start the FastAPI server:
84+
85+
```bash
86+
make run
87+
```
88+
89+
- The FastAPI server will be available at `http://localhost:3000`.
90+
- API documentation can be accessed at `http://localhost:3000/docs`.
91+
92+
## Testing
93+
94+
Running automated tests:
95+
96+
```bash
97+
make test
98+
```
99+
100+
## Writing your own code
101+
102+
You can use this repository as a template for your own projects. You can start by modifying the existing code or adding new files as needed, using the existing ones as example or reference of how to structure your code. Make sure to try to follow the same folder/file structure and conventions to keep the codebase clean and organized.
103+
104+
---
105+
106+
Pre-commit hooks are set up to run automatically before each commit. They will check for code formatting, linting, and other issues. You can also run them manually with:
107+
108+
```bash
109+
make pre-commit
110+
```
111+
112+
---
113+
114+
For changes to the database, you can follow these steps:
115+
116+
1. Make changes to the database models in `api/database/models`.
117+
2. Make sure new models are imported in `api/database/__init__.py`.
118+
3. Create a new revision:
119+
```bash
120+
make revision
121+
```
122+
4. Check the new revision file created in `api/database/alembic/versions` and make sure it reflects the changes you expect.
123+
5. Apply the migration to your database (the database needs to be running and available):
124+
```bash
125+
make migrate
126+
```
127+
128+
You can also undo the last migration with:
129+
130+
```bash
131+
make downgrade
132+
```
133+
134+
## Deployment
135+
136+
This template is set up to be easily deployed to Vercel. Vercel provides easy, free, deployment/hosting of web applications and databases. You can also setup the database with a couple click and it should be just a matter of just setting up the environment variables in the Vercel dashboard and deploying the application with a few clicks.
137+
138+
## Contributing
139+
140+
Feel free to contribute to this project. You can open issues for bugs or feature requests, and submit pull requests for improvements or fixes. Make sure to follow the existing code style and conventions. Also, make sure to run the pre-commit hooks before submitting a pull request. If you have any questions, feel free to ask.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fastapi-backend-template"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
description = "A FastAPI backend template."
55
authors = ["ArielMAJ <ariel.maj@hotmail.com>"]
66
readme = "README.md"

0 commit comments

Comments
 (0)