Skip to content

Commit 1f18a4a

Browse files
authored
Merge pull request #21 from flavienbwk/develop
1.3.0 : The right way to package your Python API
2 parents 2e421a5 + 0c68c6e commit 1f18a4a

33 files changed

+82
-50
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FLASK_API_VERSION="1.2.0"
1+
FLASK_API_VERSION="1.3.0"
22
FLASK_SERVER_NAME="My API Project"
33
FLASK_SERVER_DESCRIPTION="Dockerized Flask API boilerplate using an LDAP and token-based authentication"
44
FLASK_SECRET_KEY="Some secret key"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
- [Logging and logs rotation](./api/app/src/utils/Logger.py#L12)
2121
- [Choose](./app/app/src/App.js#L65) between sidebar and navbar (or use both !)
2222
- Responsive design
23-
- [Production](./prod.docker-compose.yml) and [development](./docker-compose.yml) builds
23+
- [Development](./docker-compose.yml), [production](./prod.docker-compose.yml) and [Kubernetes](./k8s) builds
24+
- State-of-the-art [Python packaging](./api/app/setup.py)
2425

2526
## API documentation
2627

api/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ FROM python:3.7-alpine
44
RUN apk update && apk add openldap-dev libc-dev gcc g++
55

66
# psycopg2 requirements
7-
RUN apk add libpq python3-dev musl-dev postgresql-dev
7+
RUN apk add libpq python3-dev py3-pip musl-dev postgresql-dev
88

9-
COPY ./requirements.txt /requirements.txt
9+
COPY ./app/requirements.txt /requirements.txt
1010
RUN pip install -r /requirements.txt
1111

1212
COPY ./entrypoint.sh /entrypoint.sh

api/app/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
__pycache__/
2+
build/
3+
dist/
4+
*.egg-info

api/app/main.py renamed to api/app/my_app/__init__.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from time import sleep
12
from flask_migrate import Migrate, MigrateCommand
23
from flask_script import Manager
34

4-
from src.routes import blueprint
5-
from src.app import create_app, database
6-
from src.utils.ApiResponse import ApiResponse
5+
from .routes import blueprint
6+
from .app import create_app, database, logger
7+
from .utils.ApiResponse import ApiResponse
8+
79

810
# Initialization
911

@@ -13,14 +15,23 @@
1315

1416
manager = Manager(app)
1517
migrate = Migrate(app, database.getDatabase(), "/migrations")
18+
19+
## Waiting for database to be available
20+
while database.isDatabaseAvailable(app) is False:
21+
logger.warning("Database unreachable. Waiting for 3 seconds to be up...")
22+
sleep(3.0)
23+
logger.info("Database is up and running ✓")
24+
database.initDatabase(app)
1625
manager.add_command('db', MigrateCommand)
1726

27+
1828
# Commands
1929

2030
@manager.command
2131
def run():
2232
app.run(debug=True, port=5000, host="0.0.0.0")
2333

34+
2435
# System functions
2536

2637
@app.after_request
@@ -32,5 +43,6 @@ def after_request(response):
3243
response, http_code = ApiResponse.formatFlaskResponse(response)
3344
return app.make_response((response, http_code))
3445

46+
3547
if __name__ == '__main__':
36-
manager.run()
48+
manager.run()

api/app/my_app/__main__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This file allows "my-app" package to be run
2+
# via `python -m` command line.
3+
from . import manager
4+
5+
if __name__ == '__main__':
6+
manager.run()

api/app/src/app.py renamed to api/app/my_app/app.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
from time import sleep
32
from flask import Flask
43

54
from .utils.Logger import Logger
@@ -12,7 +11,7 @@
1211
FLASK_LEVEL = os.environ.get("FLASK_LEVEL", "dev")
1312

1413
logger = Logger()
15-
database = Database()
14+
database = Database(logger)
1615

1716
def page_not_found(e):
1817
apiResponse = ApiResponse()
@@ -25,11 +24,4 @@ def create_app():
2524
app.register_error_handler(404, page_not_found)
2625
app.config.from_object(config_by_name[FLASK_LEVEL])
2726
os.environ["FLASK_ENV"] = config_by_name[FLASK_LEVEL].FLASK_ENV
28-
29-
# Waiting for database to be available
30-
while database.isDatabaseAvailable(app) is False:
31-
logger.warning("Database unreachable. Waiting for 3 seconds to be up...")
32-
sleep(3.0)
33-
logger.info("Database is up and running ✓")
34-
database.initDatabase(app)
3527
return app
File renamed without changes.
File renamed without changes.

api/app/src/controller/auth_controller.py renamed to api/app/my_app/controller/auth_controller.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from flask import request, escape
22
from flask_restplus import Resource, Namespace, fields
33

4-
from ..utils.Logger import Logger
5-
64
from ..service.auth_service import AuthService, requires_authentication
75

8-
logger = Logger()
9-
106
# DTOs
117

128
api = Namespace('Auth', description='Authentication-related operations')

0 commit comments

Comments
 (0)