69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
"""contracts-flask v2.0 — полный перенос с ВМ на Flask.
|
|
Больше никаких прокси на contracts.kube5s.ru — всё локально.
|
|
"""
|
|
import sys, os, hmac
|
|
# site/ в sys.path — импортируем модули напрямую, без префиксов
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
# корень репо — для переиспользуемого модуля upload/ (как в drhider)
|
|
_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
if _REPO_ROOT not in sys.path:
|
|
sys.path.insert(0, _REPO_ROOT)
|
|
|
|
from flask import Flask, jsonify, request
|
|
from config import VERSION, MAX_CONTENT_LENGTH, API_KEY
|
|
from routes import register_routes
|
|
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
app.config["VERSION"] = VERSION
|
|
app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH
|
|
app.config["API_KEY"] = API_KEY
|
|
|
|
_init_db()
|
|
register_routes(app)
|
|
|
|
@app.before_request
|
|
def authorize_request():
|
|
if request.endpoint == "health.health":
|
|
return None
|
|
key = app.config.get("API_KEY", "")
|
|
if not key:
|
|
return jsonify(ok=False, error="application API_KEY is not configured"), 503
|
|
if "X-API-Key" in request.headers:
|
|
supplied = request.headers.get("X-API-Key", "")
|
|
else:
|
|
authorization = request.authorization
|
|
supplied = authorization.password if authorization and authorization.type.lower() == "basic" else ""
|
|
if not hmac.compare_digest((supplied or "").encode("utf-8"), key.encode("utf-8")):
|
|
return jsonify(ok=False, error="unauthorized"), 401, {
|
|
"WWW-Authenticate": 'Basic realm="Contracts", charset="UTF-8"',
|
|
}
|
|
return None
|
|
|
|
@app.after_request
|
|
def no_cache(response):
|
|
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
|
|
response.headers["Pragma"] = "no-cache"
|
|
response.headers["Expires"] = "0"
|
|
return response
|
|
|
|
return app
|
|
|
|
|
|
def _init_db():
|
|
"""Создать БД + схему + seed prompts. SQLite — всё в одном файле /tmp."""
|
|
from db.connection import init_db
|
|
init_db()
|
|
try:
|
|
from db import prompts as db_prompts
|
|
db_prompts.seed_defaults()
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
app = create_app()
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="0.0.0.0", port=5000, threaded=True)
|