Skip to content
This repository was archived by the owner on Feb 19, 2023. It is now read-only.

Commit 78701b2

Browse files
committed
docs: update README; chore: improve folder structure; feat: add make commands
1 parent 8e68dcf commit 78701b2

File tree

13 files changed

+108
-18
lines changed

13 files changed

+108
-18
lines changed

.env_example

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# app
2-
ENCRYPT_KEY=
3-
SECRET_KEY=
2+
SECRET_KEY=test-key
43
ENV=dev
54
PROJECT_NAME=fastapi_backend_base
65
VERSION=v1
@@ -24,5 +23,5 @@ REDIS_HOST=redis
2423
REDIS_PORT=6379
2524

2625
# Misc
27-
CLIENT_ID=
28-
CLIENT_SECRET=
26+
CLIENT_ID=client-id
27+
CLIENT_SECRET=client-secret

Makefile

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
all:
2+
3+
# docker
4+
up:
5+
@echo "bringing up project...."
6+
docker compose up
7+
8+
down:
9+
@echo "bringing down project...."
10+
docker compose down
11+
12+
# alembic
13+
alembic-scaffold:
14+
@echo "scaffolding migrations folder..."
15+
docker compose exec backend alembic init -t async migrations
16+
17+
alembic-init:
18+
@echo "initializing first migration...."
19+
docker compose exec backend alembic revision --autogenerate -m "init"
20+
21+
alembic-make-migrations:
22+
@echo "creating migration file...."
23+
docker compose exec backend alembic revision --autogenerate -m "add year"
24+
25+
alembic-migrate:
26+
@echo "applying migration...."
27+
docker compose exec backend alembic upgrade head
28+
29+
# lint
30+
test:
31+
@echo "running pytest...."
32+
docker compose exec backend pytest --cov-report xml --cov=src tests/
33+
34+
flake8:
35+
@echo "running flake8...."
36+
docker compose exec backend flake8
37+
38+
black:
39+
@echo "running black...."
40+
docker compose exec backend black .
41+
42+
# misc
43+
check: BREW-exists
44+
BREW-exists: ; @which brew > /dev/null
45+
46+
hooks: check
47+
@echo "installing pre-commit hooks...."
48+
pre-commit install

README.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,42 @@ Small base project I use to build and deploy fastapi backends..batteries include
4040
2. `cd fastapi-backend-base`
4141
3. `mv .env_example .env`
4242
4. `docker compose up --build`
43-
5. visit `http://localhost:8000` for uvicorn server, or `http://localhost` for nginx proxy
43+
5. visit `http://localhost:8666/v1/openapi.json` for uvicorn server, or `http://localhost` for nginx proxy
4444

45-
## Deploy
46-
A common scenario is to use an orchestration tool, such as docker swarm, to deploy your containers to the cloud (DigitalOcean)
45+
## Nginx
46+
```yml
47+
volumes:
48+
proxydata-vol:
49+
...
50+
proxy:
51+
image: your-registry/proxy # OR
52+
build:
53+
context: ./proxy
54+
dockerfile: ./Dockerfile
55+
environment:
56+
- UPSTREAMS=/:backend:8000
57+
- NGINX_SERVER_NAME=yourservername.com
58+
- ENABLE_SSL=true
59+
- HTTPS_REDIRECT=true
60+
61+
- DOMAIN_LIST=yourservername.com
62+
ports:
63+
- '0.0.0.0:80:80'
64+
- '0.0.0.0:443:443'
65+
volumes:
66+
- proxydata-vol:/etc/letsencrypt
67+
```
68+
69+
Some of the envrionment variables available:
70+
- `UPSTREAMS=/:backend:8000` a comma separated list of \<path\>:\<upstream\>:\<port\>. Each of those of those elements creates a location block with proxy_pass in it.
71+
- `HTTPS_REDIRECT=true` enabled a standard, ELB compliant https redirect.
72+
- `ENABLE_SSL=true` to enable redirects to https from http
73+
- `NGINX_SERVER_NAME` name of the server and used as path name to store ssl fullchain and privkey
74+
- `[email protected]` the email to register with Certbot.
75+
- `DOMAIN_LIST` domain(s) you are requesting a certificate for.
4776

48-
## Configuration
77+
When SSL is enabled, server will install Cerbot in standalone mode and add a new daily periodic script to `/etc/periodic/daily/` to run a cronjob in the background. This allows you to automate cert renewing (every 3 months). See [docker-entrypoint](proxy/docker-entrypoint.sh) for details.
4978

50-
## Development
79+
80+
## Deploy
81+
A common scenario is to use an orchestration tool, such as docker swarm, to deploy your containers to the cloud (DigitalOcean).

backend/.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[flake8]
22
max-line-length = 100
3-
exclude = __init__.py,__pycache__,git
3+
exclude = __init__.py,__pycache__,git,migrations/
44
ignore = F403,F405,W605,W503,E203,E231

backend/alembic.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[alembic]
44
# path to migration scripts
5-
script_location = migrations
5+
script_location = src/migrations
66

77
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
88
# Uncomment the line below if you want the files to be prepended with date and time

backend/pyproject.toml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fastapi-backend-base"
3-
version = "2.4.0"
3+
version = "3.0.0"
44
description = "Base project for building fastapi backends"
55
authors = ["nickatnight <[email protected]>"]
66

@@ -32,7 +32,19 @@ sections = 'FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER'
3232
[tool.black]
3333
line-length = 100
3434
include = '\.pyi?$'
35-
exclude = '(\.git|\.hg|\.mypy_cache|\.tox|\.venv|_build)'
35+
extend-exclude = '''
36+
(
37+
| \.git # root of the project
38+
| \.hg
39+
| \.mypy_cache
40+
| \.tox
41+
| \.venv
42+
| _build
43+
| buck-out
44+
| build
45+
| dist
46+
)
47+
'''
3648

3749
[build-system]
3850
requires = ["poetry-core>=1.0.0"]
File renamed without changes.
File renamed without changes.
File renamed without changes.

backend/migrations/versions/d7effbf2730a_init.py renamed to backend/src/migrations/versions/3577cec8a2bb_init.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
"""init
22
3-
Revision ID: d7effbf2730a
3+
Revision ID: 3577cec8a2bb
44
Revises:
5-
Create Date: 2022-09-03 07:30:33.398069
5+
Create Date: 2022-10-05 02:04:44.047062
66
77
"""
88
import sqlalchemy as sa
@@ -11,7 +11,7 @@
1111

1212

1313
# revision identifiers, used by Alembic.
14-
revision = "d7effbf2730a"
14+
revision = "3577cec8a2bb"
1515
down_revision = None
1616
branch_labels = None
1717
depends_on = None

0 commit comments

Comments
 (0)