Skip to content

Instantly share code, notes, and snippets.

@tswicegood
Created July 25, 2014 21:06
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 tswicegood/a9fe9fd511ba1a106f8f to your computer and use it in GitHub Desktop.
Save tswicegood/a9fe9fd511ba1a106f8f to your computer and use it in GitHub Desktop.
from werkzeug import wrappers
def application(request):
request > get("/") > do_response()
request > get("/msg") > say_hello()
# # WARNING
#
# Don't judge me. :-)
#
# Seriously, the code below this is just proof of concept code that
# enables the application function to actually run.
class HttpResponse(BaseException):
pass
class Http405(HttpResponse):
def do(self, environ, start_response):
return wrappers.Response(status=405)(environ, start_response)
class Http404(HttpResponse):
def do(self, environ, start_response):
return wrappers.Response(status=404)(environ, start_response)
def route(path, methods="GET"):
if not type(methods) is list:
methods = [methods, ]
class Route(object):
def __lt__(self, request):
if not request.path == path:
return False
if not request.method in methods:
raise Http405
request.route = self
self.request = request
return True
return Route()
def get(path):
return route(path, methods=["GET"])
class Response(HttpResponse):
def __lt__(self, route):
self.request = route.request
raise self
def do(self, environ, start_response):
return wrappers.Response("Hello!\n", mimetype="text/html")(environ, start_response)
def do_response():
return Response()
def say_hello():
class HelloResponder(Response):
def do(self, environ, start_response):
who = self.request.args.get('who', 'World')
return wrappers.Response(
"Hello, %s!\n" % who,
mimetype="text/html"
)(environ, start_response)
return HelloResponder()
if __name__ == '__main__':
from werkzeug.serving import run_simple
def app(environ, start_response):
request = wrappers.Request(environ)
try:
application(request)
except HttpResponse as response:
return response.do(environ, start_response)
run_simple("127.0.0.1", 1981, app, use_reloader=True)
@tswicegood
Copy link
Author

This is in a better form here https://github.com/tswicegood/steinie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment