Skip to content
Snippets Groups Projects
Commit 16157ce8 authored by David Trattnig's avatar David Trattnig
Browse files

Improved SQLAlchemy initialization.

parent 2cbf3b5d
No related branches found
No related tags found
No related merge requests found
......@@ -19,5 +19,5 @@ COPY config/sample/gunicorn/sample-docker.gunicorn.conf.py config/docker/gunicor
EXPOSE 8008
ENTRYPOINT ["gunicorn"]
CMD ["-c", "/srv/config/docker/gunicorn.conf.py", "src.server:app"]
CMD ["-c", "/srv/config/docker/gunicorn.conf.py", "src.app:app"]
......@@ -44,7 +44,7 @@ if [[ $docker == "false" ]]; then
# echo "Building Web Applications"
# sh ./script/build-web.sh
echo "Starting API Server"
/usr/bin/env python3.7 src/server.py
/usr/bin/env python3.7 src/app.py
fi
### Runs the API Server using Gunicorn without a system daemon (Production) ###
......@@ -53,7 +53,7 @@ if [[ $docker == "false" ]]; then
echo "Activating Python Environment"
source ../python-env/bin/activate
echo "Starting API Server"
gunicorn -c config/gunicorn.conf.py src.server:app
gunicorn -c config/gunicorn.conf.py src.app:app
fi
if [[ $mode == "test" ]]; then
......@@ -64,7 +64,7 @@ if [[ $docker == "false" ]]; then
### CAUTION: This deletes everything in your database ###
if [[ $mode == "recreate-database" ]]; then
/usr/bin/env python3.7 src/server.py --recreate-database
/usr/bin/env python3.7 src/app.py --recreate-database
fi
fi
......
......@@ -45,7 +45,7 @@ setup(
package_data={'': ['src/rest/swagger/swagger.yaml']},
include_package_data=True,
entry_points={
'console_scripts': ['src.server=src.server.__main__:main']
'console_scripts': ['src.app=src.app.__main__:main']
},
long_description="""\
This is the AURA Engine API. Read more at https://gitlab.servus.at/aura/engine.
......
......@@ -23,36 +23,35 @@ import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import connexion
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from base.config import AuraConfig
from base.logger import AuraLogger
from rest import encoder
from service import ApiService
from models import db, ma
# App Initialization
config = AuraConfig()
logger = AuraLogger(config, "engine-api").logger
def configure_flask(app):
api.app.json_encoder = encoder.JSONEncoder
def build_app(app):
app.json_encoder = encoder.JSONEncoder
app.config["SQLALCHEMY_DATABASE_URI"] = config.get_database_uri()
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['ENV'] = "development"
app.config['FLASK_ENV'] = "development"
if config.get("debug_flask") == "true":
app.config['DEBUG'] = True
db.init_app(app)
ma.init_app(app)
return app
api = connexion.App(__name__, specification_dir='rest/swagger')
api.add_api('swagger.yaml', arguments={'title': 'AURA Engine API'}, pythonic_params=True)
app = configure_flask(api.app)
db = SQLAlchemy(app)
db.init_app(app)
ma = Marshmallow(app)
app = build_app(api.app)
def startup():
......@@ -67,8 +66,9 @@ with app.app_context():
"""
Initialize Server.
"""
db.create_all()
service = ApiService(config, logger)
logger.info("API Service initialized.")
logger.info("API server initialized.")
if __name__ == '__main__':
......
......@@ -21,10 +21,11 @@
import datetime
from sqlalchemy import create_engine, Column, DateTime, String, Integer, Boolean
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from src.server import db, ma
db = SQLAlchemy()
ma = Marshmallow()
class PlayLog(db.Model):
......@@ -144,7 +145,3 @@ class TrackServiceSchema(ma.SQLAlchemySchema):
"track_title"
)
# Create Tables
db.create_all()
......@@ -15,7 +15,7 @@ def current_track(): # noqa: E501
:rtype: PlayLogEntry
"""
from src.server import service
from src.app import service
return service.current_track()
......@@ -31,5 +31,5 @@ def list_tracks(offset=None, limit=None): # noqa: E501
:rtype: List[PlayLogEntry]
"""
from src.server import service
from src.app import service
return service.list_tracks(offset, limit)
\ No newline at end of file
......@@ -19,6 +19,7 @@
import datetime
from models import PlayLog, TrackServiceSchema
class ApiService():
......@@ -42,7 +43,6 @@ class ApiService():
Returns:
(PlayLogEntry)
"""
from models import PlayLog, TrackServiceSchema
track = PlayLog.select_current()
track_schema = TrackServiceSchema()
return track_schema.dump(track)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment