-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
53 lines (40 loc) · 1.26 KB
/
main.py
File metadata and controls
53 lines (40 loc) · 1.26 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
"""FastAPI application for the Python API."""
from contextlib import asynccontextmanager
from core import __version__
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from routes import kaspa, server
from utils import KaspaClient
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Manage application lifespan - startup and shutdown."""
# Startup
app.state.kaspa_client = KaspaClient()
await app.state.kaspa_client.connect()
yield
# Shutdown
await app.state.kaspa_client.disconnect()
def create_app() -> FastAPI:
"""Create and configure the FastAPI application."""
app = FastAPI(
title="Python API",
description="Starter kit on Kaspa",
version=__version__,
lifespan=lifespan,
docs_url="/docs",
redoc_url="/redoc",
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure appropriately for production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers
app.include_router(server.router, tags=["Health"])
app.include_router(kaspa.router, prefix="/api", tags=["Kaspa"])
return app
# Create app instance
app = create_app()