Skip to content

Refactor the server as a class that returns an object #120

@matweldon

Description

@matweldon

Currently, the server is a function that wraps up building a FastAPI instance and serving it using uvicorn.run().

But for operational use, we don't need to serve it, because the user can serve the app (for example in a dockerfile uvicorn cli command with worker settings etc)

The user might also need to add to the FastAPI instance before serving, for example adding HTTP headers, custom logs and authentication middleware.

Proposal

The server module builds the FastAPI object and returns it so that the user can run something like:

from server import ClassifAIServer

server = ClassifAIServer(...
                                      )

app = server.expose_app()
# The app is a FastAPI object
# You can then do uvicorn main:app

# Example, adding HTTP headers
@app.middleware("http")
def add_headers(...):
       pass


# Alternatively to call the python script directly
# Works same as start_api()
server.run()

Maybe it would be possible to subclass FastAPI then add .run() as a method directly. But maybe this is easier

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions