def test_get_yaml(project_path: Path):
    config = Config.from_yaml_file(file=project_path /
                                   "resources/application.yaml")

    assert config.security.admin_pwd == "admin"
    assert config.storage.workspaces["default"].path == Path(
        "examples/studies/")
    assert config.logging.level == "INFO"
Example #2
0
def flask_app(config_file: Path,
              resource_path: Optional[Path] = None) -> Flask:
    res = resource_path or get_local_path() / "resources"
    config = Config.from_yaml_file(res=res, file=config_file)

    configure_logger(config)
    # Database
    engine = create_engine(config.db_url, echo=config.debug)
    Base.metadata.create_all(engine)
    db_session = scoped_session(
        sessionmaker(autocommit=False, autoflush=False, bind=engine))

    application = Flask(__name__,
                        static_url_path="/static",
                        static_folder=str(res / "webapp"))
    application.wsgi_app = ReverseProxyMiddleware(
        application.wsgi_app)  # type: ignore

    application.config["SECRET_KEY"] = config.security.jwt_key
    application.config["JWT_ACCESS_TOKEN_EXPIRES"] = Auth.ACCESS_TOKEN_DURATION
    application.config[
        "JWT_REFRESH_TOKEN_EXPIRES"] = Auth.REFRESH_TOKEN_DURATION
    application.config["JWT_TOKEN_LOCATION"] = ["headers", "cookies"]

    @application.route("/", methods=["GET"])
    def home() -> Any:
        """
        Home ui
        ---
        responses:
            '200':
              content:
                 application/html: {}
              description: html home page
        tags:
          - UI
        """
        return render_template("index.html")

    @application.teardown_appcontext
    def shutdown_session(exception: Any = None) -> None:
        Auth.invalidate()
        db_session.remove()

    @application.errorhandler(HTTPException)
    def handle_exception(e: Any) -> Tuple[Any, Number]:
        """Return JSON instead of HTML for HTTP errors."""
        # start with the correct headers and status code from the error
        response = e.get_response()
        # replace the body with JSON
        response.data = json.dumps({
            "name": e.name,
            "description": e.description,
        })
        response.content_type = "application/json"
        return response, e.code

    event_bus = build_eventbus(application, config)
    user_service = build_login(application,
                               config,
                               db_session,
                               event_bus=event_bus)
    storage = build_storage(
        application,
        config,
        db_session,
        user_service=user_service,
        event_bus=event_bus,
    )
    build_launcher(
        application,
        config,
        db_session,
        service_storage=storage,
        event_bus=event_bus,
    )
    build_swagger(application)

    return application