aminalaee / sqladmin

SQLAlchemy Admin for FastAPI and Starlette
https://aminalaee.dev/sqladmin/
BSD 3-Clause "New" or "Revised" License
1.8k stars 181 forks source link

Adding root_path breaks admin page #538

Open mmzeynalli opened 1 year ago

mmzeynalli commented 1 year ago

Checklist

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 commented 1 year ago

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

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

aminalaee commented 1 year ago

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

admin.app["root_path"] = ...
mmzeynalli commented 1 year ago

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 commented 8 months ago

@mmzeynalli have you solved this problem?

mmzeynalli commented 8 months ago

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

tanguyMichardiere commented 3 months ago

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)
logicli0n commented 1 month ago
Screenshot 2024-07-12 at 12 45 45 AM

@aminalaee I set root_path="/api/v1" Admin page works in /api/v1/admin and normal. But when i use ajax_lookup in admin, ajax_lookup made request http://localhost:8070/admin/user/ajax/lookup?name=role&term=a without adding root_path and it give 404 Not Found

logicli0n commented 1 month ago

The problem is in here https://github.com/aminalaee/sqladmin/blob/main/sqladmin/application.py#L226? Can u add root_path before base_url? @aminalaee

aminalaee commented 1 month ago

@logicli0n Sure. Are you setting root path with admin.app.root_path ? Or on your main app?

logicli0n commented 1 month ago

@aminalaee I'm setting root path on my main app(in Fastapi).

logicli0n commented 1 month ago

Did u fix @aminalaee?