Coder Social home page Coder Social logo

Comments (15)

sinisaos avatar sinisaos commented on May 22, 2024 1

@Goldziher I'm not home right now. Tomorrow I will make a github repo with whole Piccolo template and post the link here.

from litestar.

sinisaos avatar sinisaos commented on May 22, 2024 1

@Goldziher Here is a repo. Starlite changes are in app.py and endpoints.py and all other files are the same for all starter asgi templates. Take a look at the comments I left in the code. You can git clone repo better than fork because I will delete that repo.

from litestar.

Goldziher avatar Goldziher commented on May 22, 2024 1

Great, I'll check it in the evening.

from litestar.

sinisaos avatar sinisaos commented on May 22, 2024 1

I already added the admin function to route_handlers before and get an error.

I am getting an internal server error after i created the user as per the documentation served from 127.0.0.1:8000, but i am running the DB in docker etc. and have no clue if im using this correctly. I did attach a debugger to this stuff, but I am not sure I want to really delve into this code to figure out why the fast api based admin is not working

No problem. There doesn't seem to be an easy way to mount Piccolo Admin, but it doesn't matter, I just wanted to give it a try. Thanks for your time and help. I will close this issue. Cheers.

from litestar.

Goldziher avatar Goldziher commented on May 22, 2024 1

Sure thing. Note- for my money this is mounted, there is an error being returned from it, but the cause of the error cannot be ascertain without debugging the code internally. The issue is no longer with the mount - its something else. @sinisaos

from litestar.

Goldziher avatar Goldziher commented on May 22, 2024

Hi, have you tried using the asgi decorator?

from litestar.

sinisaos avatar sinisaos commented on May 22, 2024

@Goldziher Yes, I have already tried this without success, because the asgi decorator function must return the response (if response is not ok we get 500 error ImproperlyConfiguredException: Unable to serialize response content) not the asgi app.

from litestar.

Goldziher avatar Goldziher commented on May 22, 2024

can you show me some code? how did you use it?

from litestar.

Goldziher avatar Goldziher commented on May 22, 2024

Ok, so looking at the code, you currently have this:

admin_app = create_admin(
    tables=APP_CONFIG.table_classes,
    # Required when running under HTTPS:
    # allowed_hosts=['my_site.com']
)

@asgi(path="/admin")
async def admin(scope: Scope, receive: Receive, send: Send) -> None:
    if scope["type"] == "http":
        if scope["method"] == "GET":
            response = Response({"hello": "world"}, status_code=HTTP_200_OK) ????
            await response(scope=scope, receive=receive, send=send)
        return
    response = Response(
        {"detail": "unsupported request"}, status_code=HTTP_400_BAD_REQUEST
    )
    await response(scope=scope, receive=receive, send=send)

# middleware for Piccolo Admin
class AdminMiddleware(MiddlewareProtocol):
    def __init__(self, app: ASGIApp):
        self.app = admin_app

    async def __call__(
        self, scope: Scope, receive: Receive, send: Send
    ) -> None:
        if scope["type"] == "http":
            request = Request(scope)
        await self.app(scope, receive, send)

But I don't understand why you'd use the response app there - response is an ASGI app that returns http responses. What you want is something like this instead:

admin_app = create_admin(
    tables=APP_CONFIG.table_classes,
    # Required when running under HTTPS:
    # allowed_hosts=['my_site.com']
)

@asgi(path="/admin")
async def admin(scope: Scope, receive: Receive, send: Send) -> None:
    await admin_app(scope=scope, receive=receive, send=send)

I also don't see the point for the middleware - a middleware is basically a pipeline to either evaluate a request or modify it, what you want is to basically have an ASGI app mounted on a specific path.

from litestar.

sinisaos avatar sinisaos commented on May 22, 2024

@Goldziher I agree with you and that’s why I asked the question here because I dont know how to make Piccolo Admin to work with Starlite.

@asgi(path="/admin")
async def admin(scope: Scope, receive: Receive, send: Send) -> None:
    await admin_app(scope=scope, receive=receive, send=send)

This is not working (error 404 Not Found).

from litestar.

Goldziher avatar Goldziher commented on May 22, 2024

I see. Is piccolo exposing an asgi app?

from litestar.

sinisaos avatar sinisaos commented on May 22, 2024

Here is Piccolo Admin docs about asgi.
In Starlette or BlackSheep we can easily mount Piccolo Admin like this.

app = Starlette()

app.mount(
    "/admin/",
    create_admin(
        tables=APP_CONFIG.table_classes,
        # Required when running under HTTPS:
        # allowed_hosts=['my_site.com']
    ),
)

from litestar.

Goldziher avatar Goldziher commented on May 22, 2024

so, you get a 404 because you didn't add the admin function into the route handlers:

from piccolo.engine import engine_finder
from piccolo_admin.endpoints import create_admin
from starlette.types import Receive, Scope, Send

from home.endpoints import create_task, delete_task, home, tasks, update_task
from home.piccolo_app import APP_CONFIG
from starlite import (
    OpenAPIConfig,
    Starlite,
    StaticFilesConfig,
    TemplateConfig,
    asgi,
)
from starlite.template.jinja import JinjaTemplateEngine

# Piccolo Admin asgi app
admin_app = create_admin(
    tables=APP_CONFIG.table_classes,
    # Required when running under HTTPS:
    # allowed_hosts=['my_site.com']
)


# I don't know how to pass Piccolo Admin app to this route with asgi decorator
# because this function returns response and not asgi app and if response is not ok
# we get 500 error ImproperlyConfiguredException: Unable to serialize response content

@asgi(path="/admin")
async def admin(scope: Scope, receive: Receive, send: Send) -> None:
    admin_app(scope=scope, receive=receive, send=send)


async def open_database_connection_pool():
    try:
        engine = engine_finder()
        await engine.start_connection_pool()
    except Exception:
        print("Unable to connect to the database")


async def close_database_connection_pool():
    try:
        engine = engine_finder()
        await engine.close_connection_pool()
    except Exception:
        print("Unable to connect to the database")


app = Starlite(
    debug=True,
    route_handlers=[admin, home, tasks, create_task, update_task, delete_task],
    template_config=TemplateConfig(
        directory="home/templates", engine=JinjaTemplateEngine
    ),
    openapi_config=OpenAPIConfig(
        title="Starlite API",
        version="1.0.0",
    ),
    static_files_config=[
        StaticFilesConfig(directories=["static"], path="/static"),
    ],
    on_startup=[open_database_connection_pool],
    on_shutdown=[close_database_connection_pool],
)

from litestar.

Goldziher avatar Goldziher commented on May 22, 2024

I am getting an internal server error after i created the user as per the documentation served from 127.0.0.1:8000, but i am running the DB in docker etc. and have no clue if im using this correctly. I did attach a debugger to this stuff, but I am not sure I want to really delve into this code to figure out why the fast api based admin is not working πŸ˜‰

from litestar.

sinisaos avatar sinisaos commented on May 22, 2024

@Goldziher Nevermind. It was just my little experiment and trying out a new framework.

from litestar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.