Skip to content

Instantly share code, notes, and snippets.

@upvalue
Created January 20, 2019 01:37
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 upvalue/823330b00c35dcce8d0103de9b996104 to your computer and use it in GitHub Desktop.
Save upvalue/823330b00c35dcce8d0103de9b996104 to your computer and use it in GitHub Desktop.
Routing for GCF.
# hack together router with werkzeug because google cloud functions does not
# support full flask apps
from werkzeug.routing import Map, Rule, NotFound
from werkzeug.exceptions import MethodNotAllowed, HTTPException
url_map = []
def route(path, methods=("GET",)):
def decorator(fn):
def endpoint(req):
if methods:
if req.method not in methods:
raise MethodNotAllowed(list(methods))
return fn(req)
url_map.append(Rule(path, endpoint=endpoint))
return decorator
@route("/", ("GET",))
def helloWorld(req):
return "hello world"
url_map = Map(url_map)
import logging
def entry(request):
logging.info(f"{request.full_path}")
logging.info(f"{request.path}")
urls = url_map.bind("localhost", path_info=request.path)
try:
endpoint, args = urls.match()
return endpoint(request)
except HTTPException as e:
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment