Skip to content

Commit

Permalink
Switch to FastAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
mekanix committed Jul 26, 2021
1 parent b5fc961 commit e9d586c
Show file tree
Hide file tree
Showing 121 changed files with 840 additions and 2,593 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ ansible/site.yml
build
cbsd.conf
coverage.xml
database.db
fstab
junit.xml
local_config.py
project.mk
site.retry
test.db
vars.mk

dist/
*.egg-info/
*.sqlite
12 changes: 6 additions & 6 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
BSD 2-Clause License

Copyright (c) 2017, Goran Mekić
Copyright (c) 2021, Goran Mekić
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Expand Down
6 changes: 1 addition & 5 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
include README.md
include LICENSE
include common_config.py
include config.py
include name.py
graft migrations
graft alembic
graft tests
graft freenit/project
graft freenit/static
graft freenit/templates
19 changes: 1 addition & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
# Backend Startkit
Template to get my favourite SQL stack up and running


![diagram](https://github.com/mekanix/backend-startkit/raw/master/backend.png)

## Forking
The `name.py` is special! Although it ends with .py, it is read by shell scripts and CBSD/Reggae `Makefile` (if you're using it). Because it's not regular Python file, it has some limitations. It should consist of one line:

```
app_name="application" # noqa: E225
```

There must be no space around `=` in the previous example, otherwise shell scripts won't work. The `noqa` part prevents `flake8` failing the test, because it normally requires spaces around `=`.

On fork, edit `name.py` and rename `application` directory acordingly.

[Backend Tutorial](https://github.com/freenit-framework/backend-tutorial)
# fastapi-playground
89 changes: 89 additions & 0 deletions alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = alembic

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# sys.path path, will be prepended to sys.path if present.
# defaults to the current working directory.
prepend_sys_path = .

# timezone to use when rendering the date
# within the migration file as well as the filename.
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =

# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

# version location specification; this defaults
# to alembic/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat alembic/versions

# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url = sqlite:///db.sqlite


[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples

# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks = black
# black.type = console_scripts
# black.entrypoint = black
# black.options = -l 79 REVISION_SCRIPT_FILENAME

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
1 change: 1 addition & 0 deletions alembic/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
15 changes: 15 additions & 0 deletions alembic/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os
import sys

import freenit.app
from alembic import context
from freenit.migration import run_migrations_offline, run_migrations_online


sys.path.append(os.getcwd())


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
24 changes: 24 additions & 0 deletions alembic/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
39 changes: 39 additions & 0 deletions alembic/versions/14eb8b6e464c_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""initial
Revision ID: 14eb8b6e464c
Revises:
Create Date: 2021-07-18 12:28:52.118800
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "14eb8b6e464c"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"users",
sa.Column("id", sa.CHAR(36), nullable=False),
sa.Column("email", sa.String(length=255), nullable=False),
sa.Column("hashed_password", sa.String(length=255), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=False),
sa.Column("is_superuser", sa.Boolean(), nullable=False),
sa.Column("is_verified", sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_users_email"), "users", ["email"], unique=True)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_users_email"), table_name="users")
op.drop_table("users")
# ### end Alembic commands ###
File renamed without changes.
3 changes: 1 addition & 2 deletions ansible/roles/devel/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
---
- name: install devel packages
with_items: "{{ devel_packages }}"
pkgng:
package:
name: "{{ item.name }}"
7 changes: 0 additions & 7 deletions application.py

This file was deleted.

Binary file removed backend.dia
Binary file not shown.
Binary file removed backend.png
Binary file not shown.
17 changes: 7 additions & 10 deletions bin/build.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
#!/bin/sh


set -e

export BIN_DIR=`dirname $0`
export PROJECT_ROOT="${BIN_DIR}/.."
BIN_DIR=`dirname $0`
PROJECT_DIR="${BIN_DIR}"
. ${BIN_DIR}/common.sh
setup


if [ "${OFFLINE}" != "yes" ]; then
pip install -U --upgrade-strategy eager -e '.[dev]'
fi
setup no
pip install -U pip
pip install -U wheel
pip install -U --upgrade-strategy eager -e ".[build]"


cd ${PROJECT_ROOT}
rm -rf *.egg-info build dist
python setup.py sdist bdist_wheel
9 changes: 2 additions & 7 deletions bin/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ export BIN_DIR=`dirname $0`
export PROJECT_ROOT="${BIN_DIR}/.."
. "${PROJECT_ROOT}/name.py"
export VIRTUALENV=${VIRTUALENV:="${app_name}"}
export FLASK_ENV=${FLASK_ENV:="production"}
export PY_VERSION=${PY_VERSION:="3.8"}
export FREENIT_ENV=${FREENIT_ENV:="prod"}
export SYSPKG=${SYSPKG:="no"}
export DBTYPE=${DBTYPE:="sql"}


setup() {
Expand All @@ -23,10 +21,7 @@ setup() {
if [ "${update}" != "no" ]; then
pip install -U pip
pip install -U wheel
pip install -U --upgrade-strategy eager -e ".[${DBTYPE}]"
pip install -U --upgrade-strategy eager -e .
fi
fi
if [ -e "${BIN_DIR}/../migrations/main/001_initial.py" ]; then
# flask migration run
fi
}
16 changes: 6 additions & 10 deletions bin/devel.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
#!/bin/sh


BIN_DIR=`dirname $0`
export FLASK_PORT=${FLASK_PORT:=5000}
export FLASK_ENV="development"
export FREENIT_ENV="dev"
export OFFLINE=${OFFLINE:="no"}
export SYSPKG="no"


. ${BIN_DIR}/common.sh
setup no

if [ "${SYSPKG}" = "no" ]; then
if [ "${OFFLINE}" != "yes" ]; then
pip install -U --upgrade-strategy eager -e '.[dev]'
fi
if [ ! -e "alembic/versions" ]; then
mkdir alembic/versions
alembic revision --autogenerate -m initial
alembic upgrade head
fi


echo "Backend"
echo "==============="
uwsgi --master --http 0.0.0.0:${FLASK_PORT} ${WEBSOCKET_FLAGS} --python-auto-reload 1 --honour-stdin --wsgi-file devel.py
uvicorn main:app --host 0.0.0.0 --port 5000 --reload
Loading

0 comments on commit e9d586c

Please sign in to comment.