Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
import os
|
| 3 |
+
import uuid
|
| 4 |
+
import platform
|
| 5 |
+
import psutil
|
| 6 |
+
import time
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
from typing import Optional
|
| 9 |
+
from fastapi import FastAPI, Query, HTTPException, Body, BackgroundTasks
|
| 10 |
+
from contextlib import asynccontextmanager
|
| 11 |
+
from pydantic import BaseModel
|
| 12 |
+
from fastapi.responses import FileResponse
|
| 13 |
+
|
| 14 |
+
from app.invite_logic import DiscordInviteLogic
|
| 15 |
+
invite_app = DiscordInviteLogic()
|
| 16 |
+
|
| 17 |
+
@asynccontextmanager
|
| 18 |
+
async def lifespan(app: FastAPI):
|
| 19 |
+
await invite_app.start()
|
| 20 |
+
yield
|
| 21 |
+
await invite_app.stop()
|
| 22 |
+
|
| 23 |
+
app = FastAPI(lifespan=lifespan)
|
| 24 |
+
|
| 25 |
+
START_TIME = time.time()
|
| 26 |
+
|
| 27 |
+
@app.get("/discord-invite")
|
| 28 |
+
async def get_new_invite():
|
| 29 |
+
res = await invite_app.get_invite()
|
| 30 |
+
if "error" in res:
|
| 31 |
+
raise HTTPException(status_code=500, detail=res["error"])
|
| 32 |
+
return res
|
| 33 |
+
|
| 34 |
+
@app.get("/")
|
| 35 |
+
def home():
|
| 36 |
+
uptime_seconds = int(time.time() - START_TIME)
|
| 37 |
+
uptime_string = str(datetime.utcfromtimestamp(uptime_seconds).strftime("%H:%M:%S"))
|
| 38 |
+
|
| 39 |
+
memory = psutil.virtual_memory()
|
| 40 |
+
cpu_percent = psutil.cpu_percent(interval=0.5)
|
| 41 |
+
|
| 42 |
+
return {
|
| 43 |
+
"status": "online",
|
| 44 |
+
"uptime_seconds": uptime_seconds,
|
| 45 |
+
"uptime_hms": uptime_string,
|
| 46 |
+
"process_id": os.getpid(),
|
| 47 |
+
"cpu_usage_percent": cpu_percent,
|
| 48 |
+
"total_ram_mb": round(memory.total / 1024 / 1024, 2),
|
| 49 |
+
"used_ram_mb": round(memory.used / 1024 / 1024, 2),
|
| 50 |
+
"ram_usage_percent": memory.percent,
|
| 51 |
+
"platform": platform.system(),
|
| 52 |
+
"platform_release": platform.release(),
|
| 53 |
+
"python_version": platform.python_version(),
|
| 54 |
+
"hostname": platform.node()
|
| 55 |
+
}
|