Skip to content

Commit 9864e54

Browse files
committed
fastapi template
1 parent 491f624 commit 9864e54

35 files changed

+2342
-2
lines changed

.env.example

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Database configuration
2+
DB_CONNECTION=postgresql
3+
DB_HOST=localhost
4+
DB_PORT=5432
5+
DB_USER=postgres
6+
DB_PASSWORD=postgres
7+
DB_NAME=postgres

.gitignore

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
build/
8+
develop-eggs/
9+
dist/
10+
downloads/
11+
eggs/
12+
.eggs/
13+
lib/
14+
lib64/
15+
parts/
16+
sdist/
17+
var/
18+
wheels/
19+
*.egg-info/
20+
.installed.cfg
21+
*.egg
22+
23+
# Virtual Environment
24+
venv/
25+
env/
26+
ENV/
27+
.env
28+
.venv
29+
30+
# FastAPI specific
31+
.pytest_cache/
32+
.coverage
33+
htmlcov/
34+
35+
# IDE specific files
36+
.idea/
37+
.vscode/
38+
*.swp
39+
*.swo
40+
.DS_Store
41+
.env.local
42+
.env.development.local
43+
.env.test.local
44+
.env.production.local
45+
46+
# Logs
47+
*.log
48+
logs/
49+
50+
# Database
51+
*.db
52+
*.sqlite3
53+
54+
# Local development settings
55+
.env.local
56+
.env.development.local
57+
.env.test.local
58+
.env.production.local
59+
60+
# Docker
61+
.docker/
62+
docker-compose.override.yml
63+
64+
# Documentation
65+
docs/_build/

Pipfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
fastapi = {extras = ["standard"], version = "*"}
8+
sqlalchemy = "*"
9+
psycopg2-binary = "*"
10+
python-jose = "*"
11+
bcrypt = "*"
12+
alembic = "*"
13+
fastapi-mail = "*"
14+
15+
[dev-packages]
16+
17+
[requires]
18+
python_version = "3.12"
19+
python_full_version = "3.12.3"

Pipfile.lock

+975
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+95-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,95 @@
1-
# fastapi-template
2-
production ready fastapi template
1+
# FastApi project template
2+
3+
## Setup project
4+
```bash
5+
pipenv shell
6+
pipenv install
7+
```
8+
9+
## Add env
10+
* add env file in root directory
11+
* copy from `.env.example`
12+
13+
```bash
14+
.env
15+
```
16+
17+
## Run project
18+
19+
run the project
20+
```bash
21+
uvicorn app.main:app --reload --port 8000
22+
# or
23+
./run-project.sh
24+
```
25+
26+
## Database migration process with Alembic
27+
Alembic is a database migration tool for SQLAlchemy.
28+
29+
### revision (migration)
30+
you can use the following command to create a new migration file.
31+
32+
```bash
33+
alembic revision --autogenerate -m "initial project"
34+
```
35+
36+
this will create a new migration file in the `alembic/versions` directory.
37+
38+
### upgrade (migrate)
39+
you can use the following command to migrate the database.
40+
41+
```bash
42+
alembic upgrade head
43+
```
44+
45+
## Directory structure
46+
47+
```bash
48+
├── app/
49+
│ ├── api/
50+
│ │ ├── v1/
51+
│ │ │ └── user.py
52+
│ │ ├── health.py
53+
│ │ └── root_index.py
54+
│ ├── config/
55+
│ │ ├── authorization.py
56+
│ │ └── cors.py
57+
│ ├── database/
58+
│ │ └── database.py
59+
│ ├── middleware/
60+
│ │ └── authorization_middleware.py
61+
│ ├── models/
62+
│ │ └── users.py
63+
│ ├── schemas/
64+
│ │ └── user_schema.py
65+
│ ├── serializers/
66+
│ │ └── user_serializer.py
67+
│ ├── services/
68+
│ │ └── user_service.py
69+
│ ├── templates/
70+
│ │ ├── mails/
71+
│ │ │ ├── css/
72+
│ │ │ │ └── mail.css
73+
│ │ │ └── welcome_email.html
74+
│ │ └── user.html
75+
│ ├── utils/
76+
│ │ ├── mailer/
77+
│ │ │ ├── inline_css.py
78+
│ │ │ ├── mail.py
79+
│ │ │ └── mail_templating.py
80+
│ │ ├── base.py
81+
│ │ ├── jwt_utils.py
82+
│ │ ├── paginate.py
83+
│ │ ├── password.py
84+
│ │ └── validation.py
85+
│ ├── __init__.py
86+
│ └── main.py
87+
├── alembic/
88+
├── .env
89+
├── .env.example
90+
├── .gitignore
91+
├── Pipfile
92+
├── Pipfile.lock
93+
├── README.md
94+
└── run-project.sh
95+
```

alembic.ini

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
# Use forward slashes (/) also on windows to provide an os agnostic path
6+
script_location = alembic
7+
8+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
9+
# Uncomment the line below if you want the files to be prepended with date and time
10+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
11+
# for all available tokens
12+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
13+
14+
# sys.path path, will be prepended to sys.path if present.
15+
# defaults to the current working directory.
16+
prepend_sys_path = .
17+
18+
# timezone to use when rendering the date within the migration file
19+
# as well as the filename.
20+
# If specified, requires the python>=3.9 or backports.zoneinfo library.
21+
# Any required deps can installed by adding `alembic[tz]` to the pip requirements
22+
# string value is passed to ZoneInfo()
23+
# leave blank for localtime
24+
# timezone =
25+
26+
# max length of characters to apply to the "slug" field
27+
# truncate_slug_length = 40
28+
29+
# set to 'true' to run the environment during
30+
# the 'revision' command, regardless of autogenerate
31+
# revision_environment = false
32+
33+
# set to 'true' to allow .pyc and .pyo files without
34+
# a source .py file to be detected as revisions in the
35+
# versions/ directory
36+
# sourceless = false
37+
38+
# version location specification; This defaults
39+
# to alembic/versions. When using multiple version
40+
# directories, initial revisions must be specified with --version-path.
41+
# The path separator used here should be the separator specified by "version_path_separator" below.
42+
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
43+
44+
# version path separator; As mentioned above, this is the character used to split
45+
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
46+
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
47+
# Valid values for version_path_separator are:
48+
#
49+
# version_path_separator = :
50+
# version_path_separator = ;
51+
# version_path_separator = space
52+
# version_path_separator = newline
53+
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
54+
55+
# set to 'true' to search source files recursively
56+
# in each "version_locations" directory
57+
# new in Alembic version 1.10
58+
# recursive_version_locations = false
59+
60+
# the output encoding used when revision files
61+
# are written from script.py.mako
62+
# output_encoding = utf-8
63+
64+
sqlalchemy.url =
65+
66+
67+
[post_write_hooks]
68+
# post_write_hooks defines scripts or Python functions that are run
69+
# on newly generated revision scripts. See the documentation for further
70+
# detail and examples
71+
72+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
73+
# hooks = black
74+
# black.type = console_scripts
75+
# black.entrypoint = black
76+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
77+
78+
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
79+
# hooks = ruff
80+
# ruff.type = exec
81+
# ruff.executable = %(here)s/.venv/bin/ruff
82+
# ruff.options = --fix REVISION_SCRIPT_FILENAME
83+
84+
# Logging configuration
85+
[loggers]
86+
keys = root,sqlalchemy,alembic
87+
88+
[handlers]
89+
keys = console
90+
91+
[formatters]
92+
keys = generic
93+
94+
[logger_root]
95+
level = WARNING
96+
handlers = console
97+
qualname =
98+
99+
[logger_sqlalchemy]
100+
level = WARNING
101+
handlers =
102+
qualname = sqlalchemy.engine
103+
104+
[logger_alembic]
105+
level = INFO
106+
handlers =
107+
qualname = alembic
108+
109+
[handler_console]
110+
class = StreamHandler
111+
args = (sys.stderr,)
112+
level = NOTSET
113+
formatter = generic
114+
115+
[formatter_generic]
116+
format = %(levelname)-5.5s [%(name)s] %(message)s
117+
datefmt = %H:%M:%S

alembic/README

Whitespace-only changes.

0 commit comments

Comments
 (0)