2021-09-27 12:03:35 +02:00
|
|
|
from flask import Flask, jsonify
|
2021-10-22 10:37:42 +02:00
|
|
|
from flask_cors import CORS
|
2022-04-05 12:11:38 +02:00
|
|
|
from flask_jwt_extended import JWTManager
|
|
|
|
from flask_migrate import Migrate
|
2021-11-02 08:54:07 +01:00
|
|
|
from jsonschema.exceptions import ValidationError
|
|
|
|
from werkzeug.exceptions import BadRequest
|
2022-03-22 08:56:36 +01:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2021-09-27 12:03:35 +02:00
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
# These imports are required
|
2021-10-28 16:09:10 +02:00
|
|
|
from areas import api_v1
|
2022-03-21 08:02:29 +01:00
|
|
|
from areas import web
|
2022-04-01 09:15:30 +02:00
|
|
|
from areas import cli
|
2022-03-21 08:02:29 +01:00
|
|
|
|
2021-10-28 16:09:10 +02:00
|
|
|
from areas import users
|
|
|
|
from areas import apps
|
|
|
|
from areas import auth
|
2022-03-21 08:02:29 +01:00
|
|
|
from areas import login
|
2022-04-01 09:15:30 +02:00
|
|
|
from areas import cliapp
|
2022-03-21 08:02:29 +01:00
|
|
|
|
2022-03-22 08:56:36 +01:00
|
|
|
from database import db
|
2021-11-02 08:54:07 +01:00
|
|
|
|
|
|
|
from helpers import (
|
|
|
|
BadRequest,
|
|
|
|
KratosError,
|
2022-01-18 10:48:18 +01:00
|
|
|
HydraError,
|
2021-11-02 08:54:07 +01:00
|
|
|
bad_request_error,
|
|
|
|
validation_error,
|
|
|
|
kratos_error,
|
|
|
|
global_error,
|
2022-01-18 10:48:18 +01:00
|
|
|
hydra_error,
|
2022-03-22 08:56:36 +01:00
|
|
|
KratosUser,
|
|
|
|
App,
|
|
|
|
AppRole
|
2021-11-02 08:54:07 +01:00
|
|
|
)
|
2022-03-22 08:56:36 +01:00
|
|
|
|
2021-09-27 12:03:35 +02:00
|
|
|
from config import *
|
2022-03-22 07:16:53 +01:00
|
|
|
import logging
|
2021-09-27 12:03:35 +02:00
|
|
|
|
2022-03-22 10:35:15 +01:00
|
|
|
app = Flask(__name__, static_url_path = '/web/static')
|
2022-03-22 08:56:36 +01:00
|
|
|
|
2021-09-27 12:03:35 +02:00
|
|
|
cors = CORS(app)
|
2022-03-22 08:56:36 +01:00
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
app.config["SECRET_KEY"] = SECRET_KEY
|
2022-03-22 08:56:36 +01:00
|
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
|
|
|
|
|
|
|
|
## from database import db
|
|
|
|
#db = SQLAlchemy()
|
|
|
|
db.init_app(app)
|
|
|
|
|
2022-04-05 12:11:38 +02:00
|
|
|
Migrate(app, db)
|
2022-03-22 07:16:53 +01:00
|
|
|
|
2022-04-05 12:11:38 +02:00
|
|
|
app.logger.setLevel(logging.INFO)
|
2022-03-22 07:16:53 +01:00
|
|
|
|
2021-09-27 12:03:35 +02:00
|
|
|
app.register_blueprint(api_v1)
|
2022-03-21 08:02:29 +01:00
|
|
|
app.register_blueprint(web)
|
2022-04-01 09:15:30 +02:00
|
|
|
app.register_blueprint(cli)
|
2021-09-27 12:03:35 +02:00
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
# Error handlers
|
|
|
|
app.register_error_handler(Exception, global_error)
|
|
|
|
app.register_error_handler(BadRequest, bad_request_error)
|
|
|
|
app.register_error_handler(ValidationError, validation_error)
|
|
|
|
app.register_error_handler(KratosError, kratos_error)
|
2022-01-18 10:48:18 +01:00
|
|
|
app.register_error_handler(HydraError, hydra_error)
|
2021-11-02 08:54:07 +01:00
|
|
|
|
2021-09-27 12:03:35 +02:00
|
|
|
jwt = JWTManager(app)
|
|
|
|
|
|
|
|
# When token is not valid or missing handler
|
|
|
|
@jwt.invalid_token_loader
|
|
|
|
@jwt.unauthorized_loader
|
|
|
|
@jwt.expired_token_loader
|
|
|
|
def expired_token_callback(*args):
|
2021-11-02 08:54:07 +01:00
|
|
|
return jsonify({"errorMessage": "Unauthorized"}), 401
|
2021-09-27 12:03:35 +02:00
|
|
|
|
|
|
|
|
2021-11-02 08:54:07 +01:00
|
|
|
@app.route("/")
|
2021-09-27 12:03:35 +02:00
|
|
|
def index():
|
2021-11-02 08:54:07 +01:00
|
|
|
return "Open App Stack API v1.0"
|