Skip to content

Instantly share code, notes, and snippets.

@href
Created November 11, 2014 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save href/d45c4c14c6e968fe027c to your computer and use it in GitHub Desktop.
Save href/d45c4c14c6e968fe027c to your computer and use it in GitHub Desktop.
Virtualhost in Morepath
import morepath
import re
from pprint import pprint
class App(morepath.App):
def request(self, environ):
request = super().request(environ)
pprint(sorted(request.headers.items()))
print(request.url)
return request
class Wiki(morepath.App):
def __init__(self, id):
self.id = id
@App.path('')
class AppRoot(object):
pass
def adjust_url(request, url):
prefix = request.headers.get('x-http-prefix', '/')
negative_prefix = request.headers.get('x-http-script-name', '/')
url = prefix + '/' + re.sub('^{}'.format(negative_prefix), '', url)
return url.replace('//', '/')
@App.view(model=AppRoot)
def get_root(self, request):
return adjust_url(request, request.link(self))
@Wiki.path('')
class WikiRoot(object):
pass
@Wiki.view(model=WikiRoot)
def get_wiki_root(self, request):
return adjust_url(request, request.link(self))
@App.mount(path='wiki/{id}', app=Wiki)
def get_wiki(id):
return Wiki(id)
if __name__ == '__main__':
config = morepath.setup()
config.scan()
config.commit()
morepath.run(App())
server {
server_name morepath.dev;
listen *:80;
location / {
proxy_pass http://127.0.0.1:5000/;
proxy_set_header X-Http-Prefix /;
proxy_set_header X-Http-Script-Name /;
}
location /alternative-root {
proxy_pass http://127.0.0.1:5000/;
proxy_set_header X-Http-Prefix /alternative-root;
proxy_set_header X-Http-Script-Name /;
}
location /alternative-wiki-root {
proxy_pass http://127.0.0.1:5000/wiki;
proxy_set_header X-Http-Prefix /alternative-wiki-root;
proxy_set_header X-Http-Script-Name /wiki;
}
}
server {
server_name ~^(?<subdomain>[a-z0-9-]+)\.morepath\.dev$;
listen *:80;
location / {
proxy_pass http://127.0.0.1:5000/wiki/$subdomain$uri;
proxy_set_header X-Http-Prefix /;
proxy_set_header X-Http-Script-Name /wiki/$subdomain;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment