This kind of problem crops up occasionally as we discover the different
ways the community want to use nameko. There is a similar issue passing
message delivery options into the RPC entrypoint.
Our general suggestion for extending nameko is to subclass the built-in
classes. At Student.com for example, our HTTP entrypoint looks like this:
import json
from nameko.web.handlers import HttpRequestHandler
from nameko.web.server import WebServer as BaseWebServer
from werkzeug.wrappers import Response
class WebServer(BaseWebServer):
def context_data_from_headers(self, request):
# specify the mapping between HTTP headers and context data
context_data = super().context_data_from_headers(request)
...
return context_data
class HttpEntrypoint(HttpRequestHandler):
server = WebServer()
def response_from_exception(self, exc):
# generate a custom response object from the thrown exception
...
return Response(
json.dumps(payload),
status=status_code,
mimetype='application/json',
)
http = HttpEntrypoint.decorator
These subclasses are light because their parent classes expect to be
extended and are structured appropriately.
Unfortunately the WsgiApp is not exposed in such a helpful way, but it'd be
great if it was!
We could perhaps add a get_wsgi_app method to the WebServer (and maybe a
get_wsgi_server method, while we're at it). These methods could then be
overridden in a subclass and the fixers and other customisations provided
as the user saw fit.
A pull request along those lines would be very welcome. In the mean time,
the sledgehammer approach would be to subclass and override the whole start
method.
Matt.
···
On Wednesday, August 17, 2016 at 8:06:10 AM UTC+8, je...@pollak.io wrote:
We're using the *@http* support of Nameko to write simple APIs and are
serving those APIs over HTTPS, with them sitting behind an nginx instance
that terminates SSL. This presents a problem for us because we do various
validation checks on *request.base_url* which shows up as HTTP (because
the request is proxied through as HTTPS) even though the original request
was HTTPS.
In Flask, we've solved this same issue using *werkzeug.fixers.ProxyFix, *a
WSGI middleware that updates the WSGI environ to be in line with what it
should have been before the request was proxied.
I'm trying to apply this same fix to our nameko instances, but am having a
really hard time because the WSGI app instantiation and use is buried
deep in the Webserver
<https://github.com/onefinestay/nameko/blob/7209c1d45fce20cb2ba7ddcd6f79fa120c85bc29/nameko/web/server.py#L82-L86>
.
Has anyone else dealt with this or have any suggestions on how to proceed?
Thanks!
Jesse