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.middleware.cors import CORSMiddleware
|
||||
from datetime import datetime
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
origins = [
|
||||
"http://localhost",
|
||||
"http://localhost:5173",
|
||||
]
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def get_current_time(request: Request):
|
||||
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
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__":
|
||||
import asyncio
|
||||
from hypercorn.config import Config
|
||||
from hypercorn.asyncio import serve
|
||||
message = request.query_params.get("message")
|
||||
if message:
|
||||
response["message"] = message
|
||||
|
||||
asyncio.run(serve(app, Config()))
|
||||
return response
|
||||
Reference in New Issue
Block a user