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.

FastApi StaticFiles with mount not working

See original GitHub issue

Describe the bug Our team is struggling to get fastapi-versioning working in our environment since we are serving static assets for our frontend and docs. This seems to be an issue with mount when using StaticFiles.

To Reproduce Steps to reproduce the behavior:

  1. Create the file test.py
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi_versioning import VersionedFastAPI, version

app = FastAPI(title="My App")


@app.get("/")
@version(1, 0)
def greet_with_hello():
    return "Hello"


@app.get("/")
@version(1, 1)
def greet_with_hi():
    return "Hi"

app.mount("/", StaticFiles(directory="/"), name="static")


app = VersionedFastAPI(app)
  1. Run the server uvicorn test:app
  2. Error from running application
Traceback (most recent call last):
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/bin/uvicorn", line 8, in <module>
    sys.exit(main())
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/main.py", line 362, in main
    run(**kwargs)
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/main.py", line 386, in run
    server.run()
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/server.py", line 49, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "uvloop/loop.pyx", line 1494, in uvloop.loop.Loop.run_until_complete
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/server.py", line 56, in serve
    config.load()
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/config.py", line 308, in load
    self.loaded_app = import_from_string(self.app)
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/uvicorn/importer.py", line 20, in import_from_string
    module = importlib.import_module(module_str)
  File "/usr/local/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 790, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "./test.py", line 22, in <module>
    app = VersionedFastAPI(app)
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/fastapi_versioning/versioning.py", line 42, in VersionedFastAPI
    version_routes = [
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/fastapi_versioning/versioning.py", line 43, in <listcomp>
    version_to_route(route, default_version) for route in app.routes
  File "/Users/killswitch-gui/Library/Caches/pypoetry/virtualenvs/portal-pNH9Wcd1-py3.9/lib/python3.9/site-packages/fastapi_versioning/versioning.py", line 24, in version_to_route
    version = getattr(api_route.endpoint, "_api_version", default_version)
AttributeError: 'Mount' object has no attribute 'endpoint'

Expected behavior A versioned API without having to version mounts.

Additional context N/A

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
aminelemaizicommented, Mar 29, 2021

@killswitch-GUI , tbh I don’t know if it is an acceptable way of doing things. I didn’t see what does the code under the hood to confirm that.

2reactions
aminelemaizicommented, Mar 28, 2021

I’ve solved this issue by using the app.mount after the app = VersionedFastAPI(app) And if you need to access the root path / (for your landing page for example), define the @app.get and its function after the app = VersionedFastAPI(app) too.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Static Files - FastAPI
"Mounting" means adding a complete "independent" application in a specific path, that then takes care of handling all the sub-paths. This is different...
Read more >
FastAPI not loading static files - Stack Overflow
Here: app.mount("/static", StaticFiles(directory="../static"), name="static"). You mount your static directory under /static path.
Read more >
08 : Serving Static Files with FastAPI - FastapiTutorial
Basically, we are informing fastapi that we are going to keep all our static files in a folder named 'static' and whenever it...
Read more >
tiangolo/fastapi - Gitter
app = FastAPI() app.mount("/statics", StaticFiles(directory="statics", packages=["bootstrap4"])) ... But for some reason I really can't make it work ;_;.
Read more >
FastAPI # 0029 # Serving Static Files with FastAPI - YouTube
https://github.com/Sumanshu-Nankana/ FastAPI /blob/main/learn/main.py.
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