78 lines
1.7 KiB
Python
78 lines
1.7 KiB
Python
from flask import Flask, jsonify
|
|
from flask_jwt_extended import JWTManager
|
|
from flask_cors import CORS
|
|
from jsonschema.exceptions import ValidationError
|
|
from werkzeug.exceptions import BadRequest
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
# These imports are required
|
|
from areas import api_v1
|
|
from areas import web
|
|
from areas import cli
|
|
|
|
from areas import users
|
|
from areas import apps
|
|
from areas import auth
|
|
from areas import login
|
|
from areas import cliapp
|
|
|
|
from database import db
|
|
|
|
from helpers import (
|
|
BadRequest,
|
|
KratosError,
|
|
HydraError,
|
|
bad_request_error,
|
|
validation_error,
|
|
kratos_error,
|
|
global_error,
|
|
hydra_error,
|
|
KratosUser,
|
|
App,
|
|
AppRole
|
|
)
|
|
|
|
from config import *
|
|
import logging
|
|
|
|
app = Flask(__name__, static_url_path = '/web/static')
|
|
|
|
cors = CORS(app)
|
|
|
|
app.config["SECRET_KEY"] = SECRET_KEY
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
|
|
|
|
## from database import db
|
|
#db = SQLAlchemy()
|
|
db.init_app(app)
|
|
|
|
# Late beceuse of circular import
|
|
##
|
|
|
|
|
|
app.logger.setLevel(logging.INFO)
|
|
|
|
app.register_blueprint(api_v1)
|
|
app.register_blueprint(web)
|
|
app.register_blueprint(cli)
|
|
|
|
# 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)
|
|
app.register_error_handler(HydraError, hydra_error)
|
|
|
|
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):
|
|
return jsonify({"errorMessage": "Unauthorized"}), 401
|
|
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return "Open App Stack API v1.0"
|