-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Chainlit * Sort imports and remove tests * Formatting and linting
- Loading branch information
1 parent
ce2baee
commit b374792
Showing
13 changed files
with
910 additions
and
634 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,6 @@ coverage.* | |
|
||
# Poetry installer local error logs | ||
poetry-installer-error-*.log | ||
|
||
# Chainlit | ||
.chainlit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,13 @@ | ||
import logging | ||
import os | ||
from typing import Optional | ||
|
||
from apiflask import APIFlask | ||
from flask import g | ||
from werkzeug.exceptions import Unauthorized | ||
|
||
import src.adapters.db as db | ||
import src.adapters.db.flask_db as flask_db | ||
import src.logging | ||
import src.logging.flask_logger as flask_logger | ||
from src.api.healthcheck import healthcheck_blueprint | ||
from src.api.schemas import response_schema | ||
from src.api.users import user_blueprint | ||
from src.auth.api_key_auth import User, get_app_security_scheme | ||
import chainlit as cl | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def create_app() -> APIFlask: | ||
app = APIFlask(__name__) | ||
|
||
src.logging.init(__package__) | ||
flask_logger.init_app(logging.root, app) | ||
|
||
db_client = db.PostgresDBClient() | ||
flask_db.register_db_client(db_client, app) | ||
|
||
configure_app(app) | ||
register_blueprints(app) | ||
register_index(app) | ||
|
||
return app | ||
|
||
|
||
def current_user(is_user_expected: bool = True) -> Optional[User]: | ||
current = g.get("current_user") | ||
if is_user_expected and current is None: | ||
logger.error("No current user found for request") | ||
raise Unauthorized | ||
return current | ||
|
||
|
||
def configure_app(app: APIFlask) -> None: | ||
# Modify the response schema to instead use the format of our ApiResponse class | ||
# which adds additional details to the object. | ||
# https://apiflask.com/schema/#base-response-schema-customization | ||
app.config["BASE_RESPONSE_SCHEMA"] = response_schema.ResponseSchema | ||
|
||
# Set a few values for the Swagger endpoint | ||
app.config["OPENAPI_VERSION"] = "3.0.3" | ||
|
||
# Set various general OpenAPI config values | ||
app.info = { | ||
"title": "Template Application Flask", | ||
"description": "Template API for a Flask Application", | ||
"contact": { | ||
"name": "Nava PBC Engineering", | ||
"url": "https://www.navapbc.com", | ||
"email": "[email protected]", | ||
}, | ||
} | ||
|
||
# Set the security schema and define the header param | ||
# where we expect the API token to reside. | ||
# See: https://apiflask.com/authentication/#use-external-authentication-library | ||
app.security_schemes = get_app_security_scheme() | ||
|
||
|
||
def register_blueprints(app: APIFlask) -> None: | ||
app.register_blueprint(healthcheck_blueprint) | ||
app.register_blueprint(user_blueprint) | ||
|
||
|
||
def get_project_root_dir() -> str: | ||
return os.path.join(os.path.dirname(__file__), "..") | ||
|
||
|
||
def register_index(app: APIFlask) -> None: | ||
@app.route("/") | ||
@app.doc(hide=True) | ||
def index() -> str: | ||
return """ | ||
<!Doctype html> | ||
<html> | ||
<head><title>Home</title></head> | ||
<body> | ||
<h1>Home</h1> | ||
<p>Visit <a href="/docs">/docs</a> to view the api documentation for this project.</p> | ||
</body> | ||
</html> | ||
""" | ||
@cl.on_message | ||
async def main(message: cl.Message) -> None: | ||
logger.info(f"Received: {message.content}") | ||
await cl.Message( | ||
content=f"Hello, world! Received: {message.content}", | ||
).send() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.