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

[QUESTION] is it possible / easy to add a klein application to an existing twisted.application.service.Application? #753

Closed
michelcrypt4d4mus opened this issue May 26, 2024 · 4 comments

Comments

@michelcrypt4d4mus
Copy link

Apologies if this is the wrong place to ask this but is it possible / easy to add a klein application to an existing Twisted app launched via a .tac file? I found this part of the docs which explain how to implement a klein app and launch it via twistd -n web so it seems like it should be possible to do what i describe - create a klein resource and add it to the Application via setServiceParent() - but I'm unclear as to how to proceed given that the documentation example assumes that klein is the entire application whereas my use case requires me to manage other resources listening on other ports.

thanks.

@michelcrypt4d4mus
Copy link
Author

managed to work this one out far more easily than i expected... for future searchers:

klein_server.py

from klein import resource, route
from twisted.application import internet
from twisted.web.server import Site


@route('/')
def hello(request):
    return "Hello, world!"


def getService():
    return internet.TCPServer(7757, Site(resource()), interface='')

app.tac

from klein_server import get_service

application = service.Application(f"illmaticized")
getService().setServiceParent(application)

Run with twistd -noy app.tac.

@glyph
Copy link
Member

glyph commented May 26, 2024

Awesome, glad you were able to quickly figure this out. Is there still a doc issue here though, where it could have been made clearer, so you wouldn't have gotten stuck in the first place?

@michelcrypt4d4mus
Copy link
Author

michelcrypt4d4mus commented May 27, 2024

it definitely wasn't super clear. i was able to figure it out by looking at the run() method and seeing how the Site object wraps a call to resource() much the same as some twisted code i was already working with.

didn't seem well documented (or maybe I missed it) that you can launch a standalone klein server outside of the twistd CLI tool or the klein runner by calling internet.TCPServer(7757, Site(resource()), interface='').

maybe a line in the page of the docs i linked pointing that out might prove fruitful for someone else in the future.

@glyph
Copy link
Member

glyph commented Jun 4, 2024

@michelcrypt4d4mus I filed #757 to try to add something to the documentation so others don't experience this as badly.

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

2 participants