2023-10-19 00:06:14 +01:00
|
|
|
from sanic import Sanic, exceptions, response
|
|
|
|
|
|
|
|
app = Sanic("http_redirect")
|
|
|
|
|
|
|
|
# Send all HTTP users to HTTPS
|
|
|
|
@app.exception(exceptions.NotFound, exceptions.MethodNotSupported)
|
|
|
|
def redirect_everything_else(request, exception):
|
|
|
|
server, path = request.server_name, request.path
|
|
|
|
if server and path.startswith("/"):
|
|
|
|
return response.redirect(f"https://{server}{path}", status=308)
|
|
|
|
return response.text("Bad Request. Please use HTTPS!", status=400)
|
|
|
|
|
|
|
|
# ACME challenge for LetsEncrypt
|
2023-10-19 01:13:47 +01:00
|
|
|
@app.get("/.well-known/acme-challenge/<challenge>")
|
2023-10-19 00:06:14 +01:00
|
|
|
async def letsencrypt(request, challenge):
|
|
|
|
try:
|
|
|
|
return response.text(acme_challenges[challenge])
|
|
|
|
except KeyError:
|
|
|
|
return response.text(f"ACME challenge not found: {challenge}", status=404)
|
|
|
|
|
|
|
|
acme_challenges = {}
|