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

Adding root_path breaks admin page #538

Open
2 tasks done
mmzeynalli opened this issue Jul 4, 2023 · 9 comments
Open
2 tasks done

Adding root_path breaks admin page #538

mmzeynalli opened this issue Jul 4, 2023 · 9 comments

Comments

@mmzeynalli
Copy link

Checklist

  • The bug is reproducible against the latest release or master.
  • There are no similar issues or pull requests to fix it yet.

Describe the bug

I have multiple endpoints and admin panel. I want my endpoints to be in root path of /api/v1/, but admin to be just /admin/. So, I added app_configs["root_path"] = f"/api/v{settings.APP_VERSION}", and admin panel breaks down.

Steps to reproduce the bug

app_configs: dict[str, Any] = {"title": "Itinerago API"}
app_configs["root_path"] = f"/api/v{settings.APP_VERSION}"

app = FastAPI(**app_configs)
app.include_router(auth_router, prefix="/auth", tags=["Auth"])
app.include_router(core_router, prefix="", tags=["Main Logic"])


admin = Admin(app, engine)

# admin.add_view-s here

Commenting out second line fixes admin, however, it does not give desired output.

Expected behavior

APIs will have /api/v1/ prefix, but, admin will not. Admin will load without a problem.

Actual behavior

Admin breaks down completely:

image

and

image

Links are also broken:
In the first image, when you click one of the models it goes to: /api/v1/admin/model/list

image

Debugging material

No response

Environment

Windows, Python 3.10, Dockerized Fastapi
sqladmin version 0.12.0

Additional context

No response

@aminalaee
Copy link
Owner

Hey,
not sure if I get your question right.
I can think of two points:

  • Use can use Admin(base_url=...) to configure the URL.
  • In your app you don't se the root_path in path, instead that's the prefix in your routers.

Generally I think this makes sense since you want to handle multiple API versionings in your app, running at the same time so setting the version on the whole app might not be a good idea.

@mmzeynalli
Copy link
Author

Default base_url suits me, however, whenever set any root_path, admin gets messy like I showed in screenshots. You second point would work, but, it is hassle to add api/v1/ to every app's router. I think admin should be independent of root_path and have its url.

@aminalaee
Copy link
Owner

Anyway if you want to do that you can just modify admin.app which is just another FastAPI app so you can do any config you want.

@mmzeynalli
Copy link
Author

How? Just adding root_path breaks admin panel. Is it expected?

@aminalaee
Copy link
Owner

I haven't tested this before, doesn't setting this work?

admin.app["root_path"] = ...

@mmzeynalli
Copy link
Author

Just checked, no it does not work. Change the line to:
admin.app.root_path = "", but as app passed to admin is the global fastapi app, I am overriding global root_path => All endpoints are without /api/v1/

@VladBein
Copy link

@mmzeynalli have you solved this problem?

@mmzeynalli
Copy link
Author

To be honest no, unfortunately, have been super busy for the last couple of months.

@tanguyMichardiere
Copy link

A workaround I found is to use a middleware:

@app.middleware("http")
async def fix_admin_root_path(request, call_next):
    if request.url.path.startswith("/admin/"):
        request.scope["path"] = app.root_path + request.url.path
    return await call_next(request)

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

No branches or pull requests

4 participants