question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How can I use Gunicorns SCRIPT_NAME with FastAPI?

See original GitHub issue

First check

  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.

Description

Is it possible to allow routes to be prefixed using Gunicorn’s support for the SCRIPT_NAME env variable? I read the discussion in #461 but could not find any other working solution than manually prepending the prefixed url to each route.

Additional context

What I would like to do is basically have a route prefix by setting: os.environ["SCRIPT_NAME"] = "/a/b/c/d/e" and then allowing Gunicorn to route for example

@app.get("/hello")
def read_root():
    return {"Hello": "World"}

to /a/b/c/d/e/hello.

I tried running the Uvicorn + Gunicorn combination using gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker to achieve this. It will simply ignore the prefix and keep on routing to /hello instead.

Gunicorn will do this out of the box if the env var is present when serving a Flask or Django app.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
Midnightercommented, Jun 17, 2020

As shown in my proof-of-concept, you still have to subclass the uvicorn worker similar to the following:

from starlette.config import Config
from uvicorn.workers import UvicornWorker


config = Config()


class ConfigurableWorker(UvicornWorker):
    """
    Define a UvicornWorker that can be configured by modifying its class attribute.
    All of the command line options for uvicorn are potential configuration options
    (see https://www.uvicorn.org/settings/ for the complete list).
    """

    #: dict: Set the equivalent of uvicorn command line options as keys.
    CONFIG_KWARGS = {
        "root_path": config("SCRIPT_NAME", default=""),
        "proxy_headers": True,
    }
1reaction
tiangolocommented, Jun 17, 2020

@Midnighter I think that’s worth a PR to Uvicorn ✔️

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server Workers - Gunicorn with Uvicorn - FastAPI
Here I'll show you how to use Gunicorn with Uvicorn worker processes. ... used by Uvicorn, main means the Python module named "...
Read more >
In Python + FastAPI, how to access a Gunicorn served website ...
I am not using virtual env. Now I ssh into the VM and cd into the project root folder where the main.py is...
Read more >
tiangolo/fastapi - Gitter
Another question, I'm deploying my apps with gunicorn which in principle should allow me to mount the main app using the environment variable...
Read more >
FastAPI Tutorial - Building RESTful APIs with Python - YouTube
In this Python tutorial you will learn about FastAPI, a Web framework for developing RESTful APIs in Python. FastAPI is based on Pydantic ......
Read more >
tiangolo/uvicorn-gunicorn-fastapi - Docker Image
You can use Gunicorn to start and manage multiple Uvicorn worker processes. That way, you get the best of concurrency and parallelism in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found