Last active
January 20, 2022 08:21
-
-
Save lwzm/613cb1d1e8443a96a2efe04ee456046d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python3 | |
from fastapi import FastAPI, Request | |
app = FastAPI() | |
@app.get("/x") | |
async def test(req: Request): | |
return str(req.base_url) | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", forwarded_allow_ips="*") | |
# equivalent to: | |
# $ uvicorn --forwarded-allow-ips '*' --host 0.0.0.0 asgi:app | |
# $ FORWARDED_ALLOW_IPS=* uvicorn --host 0.0.0.0 asgi:app |
This file contains hidden or 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
#!/usr/bin/env python3 | |
from flask import Flask, redirect, request | |
app = Flask(__name__) | |
# https://werkzeug.palletsprojects.com/en/latest/middleware/proxy_fix/ | |
# https://flask.palletsprojects.com/en/2.0.x/quickstart/#hooking-in-wsgi-middleware | |
from werkzeug.middleware.proxy_fix import ProxyFix | |
app.wsgi_app = ProxyFix(app.wsgi_app) | |
@app.route("/") | |
def root(): | |
return redirect("/hello") | |
@app.route("/hello") | |
def hello(): | |
return "hello world: " + request.url | |
if __name__ == "__main__": | |
# ./wsgi.py | |
if __debug__: | |
app.run(host="0.0.0.0", port=8000, debug=True) # equivalent to: $ FLASK_DEBUG=1 flask run -h '' -p 8000 | |
# PYTHONOPTIMIZE=1 ./wsgi.py | |
import bjoern | |
bjoern.run(app.wsgi_app, "0.0.0.0", 80) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment