Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A few adjustments to make setup easier #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ DB_HOST=localhost:54320
DB_NAME=dbname
DB_URL='postgresql+psycopg2://${DB_HOST}/${DB_NAME}'
SECRET_KEY='9de23e89a1abacb0dea9690970dc21be755cdf5d42a5'
DEV=1
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ aiofiles = "*"
fastapi-static-digest = "~=1.1.0"
fastapi-jinja-utils = {version = "~=1.0"}
fastapi-utils = "*"
typing-extensions = "*"
importlib-resources = "*"

[dev-packages]
pytest = "*"
Expand Down
340 changes: 147 additions & 193 deletions Pipfile.lock

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,25 @@ Explore the [full docs here](./docs/index.md)
## Get Started

1. Copy `.env.example` to `.env` (or create your own)
2. `docker-compose up -d` to start the database
3. `pipenv shell`
4. `FASTAPI_STATICDIGEST_RELOAD=1 uvicorn app.main:create_app --port=8080 --reload`
2. Change the database configuration/credentials in `.env` and `docker-compose.yaml`
3. Create a random secret key and put in your env's SECRET_KEY: `openssl rand -hex 32`
4. Create a new environment and install dependencies: `python -m venv .venv && pipenv install`
5. Use venv and load .env vars: `pipenv shell`
6. Start the database: `docker-compose up -d`
7. Run migrations: `alembic upgrade head`
8. `FASTAPI_STATICDIGEST_RELOAD=1 uvicorn app.main:create_app --port=8080 --reload`

### Configuration

- Change the database configuration/credentials in `.env` and `docker-compose.yaml`
- Set `DEBUG_ADMIN=1` to disable the authorization for the admin panel
PS: Set `DEBUG_ADMIN=1` to disable the authorization for the admin panel

## Why?

The dependency-injection provided by FastAPI is such a huge improvement over any other python web framework. It's OpenAPI integration is excellent, and it's `asgi`-first, unlike Flask or Django.

But Django is famous for its admin panel, and the ability to rapidly build server side applications. For this, we include Flask-Admin, and centralied Jinja2 templates as a dependency.


## Built On

- FastAPI
- [fastapi-utils class-based-view](https://fastapi-utils.davidmontague.xyz/user-guide/class-based-views/)
- [cookiecutter-flask CRUD mixin](https://github.com/cookiecutter-flask/cookiecutter-flask)
- [Flask-Admin](https://flask-admin.readthedocs.io/en/latest/)

12 changes: 10 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from os import environ
import sqlalchemy
from fastapi import FastAPI, Depends, APIRouter
from fastapi.staticfiles import StaticFiles
Expand Down Expand Up @@ -31,6 +32,7 @@ def home(deps):
context = {
"message": "Hello",
"user_count": user_count,
"APP_NAME": settings.APP_NAME,
}
return deps.render("home.html", context)

Expand All @@ -55,11 +57,17 @@ def create_app():
title=settings.APP_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json"
)
static_digest = StaticDigest(source_dir=settings.STATIC_DIR)
static_files = StaticFiles(directory=static_digest.directory)

if not environ.get('DEV'):
static_digest = StaticDigest(source_dir=settings.STATIC_DIR)
static_files = StaticFiles(directory=static_digest.directory)
else:
static_files = StaticFiles(directory=settings.STATIC_DIR)

app.include_router(web, include_in_schema=False)
app.include_router(api, prefix="/api")
app.mount(path="/admin", app=admin_app)
app.mount("/static", static_files, name="static")

return app

21 changes: 13 additions & 8 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
version: '3'
services:
db:
image: postgres:12
environment:
- POSTGRES_DB=dbname
- POSTGRES_USER=dbuser
- POSTGRES_PASSWORD=dbpassword
ports:
- "54320:5432"
db:
image: postgres:12
environment:
- POSTGRES_DB=dbname
- POSTGRES_USER=dbuser
- POSTGRES_PASSWORD=dbpassword
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- '54320:5432'

volumes:
pgdata: