-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
105 lines (84 loc) · 2.61 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import json
import logging
from logging.config import dictConfig
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from flask import Flask, request
from flask_apispec import FlaskApiSpec, doc
from flask_cors import CORS
from healthcheck import HealthCheck
from api.v1.cv import bp as cv_bp_v1
from api.v1.cv.tonal import opencv_skin_tone_v1
from api.v2.cv import bp as cv_bp_v2
from api.v2.cv.tonal import mediapipe_skin_tone_v2, opencv_skin_tone_v2
from api.v3.cv import bp as cv_bp_v3
from api.v3.cv.tonal import skin_tone_v3
from core import settings
from core.loger import LOGGING
from core.settings import CV_ALLOWED_HOSTS, DEBUG
from schemas.error_schema import ErrorSchema
description = """
CV API helps you do awesome stuff. 🚀
## Tonal
You will be able to:
* **Get tone of your skin by photo**
"""
spec = APISpec(
openapi_version="2.0.0",
title="Cv api",
version="v1",
plugins=[MarshmallowPlugin()],
info={
"description": description,
},
)
spec.tag(
{
"name": "tonal",
"description": "CV operations for tonal cream recommendation",
}
)
dictConfig(LOGGING)
app = Flask(__name__)
CORS(app, resources={r"/api/*": {"origins": "*"}})
app.config.from_object(settings)
health = HealthCheck()
@app.route("/ht")
@doc(
description="Check service health",
)
def health_check():
return json.loads(health.run()[0])
app.config.update(
{
"APISPEC_SPEC": spec,
"APISPEC_SWAGGER_URL": settings.APISPEC_SWAGGER_URL,
"APISPEC_SWAGGER_UI_URL": settings.APISPEC_SWAGGER_UI_URL,
}
)
docs = FlaskApiSpec(app, document_options=False)
app.register_blueprint(cv_bp_v1)
app.register_blueprint(cv_bp_v2)
app.register_blueprint(cv_bp_v3)
docs.register(opencv_skin_tone_v1, blueprint=cv_bp_v1.name)
docs.register(opencv_skin_tone_v2, blueprint=cv_bp_v2.name)
docs.register(mediapipe_skin_tone_v2, blueprint=cv_bp_v2.name)
docs.register(skin_tone_v3, blueprint=cv_bp_v3.name)
docs.register(health_check)
@app.errorhandler(422)
def handle_validation_error(err):
exc = err.exc
return ErrorSchema().dump({"error": exc.messages}), 422
@app.before_request
def check_for_maintenance():
if not DEBUG:
if CV_ALLOWED_HOSTS == ["*"]:
return
if request.host.split(":")[0] not in CV_ALLOWED_HOSTS:
return ErrorSchema().dump({"error": {"host": "host is not allowed!"}}), 403
if __name__ == "__main__":
app.run(debug=DEBUG, port=8000)
else:
gunicorn_logger = logging.getLogger("gunicorn.error")
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)