Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
549 lines (480 loc) · 23.4 KB
/
Copy pathmain.py
File metadata and controls
549 lines (480 loc) · 23.4 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import asyncio
import gzip
import logging
import os
import time
from contextlib import asynccontextmanager
from pathlib import Path
from dotenv import load_dotenv
# Ensure we always load PUNoted-Prod/.env regardless of working directory
_env_path = Path(__file__).resolve().parent / ".env"
load_dotenv(dotenv_path=_env_path)
from fastapi import FastAPI, Request, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from fastapi.responses import JSONResponse
from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend
from prometheus_fastapi_instrumentator import Instrumentator
from redis import asyncio as aioredis
from slowapi.errors import RateLimitExceeded
from starlette.types import ASGIApp, Receive, Scope, Send
from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
from app.core.event_manager import EventManager
from app.core.limiter import limiter, rate_limit_exceeded_handler
from app.routers.buildings import buildings_router as internal_buildings_router
from routers.internal.materials import materials_router as internal_materials_router
from app.services.background import scrape_and_save_data, scrape_prices_and_save_data
from auth import auth_router
from data_handlers import BackgroundTasks, data_router
from db import Database
from discord_bot.webhook import router as discord_router
from endpoints.Protected.routers.accounting import (
accounting_router as api_useraccounting_external_router,
)
from endpoints.Protected.routers.contracts import (
contracts_router as api_usercontracts_external_router,
)
from endpoints.Protected.routers.corporation import corporation_router as corporation_external_router
from endpoints.Protected.routers.cxuser import cxuser_router as api_cxuser_router
from endpoints.Protected.routers.flights import (
flights_router as api_userflights_external_router,
)
from endpoints.Protected.routers.production import (
production_router as api_userproduction_external_router,
)
from endpoints.Protected.routers.ships import (
ships_router as api_userships_external_router,
)
from endpoints.Protected.routers.sites import (
sites_router as api_usersites_external_router,
)
from endpoints.Protected.routers.storageuser import (
storage_router as api_storageuser_router,
)
from endpoints.Protected.routers.user import user_router as api_user_router
from endpoints.Protected.routers.workforce import (
workforce_router as api_userworkforce_external_router,
)
from endpoints.Protected.routers.shipments import (
protected_shipments_router as api_shipments_external_router,
)
from endpoints.Protected.routers.banking import (
protected_banking_router as api_banking_external_router,
)
from endpoints.Public.routers.buildings import buildings_router as buildings_public_external_router
from endpoints.Public.routers.banking import (
public_banking_router as public_banking_external_router,
)
from endpoints.Public.routers.company import company_router as company_public_external_router
from endpoints.Public.routers.corp import corporation_router as corporation_public_external_router
from endpoints.Public.routers.cx import cx_router as api_cx_external_router
from endpoints.Public.routers.materials import materials_router as api_materials_external_router
from endpoints.Public.routers.planets import planets_router as api_planets_external_router
from endpoints.Public.routers.vendors import (
vendors_router as api_vendors_external_router,
)
from endpoints.Public.routers.governance import (
governance_router as governance_public_external_router,
)
from endpoints.Public.routers.shipments import shipments_router as shipments_public_external_router
from routers.cxuser import cx_router
from routers.governance import governance_router
from routers.group import group_router
from routers.internal.contracts import contracts_router
from routers.internal.banking import banking_router
from routers.internal.corporation import corporation_internal_router
from routers.internal.corporation_ships import corp_ships_internal_router
from routers.internal.company import internal_company_router
from routers.internal.cx import cx_internal_router
from routers.internal.data_group import group_router as internal_data_group_router
from routers.internal.finances import finances_router
from routers.internal.leaderboard import leaderboard_router
from routers.internal.production import production_router
from routers.internal.storage import storage_router
from routers.internal.users import users_router
from routers.internal.ships import ships_router
from routers.internal.ship_routes import ship_routes_router
from routers.internal.sites import sites_router
from routers.internal.notifications import notifications_router
from routers.internal.notification_rules import notification_rules_router
from routers.internal.entity_settings import entity_settings_router
from routers.public.announcements import announcements_router
from routers.logistics import logistics_router
from routers.map import map_router
from routers.planets import planets_router
from routers.user import user_router
from routers.usersettings import user_settings_router as settings_router
from routers.vendor import vendor_router
from routers.websocket_router import ws_router
# Logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(name)s - %(message)s")
logger = logging.getLogger(__name__)
# --- Lifecycle Events ---
@asynccontextmanager
async def lifespan(app: FastAPI):
# 1. Database
await db.create_pool()
app.state.db = db
v1_app.state.db = db # Propagate to sub-app
print("Database connected.")
# 2. Redis Cache
redis_url = os.getenv("REDIS_URL", "redis://localhost:6379/0")
redis = aioredis.from_url(redis_url, encoding="utf8", decode_responses=True)
FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache")
print("Redis initialized.")
event_manager = EventManager(db.pool)
app.state.event_manager = event_manager
v1_app.state.event_manager = event_manager # Propagate to sub-app
async def periodic_notification_evaluator():
from managers.global_ws_manager import global_ws_manager
from services.notification_evaluator import evaluate_user_telemetry_notifications
while True:
try:
await asyncio.sleep(300)
connected_user_ids = list(global_ws_manager.user_sockets.keys())
for uid in connected_user_ids:
try:
await evaluate_user_telemetry_notifications(db.pool, uid)
except Exception as ex:
logger.error(f"Periodic evaluator error for user {uid}: {ex}")
except asyncio.CancelledError:
break
except Exception as e:
logger.error(f"Error in periodic_notification_evaluator loop: {e}")
eval_task = asyncio.create_task(periodic_notification_evaluator())
yield
eval_task.cancel()
print("Shutting down...")
await db.close_pool()
print("System shutdown complete.")
# --- App Initialization ---
import os
DEBUG_MODE = os.getenv("DEBUG_MODE", "false").lower() == "true"
# Main application (internal)
app = FastAPI(
title="PUNoted API (Internal)",
description="Internal endpoints and main frontend API.",
version="1.0.0",
lifespan=lifespan,
openapi_url="/openapi.json" if DEBUG_MODE else None,
docs_url="/docs" if DEBUG_MODE else None,
redoc_url=None,
)
# V1 Sub-application for public external API
v1_app = FastAPI(
title="PUNoted API (v1)",
description="Public API for PrUn data.",
version="1.0.0",
lifespan=None,
openapi_url="/openapi.json",
docs_url="/docs",
redoc_url=None,
)
app.state.limiter = limiter
v1_app.state.limiter = limiter
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
try:
body = await request.json()
except Exception:
body = await request.body()
logger.error(f"Validation error on {request.method} {request.url.path}: {exc.errors()} | Received body: {body}")
return JSONResponse(
status_code=422,
content={"detail": exc.errors(), "body": str(body)}
)
app.add_exception_handler(RateLimitExceeded, rate_limit_exceeded_handler)
v1_app.add_exception_handler(RateLimitExceeded, rate_limit_exceeded_handler)
@app.exception_handler(Exception)
@v1_app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
logger.error(f"Unhandled 500 server error on {request.method} {request.url.path}: {exc}", exc_info=True)
return JSONResponse(
status_code=500,
content={"detail": "Internal Server Error"},
headers={"Access-Control-Allow-Origin": request.headers.get("origin") or "*"}
)
Instrumentator().instrument(app).expose(app)
class SecurityLoggerMiddleware:
def __init__(self, app: ASGIApp):
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send):
if scope["type"] != "http":
return await self.app(scope, receive, send)
start_time = time.perf_counter()
status_code = [200]
async def wrapped_send(message: dict):
if message["type"] == "http.response.start":
status_code[0] = message["status"]
await send(message)
# Process request
await self.app(scope, receive, wrapped_send)
# Post-processing
process_time = (time.perf_counter() - start_time) * 1000
path = scope.get("path", "")
if not any(p in path for p in ["/status", "/metrics", "/favicon.ico"]):
# Look for the tags created by DecompressMiddleware
orig_size = scope.get("original_gzip_size")
decomp_size = scope.get("decompressed_size")
headers = dict(scope.get("headers", []))
# Safe IP detection from b"headers" list
xf = headers.get(b"x-forwarded-for", b"").decode().split(",")[0].strip()
real_ip = xf if xf else (scope.get("client")[0] if scope.get("client") else "unknown")
if orig_size is not None:
size_mb = orig_size / (1024 * 1024)
ratio = (decomp_size / orig_size) if orig_size > 0 else 0
tag = f"GZIP ({ratio:.1f}x to {decomp_size / (1024*1024):.2f}MB)"
else:
# Fallback for RAW requests
try:
cl = int(headers.get(b"content-length", 0))
except: cl = 0
size_mb = cl / (1024 * 1024)
tag = "RAW"
log_msg = (
f"SECURITY: {real_ip} | {scope['method']} {path} | "
f"SIZE: {size_mb:.2f}MB | {tag} | "
f"STATUS: {status_code[0]} | TIME: {process_time:.2f}ms"
)
if status_code[0] == 401:
auth_hdr = headers.get(b"authorization", b"").decode("utf-8", errors="ignore")
data_tok = headers.get(b"x-data-token", b"").decode("utf-8", errors="ignore")
logger.warning(f"DEBUG 401 on {path} | Authorization: {auth_hdr[:15]}... | X-Data-Token: {data_tok[:15]}...")
if size_mb > 5 or status_code[0] >= 400:
logger.warning(log_msg)
else:
logger.info(log_msg)
class DecompressMiddleware:
def __init__(self, app: ASGIApp):
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send):
if scope["type"] != "http":
await self.app(scope, receive, send)
return
headers = dict(scope.get("headers", []))
content_encoding = headers.get(b"content-encoding", b"")
if b"gzip" in content_encoding:
# 1. Strip the encoding headers so downstream doesn't try to decompress again
new_headers = [
(k, v) for k, v in scope["headers"]
if k.lower() not in (b"content-encoding", b"content-length")
]
scope["headers"] = new_headers
# 2. Collect the full compressed body
body_chunks = []
while True:
message = await receive()
if message["type"] == "http.request":
body_chunks.append(message.get("body", b""))
if not message.get("more_body", False):
break
else:
# If we get a disconnect or other event, pass it through
await self.app(scope, lambda: message, send)
return
full_body = b"".join(body_chunks)
# 3. Decompress
try:
if not full_body:
decompressed_body = b""
else:
decompressed_body = gzip.decompress(full_body)
# Metrics for your logs
scope["original_gzip_size"] = len(full_body)
scope["decompressed_size"] = len(decompressed_body)
except Exception as e:
logger.error(f"GZIP DECOMPRESSION FAILED: {e}")
# Fallback to original body if decompression fails
decompressed_body = full_body
# 4. Create a stateful receive function
# Downstream apps (and Sentry) call receive() multiple times.
# First call gets the data. Second call gets empty + more_body: False.
has_sent_body = False
async def _receive() -> dict:
nonlocal has_sent_body
if not has_sent_body:
has_sent_body = True
return {
"type": "http.request",
"body": decompressed_body,
"more_body": False
}
# Subsequent calls return a disconnect or empty state
return {"type": "http.request", "body": b"", "more_body": False}
await self.app(scope, _receive, send)
return
# Not GZIP? Just pass through
await self.app(scope, receive, send)
app.add_middleware(DecompressMiddleware)
app.add_middleware(SecurityLoggerMiddleware)
# Middleware
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:5174",
"http://127.0.0.1:5174",
"https://punoted.net",
"https://apex.prosperousuniverse.com",
],
allow_origin_regex=r"(chrome|moz)-extension://.*",
allow_credentials=True,
allow_methods=["*"],
allow_headers=[
"authorization",
"content-type",
"x-data-token",
"content-encoding",
"x-extension-client"
],
)
v1_app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:5174",
"http://127.0.0.1:5174",
"https://punoted.net",
"https://apex.prosperousuniverse.com",
],
allow_origin_regex=r"(chrome|moz)-extension://.*",
allow_credentials=True,
allow_methods=["*"],
allow_headers=[
"authorization",
"content-type",
"x-data-token",
"content-encoding",
"x-extension-client"
],
)
app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*")
app.add_middleware(GZipMiddleware, minimum_size=1000)
db = Database()
# --- Route Includes ---
# Core
app.include_router(auth_router, prefix="/auth", tags=["auth"])
app.include_router(data_router, tags=["data"])
app.include_router(discord_router, prefix="/discord")
app.include_router(ws_router)
# Application Logic
app.include_router(group_router, prefix="/groups")
app.include_router(map_router, prefix="/map")
#app.include_router(contracts_router)
app.include_router(logistics_router)
app.include_router(cx_router, prefix="/cx", tags=["cx"])
app.include_router(governance_router, prefix="/governance", tags=["governance"])
app.include_router(planets_router, prefix="/planets", tags=["planets"])
# Internal
app.include_router(
corporation_internal_router,
prefix="/internal/corporation",
tags=["API_corporation"],
)
app.include_router(
corp_ships_internal_router,
prefix="/internal/corporation",
tags=["API_corporation_ships"],
)
app.include_router(storage_router, prefix="/internal/storage", tags=["API_storage"])
app.include_router(production_router, prefix="/internal/production", tags=["API_production"])
app.include_router(internal_data_group_router, prefix="/internal/datagroup", tags=["API_group"])
app.include_router(user_router, prefix="/internal/users", tags=["users"])
app.include_router(settings_router, prefix="/internal/settings", tags=["User Settings"])
app.include_router(contracts_router, prefix="/internal/contracts", tags=["Contracts"])
app.include_router(banking_router, prefix="/internal/banking", tags=["Banking"])
app.include_router(leaderboard_router, prefix="/internal/leaderboard", tags=["Leaderboard"])
app.include_router(finances_router, prefix="/internal/finances", tags=["Finances"])
app.include_router(internal_buildings_router, prefix="/internal/buildings", tags=["Buildings"])
app.include_router(internal_materials_router, prefix="/internal/materials", tags=["Materials"])
app.include_router(cx_internal_router, prefix="/internal/cx", tags=["CX"])
app.include_router(users_router, prefix="/internal/users", tags=["Users"])
app.include_router(notifications_router, prefix="/internal/notifications", tags=["Notifications"])
app.include_router(notification_rules_router, prefix="/internal/notifications/settings-api", tags=["Notification Rules"])
app.include_router(entity_settings_router, prefix="/internal/entity-settings", tags=["Entity Settings"])
app.include_router(announcements_router, prefix="/public/announcements", tags=["Announcements"])
app.include_router(shipments_public_external_router, prefix="/public/shipments", tags=["Public Shipments"])
app.include_router(ships_router, prefix="/internal/ships", tags=["Ships"])
app.include_router(ship_routes_router, prefix="/internal/ship-routes", tags=["Ship Routes"])
app.include_router(sites_router, prefix="/internal/sites", tags=["Sites"])
app.include_router(vendor_router, prefix="/internal/vendor", tags=["Vendor"])
app.include_router(internal_company_router, prefix="/internal/company", tags=["Company"])
# Protected External API v1 (Registered to v1_app sub-app)
v1_app.include_router(api_user_router, prefix="/user", tags=["User Data"])
v1_app.include_router(api_cxuser_router, prefix="/cxuser", tags=["CX User Data"])
v1_app.include_router(api_storageuser_router, prefix="/storages", tags=["Storage User Data"])
v1_app.include_router(api_usercontracts_external_router, prefix="/contracts", tags=["Contracts Data"])
v1_app.include_router(api_userflights_external_router, prefix="/flights", tags=["Flights Data"])
v1_app.include_router(
api_useraccounting_external_router,
prefix="/accounting",
tags=["Accounting Data"],
)
v1_app.include_router(api_usersites_external_router, prefix="/sites", tags=["Sites Data"])
v1_app.include_router(api_userships_external_router, prefix="/ships", tags=["Ships Data"])
v1_app.include_router(
api_userproduction_external_router,
prefix="/production",
tags=["Production Data"],
)
v1_app.include_router(api_userworkforce_external_router, prefix="/workforce", tags=["Workforce Data"])
v1_app.include_router(corporation_external_router, prefix="/corporation", tags=["Corporation Data"])
v1_app.include_router(api_banking_external_router, prefix="/banking", tags=["Banking Data"])
v1_app.include_router(api_shipments_external_router, prefix="/shipments", tags=["Shipments Data"])
# -- Public External API v1 (Registered to v1_app sub-app)
v1_app.include_router(api_vendors_external_router, prefix="/vendors", tags=["Vendors Data"])
v1_app.include_router(api_cx_external_router, prefix="/cx", tags=["CX Data"])
v1_app.include_router(api_materials_external_router, prefix="/materials", tags=["Materials Data"])
v1_app.include_router(api_planets_external_router, prefix="/planets", tags=["Planets Data"])
v1_app.include_router(corporation_public_external_router, prefix="/corporation", tags=["Corporation Data"])
v1_app.include_router(buildings_public_external_router, prefix="/buildings", tags=["Buildings Data"])
v1_app.include_router(company_public_external_router, prefix="/company", tags=["Company Data"])
v1_app.include_router(governance_public_external_router, prefix="/governance", tags=["Governance Data"])
v1_app.include_router(shipments_public_external_router, prefix="/shipments", tags=["Shipments Data"])
v1_app.include_router(public_banking_external_router, prefix="/banking", tags=["Public Banking Data"])
# If in development mode, register them to the main app as well with "/v1" prefixes,
# so that the main app's /docs page shows ALL endpoints (both internal and public).
if DEBUG_MODE:
app.include_router(api_user_router, prefix="/v1/user", tags=["User Data"])
app.include_router(api_cxuser_router, prefix="/v1/cxuser", tags=["CX User Data"])
app.include_router(api_storageuser_router, prefix="/v1/storages", tags=["Storage User Data"])
app.include_router(api_usercontracts_external_router, prefix="/v1/contracts", tags=["Contracts Data"])
app.include_router(api_userflights_external_router, prefix="/v1/flights", tags=["Flights Data"])
app.include_router(
api_useraccounting_external_router,
prefix="/v1/accounting",
tags=["Accounting Data"],
)
app.include_router(api_usersites_external_router, prefix="/v1/sites", tags=["Sites Data"])
app.include_router(api_userships_external_router, prefix="/v1/ships", tags=["Ships Data"])
app.include_router(
api_userproduction_external_router,
prefix="/v1/production",
tags=["Production Data"],
)
app.include_router(api_userworkforce_external_router, prefix="/v1/workforce", tags=["Workforce Data"])
app.include_router(corporation_external_router, prefix="/v1/corporation", tags=["Corporation Data"])
app.include_router(api_banking_external_router, prefix="/v1/banking", tags=["Banking Data"])
app.include_router(api_shipments_external_router, prefix="/v1/shipments", tags=["Shipments Data"])
app.include_router(api_vendors_external_router, prefix="/v1/vendors", tags=["Vendors Data"])
app.include_router(api_cx_external_router, prefix="/v1/cx", tags=["CX Data"])
app.include_router(api_materials_external_router, prefix="/v1/materials", tags=["Materials Data"])
app.include_router(api_planets_external_router, prefix="/v1/planets", tags=["Planets Data"])
app.include_router(corporation_public_external_router, prefix="/v1/corporation", tags=["Corporation Data"])
app.include_router(buildings_public_external_router, prefix="/v1/buildings", tags=["Buildings Data"])
app.include_router(company_public_external_router, prefix="/v1/company", tags=["Company Data"])
app.include_router(governance_public_external_router, prefix="/v1/governance", tags=["Governance Data"])
app.include_router(shipments_public_external_router, prefix="/v1/shipments", tags=["Shipments Data"])
app.include_router(public_banking_external_router, prefix="/v1/banking", tags=["Public Banking Data"])
# Mount the v1 sub-app under /v1 prefix
app.mount("/v1", v1_app)
@app.get("/")
def read_main():
return Response("frontend: https://punoted.net\ndocs: https://api.punoted.net/v1/docs\n")
@app.get("/status")
async def status_check():
return {"status": "online", "db_check": "success"}
if __name__ == "__main__":
import uvicorn
uvicorn.run("app.main:app", host="0.0.0.0", port=9901, reload=True, log_level="info")