79 lines
2.1 KiB
Python
79 lines
2.1 KiB
Python
from contextlib import asynccontextmanager
|
|
import asyncpg
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from core.config import API_VERSION, ALLOWED_ORIGINS, DB_DSN
|
|
from api.routers.system_router import router as system_router
|
|
from api.routers.upload_file_router import router as upload_router
|
|
from api.routers.datasets_router import router as dataset_router
|
|
from services.pipeline.api.router import router as pipeline_router
|
|
# from contextlib import asynccontextmanager
|
|
# from utils.qgis_init import init_qgis
|
|
|
|
|
|
db_pool = None
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# 1. Start: Buat Pool koneksi saat aplikasi nyala
|
|
global db_pool
|
|
print("Creating DB Pool...")
|
|
db_pool = await asyncpg.create_pool(DB_DSN, min_size=5, max_size=20)
|
|
yield
|
|
# 2. Shutdown: Tutup Pool saat aplikasi mati
|
|
print("Closing DB Pool...")
|
|
await db_pool.close()
|
|
|
|
|
|
|
|
app = FastAPI(
|
|
title="ETL Geo Upload Service",
|
|
version=API_VERSION,
|
|
description="Upload Automation API",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=ALLOWED_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
# Base.metadata.create_all(bind=engine)
|
|
|
|
# qgis setup
|
|
# @asynccontextmanager
|
|
# async def lifespan(app: FastAPI):
|
|
# global qgs
|
|
# qgs = init_qgis()
|
|
# print("QGIS initialized")
|
|
|
|
# yield
|
|
|
|
# # SHUTDOWN (optional)
|
|
# print("Shutting down...")
|
|
|
|
# app = FastAPI(lifespan=lifespan)
|
|
|
|
|
|
# @app.get("/qgis/status")
|
|
# def qgis_status():
|
|
# try:
|
|
# version = QgsApplication.qgisVersion()
|
|
# return {
|
|
# "qgis_status": "connected",
|
|
# "qgis_version": version
|
|
# }
|
|
# except Exception as e:
|
|
# return {
|
|
# "qgis_status": "error",
|
|
# "error": str(e)
|
|
# }
|
|
|
|
# Register routers
|
|
app.include_router(system_router, tags=["System"])
|
|
app.include_router(upload_router, prefix="/upload", tags=["Upload"])
|
|
app.include_router(dataset_router, prefix="/dataset", tags=["Upload"])
|
|
app.include_router(pipeline_router) |