mirror of
https://github.com/Xevion/linkpulse.git
synced 2025-12-07 05:15:36 -06:00
add CORS, return message, fallback IP to request.client.host
This commit is contained in:
@@ -1,19 +1,37 @@
|
|||||||
from fastapi import FastAPI, Request
|
from fastapi import FastAPI, Request
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
origins = [
|
||||||
|
"http://localhost",
|
||||||
|
"http://localhost:5173",
|
||||||
|
]
|
||||||
|
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=origins,
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"],
|
||||||
|
allow_headers=["*"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def get_current_time(request: Request):
|
async def get_current_time(request: Request):
|
||||||
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
user_ip = request.headers.get("X-Forwarded-For")
|
user_ip = request.headers.get("X-Forwarded-For")
|
||||||
return {"time": current_time, "ip": user_ip}
|
if not user_ip:
|
||||||
|
# Fallback, probably not on a proxy
|
||||||
|
user_ip = request.client.host
|
||||||
|
|
||||||
|
response = {"time": current_time, "ip": user_ip}
|
||||||
|
|
||||||
if __name__ == "__main__":
|
message = request.query_params.get("message")
|
||||||
import asyncio
|
if message:
|
||||||
from hypercorn.config import Config
|
response["message"] = message
|
||||||
from hypercorn.asyncio import serve
|
|
||||||
|
|
||||||
asyncio.run(serve(app, Config()))
|
return response
|
||||||
Reference in New Issue
Block a user