Using a custom resource

29 views
Skip to first unread message

Daniel Faust

unread,
Sep 12, 2014, 3:49:29 PM9/12/14
to cross...@googlegroups.com

I think I understood that I can have crossbar serve HTTP content via the transports.paths configuration, where in the hello demo path "/" is type static serving directory ../hello/web and "/ws" is the websocket resource.

How can I add my own resource to let's say "/api/v1", one where I can define a def render(self, request): method?

Bonus question: how can this resource call methods in hello.py?

Daniel Faust

unread,
Sep 12, 2014, 3:53:06 PM9/12/14
to cross...@googlegroups.com

I basically want to be able to access hello.py functionality via a HTTP resource, so that WAMP clients can interact with it as well as some tools which just speak HTTP and don't require the bidirectionality of websockets.

Daniel Faust

unread,
Sep 12, 2014, 4:13:28 PM9/12/14
to cross...@googlegroups.com
Ok, so I've added a

                  "myapp": {
                     
"type": "wsgi",
                     
"module": "myapp",
                     
"object": "theapp"
                 
}

to the config, but I want to use Twisted and not Django, etc.  So I tried creating myapp.py with

from twisted.web.wsgi import WSGIResource
from twisted.internet import reactor

def application(environ, start_response):
    start_response
('200 OK', [('Content-type', 'text/plain')])
   
return ['Hello, world!']

theapp
= WSGIResource(reactor, reactor.getThreadPool(), application)

but this doesn't seem to work. Then there's teh issue of how to get that module to communicate with the hello.py backend... ayayay




On Friday, September 12, 2014 9:49:29 PM UTC+2, Daniel Faust wrote:

Daniel Faust

unread,
Sep 12, 2014, 4:35:35 PM9/12/14
to cross...@googlegroups.com
ok

class WSGI():
 
def __call__(self, environ, start_response):

    start_response
('200 OK', [('Content-type', 'text/plain')])

   
print 'Hello, world!'
   
return ['Hello, world!']

theapp
= WSGI()

does work but how can I access the backend defined in hellp.py in order to have this WSGI object to be a configuration interface for some hello.py properties, or to inspect the hello.py variables.

Tobias Oberstein

unread,
Sep 13, 2014, 5:14:15 AM9/13/14
to cross...@googlegroups.com
Am 12.09.2014 21:49, schrieb Daniel Faust:
>
> I think I understood that I can have crossbar serve HTTP content via the
> transports.paths configuration, where in the hello demo path "/" is type
> static serving directory ../hello/web and "/ws" is the websocket resource.
>
> How can I add my own resource to let's say "/api/v1", one where I can
> define a def render(self, request): method?

I guess you are speaking of a general Twisted Web resource hooking into
a HTTP path?

This is not supported. A WAMP application should have it's backend logic
in WAMP components.

It's already a convenience that Crossbar can also serve static Web
content. The reason that Crossbar also supports WSGI is already
problematic from a design point of view. It is convenient since it
allows you to use Jinja templating (e.g. via Flask).

>
> Bonus question: how can this resource call methods in hello.py?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Crossbar" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to crossbario+...@googlegroups.com
> <mailto:crossbario+...@googlegroups.com>.
> To post to this group, send email to cross...@googlegroups.com
> <mailto:cross...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/crossbario/9df119f2-7d65-42a7-85f0-70ddbe833699%40googlegroups.com
> <https://groups.google.com/d/msgid/crossbario/9df119f2-7d65-42a7-85f0-70ddbe833699%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Daniel Faust

unread,
Sep 13, 2014, 7:12:28 AM9/13/14
to cross...@googlegroups.com
Hi Tobias,

yes, as a general twisted resource. It would be just as convenient as a wsgi resource. But I understand why one would want to avoid this. Specially if the resource should also be able to talk directly (in memory) to other components, as this can easily end up in becoming spaghetti code if care is not taken.


On Saturday, September 13, 2014 11:14:15 AM UTC+2, Tobias Oberstein wrote:
Am 12.09.2014 21:49, schrieb Daniel Faust:
>
> I think I understood that I can have crossbar serve HTTP content via the
> transports.paths configuration, where in the hello demo path "/" is type
> static serving directory ../hello/web and "/ws" is the websocket resource.
>
> How can I add my own resource to let's say "/api/v1", one where I can
> define a def render(self, request): method?

I guess you are speaking of a general Twisted Web resource hooking into
a HTTP path?

This is not supported. A WAMP application should have it's backend logic
in WAMP components.

It's already a convenience that Crossbar can also serve static Web
content. The reason that Crossbar also supports WSGI is already
problematic from a design point of view. It is convenient since it
allows you to use Jinja templating (e.g. via Flask).

>
> Bonus question: how can this resource call methods in hello.py?
>
> --
> You received this message because you are subscribed to the Google
> Groups "Crossbar" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to crossbario+...@googlegroups.com

Tobias Oberstein

unread,
Sep 14, 2014, 4:52:39 AM9/14/14
to cross...@googlegroups.com
Am 13.09.2014 13:12, schrieb Daniel Faust:
> Hi Tobias,
>
> yes, as a general twisted resource. It would be just as convenient as a
> wsgi resource. But I understand why one would want to avoid this.

Technically, it would be trivial to add another path service to Crossbar
that serves a general Twisted Web resource. However, to make that
practically useful, the resource would probably then want to talk WAMP /
wire up to a WAMP component. And this is exactly what is discourage
because of the spaghetti this will lead to (below).

Crossbar is a WAMP router. In addition, it can also serve static Web
files (and simple Jinja templating based Web pages). It is _not_ a
general Web framework for dynamic Web applications. This is the old
world, and there are plenty of frameworks like Django, Flask etc

> Specially if the resource should also be able to talk directly (in
> memory) to other components, as this can easily end up in becoming
> spaghetti code if care is not taken.

Exactly. I'd consider that bad design.

Nevertheless I'd be interested in learning what you do in your Twisted
Web resource that you cannot migrate out to a regular WAMP component.
> > an email to crossbario+...@googlegroups.com <javascript:>
> > <mailto:crossbario+...@googlegroups.com <javascript:>>.
> > To post to this group, send email to cross...@googlegroups.com
> <javascript:>
> > <mailto:cross...@googlegroups.com <javascript:>>.
> <https://groups.google.com/d/msgid/crossbario/9df119f2-7d65-42a7-85f0-70ddbe833699%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Crossbar" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to crossbario+...@googlegroups.com
> <mailto:crossbario+...@googlegroups.com>.
> To post to this group, send email to cross...@googlegroups.com
> <mailto:cross...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/crossbario/68f90801-adfa-4810-97d6-e905f92a6838%40googlegroups.com
> <https://groups.google.com/d/msgid/crossbario/68f90801-adfa-4810-97d6-e905f92a6838%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages