Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created Feb 3, 2021
Embed
What would you like to do?
import sys
import json
from wsgiref.simple_server import make_server
def make_app(handler):
def app(environ, start_response):
status = "200 OK"
headers = [("Content-type", "application/json; charset=utf-8")]
start_response(status, headers)
return [json.dumps(handler(environ)).encode("utf-8")]
return app
def run_app(app, port):
httpd = make_server("", port, app)
print(f"Serving on port {port}...", file=sys.stderr)
# Serve until process is killed
httpd.serve_forever()
def hello(name: str) -> str:
return f"hello {name}"
import foo
from foo.hello import hello
def main():
app = foo.make_app(lambda environ: {"message": hello("world")})
foo.run_app(app, 8000)
if __name__ == "__main__":
main()
run:
python -m main.py
build:
python -m zipapp -c -o app.pyz ../myzipapp -m "main:main"
clean:
rm -f app.pyz
find . -name __pycache__ -type d -exec rm -rf {} +
.PHONY: run build clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment