Last active
October 15, 2019 10:24
-
-
Save FRII/6753045 to your computer and use it in GitHub Desktop.
Code for deploying Bottle apps to Apache mod_wsgi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/etc/httpd/sites-enabled/example.com.conf | |
<VirtualHost *:80> | |
ServerName example.com | |
ServerAlias www.example.com | |
ServerAdmin hostmaster@example.com | |
DocumentRoot /var/www/example.com/www | |
Alias /static /var/www/example.com/www | |
<Directory /var/www/example.com/www> | |
Order Allow,Deny | |
Allow from all | |
AllowOverride All | |
</Directory> | |
#tool a | |
WSGIDaemonProcess tool_a user=www-data group=www-data processes=2 threads=5 maximum-requests=1000 python-eggs=/tmp/python-eggs | |
WSGIScriptAlias /a /var/www/example.com/wsgi/a/application.wsgi | |
<Directory /var/www/example.com/wsgi/a> | |
WSGIProcessGroup tool_a | |
</Directory> | |
#tool b | |
WSGIDaemonProcess tool_b user=www-data group=www-data processes=2 threads=5 maximum-requests=1000 python-eggs=/tmp/python-eggs | |
WSGIScriptAlias /b /var/www/example.com/wsgi/b/application.wsgi | |
<Directory /var/www/example.com/wsgi/b> | |
WSGIProcessGroup tool_b | |
</Directory> | |
</VirtualHost> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#wsgi/a/index.py | |
import template | |
routes = [] | |
def index(): | |
return template.render("a/index") | |
routes.append(("/", "GET", index)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#wsgi/master_application.wsgi | |
import importlib | |
import bottle | |
import os, sys | |
os.chdir(os.path.dirname(__file__)) | |
sys.path = ["../../lib", "."] + sys.path | |
from sessionifier import sessionifier | |
from pathstripifier import pathstripifier | |
app = bottle.Bottle() | |
for filename in os.listdir("."): | |
if filename[0] != "_" and filename[-3:] == ".py": | |
module = importlib.import_module(filename[:-3]) | |
for path, method, func in module.routes: | |
app.route(path=path, method=method, callback=func) | |
application = pathstripifier(sessionifier(app)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lib/pathstripifier.py | |
class StripPathMiddleware(object): | |
def __init__(self, app): | |
self.app = app | |
def __call__(self, e, h): | |
e['PATH_INFO'] = e['PATH_INFO'].rstrip('/') | |
return self.app(e,h) | |
def pathstripifier(app): | |
return StripPathMiddleware(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lib/pathstripifier.py | |
from beaker.middleware import SessionMiddleware | |
session_opts = { | |
'session.type': 'file', | |
'session.cookie_expires': 600, | |
'session.data_dir': '/var/tmp', | |
'session.auto': True | |
} | |
def sessionifier(app): | |
return SessionMiddleware(app, session_opts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lib/template.py | |
from mako.template import Template | |
from mako.lookup import TemplateLookup | |
mylookup = TemplateLookup(directories="../../templates", module_directory="/var/tmp/mako_modules") | |
def render(name, **view): | |
return mylookup.get_template(name+".mako").render(**view) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment