Skip to content

Commit 9f20d9d

Browse files
committed
Switches from flask to FastAPI.
1 parent 7875d83 commit 9f20d9d

27 files changed

+1712
-1883
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ Once the database, server, and webapp are running, the database itself needs to
7575
To update your database to the latest version, first start the database and server containers, run the relevant docker-compose alias command (above), then run:
7676
```sh
7777
#sh
78-
./server.sh db upgrade
78+
./server.sh alembic upgrade
7979
```
8080

8181
```powershell
8282
#powershell
83-
./server.ps1 db upgrade
83+
./server.ps1 alembic upgrade
8484
```
8585

8686
Whenever changes to the database schema for Flow are made, new migration(s) get created in the server/migrations/versions/ directory.

docker-compose.dev-nodb.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818
context: server
1919
args:
2020
REACT_APP_VERSION: local+devcontainer
21-
command: "python -m flask run --host=0.0.0.0 --port=5000"
21+
command: uvicorn main:app --port 5000 --host 0.0.0.0 --timeout-keep-alive 120 --reload --log-level trace
2222
environment:
2323
- "SQLALCHEMY_ECHO=True"
2424
volumes:

docker-compose.dev.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818
context: server
1919
args:
2020
REACT_APP_VERSION: local+devcontainer
21-
command: "python -m flask run --host=0.0.0.0 --port=5000"
21+
command: uvicorn main:app --port 5000 --host 0.0.0.0 --timeout-keep-alive 120 --reload --log-level trace
2222
environment:
2323
- "SQLALCHEMY_ECHO=True"
2424
volumes:

server/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ COPY ./ .
1818
ENV FLASK_APP=/app/main.py
1919
ENV SERVER_VERSION=$SERVER_VERSION
2020

21-
CMD gunicorn --bind 0.0.0.0:${PORT} main:app --access-logfile - --error-logfile - --timeout 120 --limit-request-line 0 --workers 4 --log-level=DEBUG
21+
CMD uvicorn main:app --port ${PORT} --host 0.0.0.0 --timeout-keep-alive 120 --log-level debug

server/Pipfile

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ verify_ssl = true
44
name = "pypi"
55

66
[packages]
7-
alembic_utils = "*"
8-
flask = "*"
9-
flask-cors = "*"
10-
flask-migrate = "*"
11-
flask-restx = "*"
12-
flask-sqlalchemy = "*"
13-
gunicorn = "*"
14-
python-jose = "*"
7+
alembic = "*"
8+
fastapi = "*"
9+
fastapi-cloudauth = "*"
10+
graphene = "*"
11+
graphene-sqlalchemy = "*"
12+
sqlalchemy = "*"
13+
pydantic = "*"
14+
uvicorn = "*"
15+
python-multipart = "*"
1516
pg8000 = "==1.16.5"
16-
Werkzeug = "*"
1717
casbin = "*"
1818
casbin-sqlalchemy-adapter = "*"
1919
deepdiff = {extras = ["murmur"], version = "*"}

server/Pipfile.lock

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

server/api/group.py

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
from flask_restx import Resource, fields, Namespace
1+
from typing import List
22

3-
from authorization import requires_auth, requires_scope, get_all_roles
3+
from authorization import get_all_roles
4+
from server import app
5+
from models import Group
46

57

6-
api = Namespace('groups', description='Extra-Simple operations on groups.', path='/')
7-
8-
9-
group_output = api.model('GroupOutput', { 'id': fields.String() })
10-
groups_output = fields.List(fields.Nested(group_output))
11-
12-
13-
@api.route('/group')
14-
class GroupsResource(Resource):
15-
@api.doc(security='token', model=groups_output)
16-
@requires_auth
17-
# @requires_scope('read:groups')
18-
def get(self):
19-
return [
20-
{'id': role}
21-
for role
22-
in get_all_roles()
23-
]
8+
@app.get('/group', tags=['groups'], response_model=List[Group], response_model_exclude_none=True)
9+
async def get_groups():
10+
return [
11+
Group(id=role)
12+
for role
13+
in get_all_roles()
14+
]

server/api/health.py

+30-33
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1-
from flask_restx import Resource, fields, Namespace
2-
3-
from server import app, db
4-
5-
6-
api = Namespace('server-health', description="Service health checks.", path='/')
7-
8-
9-
health_output = api.model('HealthOutput', {
10-
'version': fields.String(),
11-
'server': fields.Boolean(),
12-
'database': fields.Boolean(),
13-
'database_error': fields.String()
14-
})
15-
16-
17-
@api.route('/health')
18-
class HealthResource(Resource):
19-
@api.doc(model=health_output)
20-
def get(self):
21-
status = {
22-
'version': app.config['SERVER_VERSION'],
23-
'server': True,
24-
'database': True
25-
}
26-
27-
try:
28-
db.session.execute('SELECT 1')
29-
except Exception as err:
30-
status['database'] = False
31-
status['database_error'] = str(err)
32-
return status, 500
33-
return status
1+
from typing import Union
2+
from fastapi import Depends
3+
from fastapi.responses import JSONResponse
4+
from sqlalchemy.orm import Session
5+
6+
from authorization import get_all_roles
7+
from server import app, get_db
8+
from settings import settings
9+
from models import HealthCheck
10+
11+
12+
@app.get('/health', tags=['system'], response_model=HealthCheck, response_model_exclude_none=True)
13+
async def health_check(db: Session = Depends(get_db)):
14+
status = {
15+
'version': settings.server_version,
16+
'server': True,
17+
'database': True
18+
}
19+
20+
try:
21+
db.execute('SELECT 1')
22+
except Exception as err:
23+
status['database'] = False
24+
status['database_error'] = str(err)
25+
return JSONResponse(
26+
status_code=500,
27+
content=status,
28+
)
29+
# return HealthCheck(**status)
30+
return status

0 commit comments

Comments
 (0)