2021-03-10 09:19:38 +00:00
|
|
|
from sanic import Sanic, response
|
2017-01-11 02:07:58 +00:00
|
|
|
from sanic.blueprints import Blueprint
|
2017-01-08 23:48:12 +00:00
|
|
|
|
|
|
|
# Usage
|
|
|
|
# curl -H "Host: example.com" localhost:8000
|
|
|
|
# curl -H "Host: sub.example.com" localhost:8000
|
2017-01-11 02:07:58 +00:00
|
|
|
# curl -H "Host: bp.example.com" localhost:8000/question
|
|
|
|
# curl -H "Host: bp.example.com" localhost:8000/answer
|
2017-01-08 23:48:12 +00:00
|
|
|
|
2021-12-23 22:30:27 +00:00
|
|
|
app = Sanic("Example")
|
2017-01-11 02:07:58 +00:00
|
|
|
bp = Blueprint("bp", host="bp.example.com")
|
2017-01-08 23:48:12 +00:00
|
|
|
|
2017-06-01 19:53:05 +01:00
|
|
|
|
2021-03-10 09:19:38 +00:00
|
|
|
@app.route(
|
|
|
|
"/", host=["example.com", "somethingelse.com", "therestofyourdomains.com"]
|
|
|
|
)
|
|
|
|
async def hello_0(request):
|
2017-04-11 21:34:55 +01:00
|
|
|
return response.text("Some defaults")
|
2017-01-19 03:40:20 +00:00
|
|
|
|
2017-06-01 19:53:05 +01:00
|
|
|
|
2021-03-10 09:19:38 +00:00
|
|
|
@app.route("/", host="sub.example.com")
|
|
|
|
async def hello_1(request):
|
2017-04-11 21:34:55 +01:00
|
|
|
return response.text("42")
|
2017-01-08 23:48:12 +00:00
|
|
|
|
2017-06-01 19:53:05 +01:00
|
|
|
|
2017-01-11 02:07:58 +00:00
|
|
|
@bp.route("/question")
|
2021-03-10 09:19:38 +00:00
|
|
|
async def hello_2(request):
|
2017-04-11 21:34:55 +01:00
|
|
|
return response.text("What is the meaning of life?")
|
2017-01-11 02:07:58 +00:00
|
|
|
|
2017-06-01 19:53:05 +01:00
|
|
|
|
2017-01-11 02:07:58 +00:00
|
|
|
@bp.route("/answer")
|
2021-03-10 09:19:38 +00:00
|
|
|
async def hello_3(request):
|
2017-04-11 21:34:55 +01:00
|
|
|
return response.text("42")
|
2017-01-11 02:07:58 +00:00
|
|
|
|
2021-03-10 09:19:38 +00:00
|
|
|
|
|
|
|
@app.get("/name")
|
|
|
|
def name(request):
|
|
|
|
return response.text(request.app.url_for("name", _external=True))
|
|
|
|
|
|
|
|
|
2017-05-11 20:33:57 +01:00
|
|
|
app.blueprint(bp)
|
2017-01-11 02:07:58 +00:00
|
|
|
|
2021-03-10 09:19:38 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0", port=8000)
|