Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: FastAPI #13615

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
Open

POC: FastAPI #13615

wants to merge 13 commits into from

Conversation

iursevla
Copy link

@iursevla iursevla commented Sep 7, 2024

See: #13324 and #11915 for more information

FastAPI POC

This is a POC of using FastAPI to replace Flask in Frigate HTTP API. It is using the defaults provided by FastAPI (uvicorn and starlette). This was discussed here and previous PR was made.

I'm proposing that we start moving to FastAPI by mounting Flask as a sub-application sub-application + 2. This means that the entire application still works as is and we can then start moving endpoint by endpoint to FastAPI until we get to a point where all endpoints are converted. At that point, we can remove Flask altogether.

I've started the conversion of 2-3 endpoints to showcase how it works:

  • Preview endpoints:
    • /preview/{camera_name}/start/{start_ts}/end/{end_ts}
    • /preview/{camera_name}/start/{start_ts}/end/{end_ts}/frames
  • Media endpoints:
    • /{camera_name}/latest.{extension} -> See warning below
  • App endpoints:
    • / -> Health
    • /config/schema.json
  • Log endpoints:
    • /logs/{service}
Here it is working with FastAPI
converted_file.mp4

HTTP API Documentation

Details

Without any extra effort, the swagger documentation is available at http://localhost:5001/docs or http://localhost:5001/redoc See documentation

image image
converted_file.mp4
Screenshot 2024-08-24 at 16 05 59

The JSON/YAML schema can then be used to feed the HTTP API page without any human interaction.

From this documentation we can quickly create a client to be used by the front end. This can be done automatically with openapi-generator-cli. I've used this successfully multiple times at work and also for personal projects, e.g., Frigate HTTP API + 2.


Problems faced

Caution

I've faced a problem with the existing endpoints with an extension suffix: .(jpg|jpeg|png|webp|gif).As a result ⚠️ This lines in nginx.conf was commented out. I've left a comment explaining the errors. If someone wants/can help me figure out why it is failing, then use my branch to debug the endpoints that contain an extension.
I've already ported all media-related endpoints in this branch and also as a PR ready to target this current PR. If somebody wants to test the problem with the endpoint terminated with those extensions use the mentioned branch.

Copy link

netlify bot commented Sep 7, 2024

Deploy Preview for frigate-docs ready!

Name Link
🔨 Latest commit aa82e56
🔍 Latest deploy log https://app.netlify.com/sites/frigate-docs/deploys/66e6e3cfc8380400082882cd
😎 Deploy Preview https://deploy-preview-13615--frigate-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@iursevla iursevla changed the title POC: Added FastAPI POC: FastAPI Sep 7, 2024
@iursevla iursevla marked this pull request as ready for review September 7, 2024 20:37
Comment on lines +403 to +410
self.fastapi_app = create_fastapi_app(
self.config,
self.detected_frames_processor,
self.storage_maintainer,
self.onvif_controller,
self.plus_api,
self.stats_emitter,
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NickM-27 @hawkeye217 @blakeblackshear When you have some time please take a look. Please let me know what you think about this PR.

Copy link
Sponsor Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do, we are currently really busy with ffmpeg 7

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, @NickM-27, totally understandable. I hope you guys get it working 💯 .

I'll continue working on other endpoints in my free time. I already covered all media endpoints, and today I will convert notification and preview (at least).

So, in short:

  • POC -> this PR (let's call it B1)
  • media endpoints -> This branch (B2)
  • notification and preview -> on it (B3)

Basically dev <- B1 <- B2 <- B3 <- B.....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants