Initial commit
This commit is contained in:
commit
c725c201a7
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
*.swp
|
||||||
|
venv
|
||||||
|
*.pyc
|
||||||
|
__pycache__/*
|
||||||
|
__pycache__
|
||||||
|
*.wpr
|
||||||
|
*.log
|
||||||
|
.idea
|
10
Dockerfile
Normal file
10
Dockerfile
Normal 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
40
model_service/app.py
Normal 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)
|
2
model_service/model/__init__.py
Normal file
2
model_service/model/__init__.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
from .db import db
|
5
model_service/model/db.py
Normal file
5
model_service/model/db.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
|
||||||
|
db = SQLAlchemy()
|
||||||
|
|
2
model_service/schemas/__init__.py
Normal file
2
model_service/schemas/__init__.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
1
model_service/tests/__init__.py
Normal file
1
model_service/tests/__init__.py
Normal file
@ -0,0 +1 @@
|
|||||||
|
import os
|
3
model_service/utils/__init__.py
Normal file
3
model_service/utils/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
from .require_decorators import json_required
|
||||||
|
from .error_handlers import register_all_error_handlers
|
18
model_service/utils/error_handlers.py
Normal file
18
model_service/utils/error_handlers.py
Normal 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))
|
16
model_service/utils/require_decorators.py
Normal file
16
model_service/utils/require_decorators.py
Normal 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
|
2
model_service/views/__init__.py
Normal file
2
model_service/views/__init__.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
from .model_view import ModelView
|
27
model_service/views/model_view.py
Normal file
27
model_service/views/model_view.py
Normal 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
1
requirements-dev.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
pytest
|
11
requirements.txt
Normal file
11
requirements.txt
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
requests
|
||||||
|
blinker
|
||||||
|
Flask
|
||||||
|
marshmallow
|
||||||
|
Flask-Classful
|
||||||
|
Flask-SQLAlchemy
|
||||||
|
SQLAlchemy-Utils
|
||||||
|
SQLAlchemy
|
||||||
|
marshmallow-sqlalchemy
|
||||||
|
psycopg2-binary
|
||||||
|
|
Loading…
Reference in New Issue
Block a user