Initial commit

This commit is contained in:
Pünkösd Marcell 2020-04-13 21:14:06 +02:00
commit c725c201a7
14 changed files with 146 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
*.swp
venv
*.pyc
__pycache__/*
__pycache__
*.wpr
*.log
.idea

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM python:3
ADD model_service requirements.txt /model_service/
WORKDIR /model_service/
RUN pip3 install -r requirements.txt && pip3 install gunicorn
EXPOSE 8000
CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]

40
model_service/app.py Normal file
View File

@ -0,0 +1,40 @@
#!/usr/bin/env python3
import os
from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
# import stuff
from model import db
from utils import register_all_error_handlers
# import views
from views import ModelView
# create flask app
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1)
# configure flask app
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URI', "sqlite://") # Default to memory db
# important stuff
app.secret_key = os.environ.get('SECRET_KEY', os.urandom(12))
# disable this for better performance
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# initialize stuff
db.init_app(app)
with app.app_context():
db.create_all()
# register error handlers
register_all_error_handlers(app)
# register views
for view in [ModelView]:
view.register(app, trailing_slash=False)
# start debuggig if needed
if __name__ == "__main__":
app.run(debug=True)

View File

@ -0,0 +1,2 @@
#!/usr/bin/env python3
from .db import db

View File

@ -0,0 +1,5 @@
#!/usr/bin/env python3
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()

View File

@ -0,0 +1,2 @@
#!/usr/bin/env python3

View File

@ -0,0 +1 @@
import os

View File

@ -0,0 +1,3 @@
#!/usr/bin/env python3
from .require_decorators import json_required
from .error_handlers import register_all_error_handlers

View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
def get_standard_error_handler(code: int):
def error_handler(err):
return {"msg": str(err)}, code
return error_handler
# function to register all handlers
def register_all_error_handlers(app):
error_codes_to_override = [404, 403, 401, 405, 400, 409, 422]
for code in error_codes_to_override:
app.register_error_handler(code, get_standard_error_handler(code))

View File

@ -0,0 +1,16 @@
#!/usr/bin/env python3
from flask import request, current_app, abort
from functools import wraps
def json_required(f):
@wraps(f)
def call(*args, **kwargs):
if request.is_json:
return f(*args, **kwargs)
else:
abort(400, "JSON required")
return call

View File

@ -0,0 +1,2 @@
#!/usr/bin/env python3
from .model_view import ModelView

View File

@ -0,0 +1,27 @@
#!/usr/bin/env python3
from flask import request, jsonify
from flask_classful import FlaskView, route
from model import db
from utils import json_required
import requests.exceptions
class ModelView(FlaskView):
def post(self):
return jsonify({})
def get(self, _id: str):
return jsonify({"id": _id})
@route('<_id>/details')
def get_details(self, _id: str):
return jsonify({"id": _id})
def delete(self, _id: str):
return jsonify({"id": _id})
@route('$default', methods=['PUT'])
def put_default(self):
return jsonify({})

1
requirements-dev.txt Normal file
View File

@ -0,0 +1 @@
pytest

11
requirements.txt Normal file
View File

@ -0,0 +1,11 @@
requests
blinker
Flask
marshmallow
Flask-Classful
Flask-SQLAlchemy
SQLAlchemy-Utils
SQLAlchemy
marshmallow-sqlalchemy
psycopg2-binary