-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (37 loc) · 1.05 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
from urllib.parse import urlparse
from fastapi import FastAPI, Request
from fastapi.responses import (
RedirectResponse,
Response,
)
from fastapi.middleware.cors import CORSMiddleware
import members
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.middleware("http")
async def middleware(request: Request, call_next):
if request.method == "HEAD":
response = Response()
elif "herokuapp" in urlparse(str(request.url)).netloc:
domain = os.getenv("DOMAIN")
if domain:
url = urlparse(str(request.url))._replace(netloc=domain).geturl()
response = RedirectResponse(url)
else:
response = await call_next(request)
else:
response = await call_next(request)
return response
@app.get("/")
async def get_root(request: Request):
return {"message": "hello, world"}
@app.get("/members")
async def get_members(request: Request):
return members.main()