Skip to content

Commit 3269dc8

Browse files
committed
Add makefile shortcuts
1 parent 2c9ac33 commit 3269dc8

File tree

5 files changed

+173
-4
lines changed

5 files changed

+173
-4
lines changed

Diff for: .gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ dmypy.json
131131
.pyre/
132132

133133
db_docker
134-
pgadmin
135134
minio
136135

137136
.sonarqube/*

Diff for: Makefile

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/make
2+
3+
include .env
4+
5+
define SERVERS_JSON
6+
{
7+
"Servers": {
8+
"1": {
9+
"Name": "fastapi-alembic",
10+
"Group": "Servers",
11+
"Host": "$(DATABASE_HOST)",
12+
"Port": 5432,
13+
"MaintenanceDB": "postgres",
14+
"Username": "$(DATABASE_PASSWORD)",
15+
"SSLMode": "prefer",
16+
"PassFile": "/tmp/pgpassfile"
17+
}
18+
}
19+
}
20+
endef
21+
export SERVERS_JSON
22+
23+
help:
24+
@echo "make"
25+
@echo " install"
26+
@echo " Install all packages of poetry project locally."
27+
@echo " run-dev-build"
28+
@echo " Run development docker compose and force build containers."
29+
@echo " run-dev"
30+
@echo " Run development docker compose."
31+
@echo " stop-dev"
32+
@echo " Stop development docker compose."
33+
@echo " init-db"
34+
@echo " Init database with sample data."
35+
@echo " add-dev-migration"
36+
@echo " Add new database migration using alembic."
37+
@echo " run-pgadmin"
38+
@echo " Run pgadmin4."
39+
@echo " load-server-pgadmin"
40+
@echo " Load server on pgadmin4."
41+
@echo " clean-pgadmin"
42+
@echo " Clean pgadmin4 data."
43+
44+
install:
45+
cd fastapi-alembic-sqlmodel-async && \
46+
poetry shell && \
47+
poetry install
48+
49+
run-dev-build:
50+
docker compose -f docker-compose-dev.yml up --build
51+
52+
run-dev:
53+
docker compose -f docker-compose-dev.yml up
54+
55+
stop-dev:
56+
docker compose -f docker-compose-dev.yml down
57+
58+
init-db:
59+
docker compose -f docker-compose-dev.yml exec fastapi_server python app/initial_data.py
60+
61+
add-dev-migration:
62+
docker compose -f docker-compose-dev.yml exec fastapi_server alembic revision --autogenerate && \
63+
docker compose -f docker-compose-dev.yml exec fastapi_server alembic upgrade head
64+
65+
run-pgadmin:
66+
echo "$$SERVERS_JSON" > ./pgadmin/servers.json && \
67+
docker volume create pgadmin_data && \
68+
docker compose -f pgadmin.yml up
69+
70+
load-server-pgadmin:
71+
docker exec -it pgadmin python /pgadmin4/setup.py --load-servers servers.json
72+
73+
clean-pgadmin:
74+
docker volume rm pgadmin_data

Diff for: README.md

+53-3
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,64 @@ This is a project template which uses [FastAPI](https://fastapi.tiangolo.com/),
66

77
Create an **.env** file on root folder and copy the content from **.env.example**. Feel free to change it according to your own configuration.
88

9-
## Run project using Docker compose
9+
## Run the project using Docker containers and forcing build containers
1010

11+
*Using docker compose command*
1112
```sh
1213
docker compose -f docker-compose-dev.yml up --build
1314
```
1415

16+
*Using Makefile command*
17+
```sh
18+
make run-dev-build
19+
```
20+
21+
## Run project using Docker containers
22+
23+
*Using docker compose command*
24+
```sh
25+
docker compose -f docker-compose-dev.yml up
26+
```
27+
28+
*Using Makefile command*
29+
```sh
30+
make run-dev
31+
```
32+
1533
## Setup database with initial data
16-
This creates a sample users on database.
34+
This creates sample users on database.
35+
36+
*Using docker compose command*
1737
```
1838
docker compose -f docker-compose-dev.yml exec fastapi_server python app/initial_data.py
1939
```
2040

41+
*Using Makefile command*
42+
```sh
43+
make init-db
44+
```
45+
46+
Any of the above commands creates three users with the following passwords:
47+
2148
- **Admin credentials ->** *username:* [email protected] and *password:* admin
2249
- **Manager credentials ->** *username:* [email protected] and *password:* admin
2350
- **User credentials ->** *username:* [email protected] and *password:* admin
2451

25-
You can connect to the Database using PGAdmin4 and use the credentials from .env file. Database port on local machine has been configured to **5454** on docker-compose-dev.yml file
52+
You can connect to the Database using pgAdmin4 and use the credentials from .env file. Database port on local machine has been configured to **5454** on docker-compose-dev.yml file
53+
54+
(Optional) If you prefer you can run pgAdmin4 on a docker container using the following commands, they should executed on different terminals:
55+
56+
*Starts pgadmin*
57+
```sh
58+
make run-pgadmin
59+
```
60+
61+
*Load server configuration (It is required just the first time)*
62+
```sh
63+
make load-server-pgadmin
64+
```
65+
66+
This starts pgamin in [http://localhost:15432](http://localhost:15432).
2667

2768
<p align="center">
2869
<img src="static/tables.png" align="center"/>
@@ -72,15 +113,22 @@ This template allows users can upload their photos. The images are stored using
72113

73114
## Run Alembic migrations (Only if you change the DB model)
74115

116+
*Using docker compose command*
75117
```sh
76118
docker compose -f docker-compose-dev.yml exec fastapi_server alembic revision --autogenerate
77119
docker compose -f docker-compose-dev.yml exec fastapi_server alembic upgrade head
78120
```
79121

122+
*Using Makefile command*
123+
```sh
124+
make init-db
125+
```
126+
80127
## Production Deployment
81128
Remember to use a persistant PostgreSQL database, update the new credentials on .env file and use this command to run the project in a production environment. For testing this configuration on localhost you can uncomment the database container and
82129
depends_on of fastapi container otherwise it will not work on a local environment.
83130

131+
*Using docker compose command*
84132
```sh
85133
docker compose up --build
86134
```
@@ -96,6 +144,7 @@ docker compose up --build
96144
- [fastapi-async-sqlalchemy](https://github.com/h0rn3t/fastapi-async-sqlalchemy).
97145
- [fastapi-minio](https://github.com/Longdh57/fastapi-minio).
98146
- [fastapi-best-practices](https://github.com/zhanymkanov/fastapi-best-practices).
147+
- [pgadmin Makefile](https://gist.github.com/alldevic/b2a0573e5464fe91fd118024f33bcbaa).
99148

100149
## TODO List:
101150

@@ -115,6 +164,7 @@ docker compose up --build
115164
- [x] Add one to one relationship sample
116165
- [x] Add sample to upload images and store them using minio
117166
- [x] Invalidate access and refresh tokens when the password is changed using Redis
167+
- [x] Add shortcuts using a Makefile
118168
- [ ] Install pg_trgm by code and add a query for smart search of users by name
119169
- [ ] Add Enum sample column
120170
- [ ] Add jsonb field on table sample

Diff for: pgadmin.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: '3.8'
2+
3+
services:
4+
pgadmin:
5+
container_name: pgadmin
6+
image: dpage/pgadmin4:4.16
7+
restart: always
8+
volumes:
9+
- ./pgadmin/servers.json:/pgadmin4/servers.json
10+
- pgadmin_data:/var/lib/pgadmin:rw
11+
ports:
12+
- 15432:15432
13+
env_file: ".env"
14+
environment:
15+
PGADMIN_CONFIG_SERVER_MODE: 0
16+
PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 0
17+
# Default user for desktop mode (SERVER_MODE = False)
18+
PGADMIN_DEFAULT_EMAIL: [email protected]
19+
# Default password for desktop mode (SERVER_MODE = False)
20+
PGADMIN_DEFAULT_PASSWORD: password
21+
PGADMIN_LISTEN_ADDRESS: 0.0.0.0
22+
PGADMIN_LISTEN_PORT: 15432
23+
entrypoint:
24+
- "/bin/sh"
25+
- "-c"
26+
- "/bin/echo '${DATABASE_HOST}:5432:*:${DATABASE_USER}:${DATABASE_PASSWORD}' > /tmp/pgpassfile && /entrypoint.sh"
27+
28+
29+
volumes:
30+
pgadmin_data:
31+
32+

Diff for: pgadmin/servers.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Servers": {
3+
"1": {
4+
"Name": "fastapi-alembic",
5+
"Group": "Servers",
6+
"Host": "database",
7+
"Port": 5432,
8+
"MaintenanceDB": "postgres",
9+
"Username": "postgres",
10+
"SSLMode": "prefer",
11+
"PassFile": "/tmp/pgpassfile"
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)