file_table_reader/api/routers/system_router.py
2026-02-24 08:47:34 +07:00

111 lines
3.0 KiB
Python
Executable File

import httpx
from fastapi import APIRouter
from datetime import datetime, timedelta
import requests
from core.config import API_VERSION, GEOSERVER_URL, GEOSERVER_USER, GEOSERVER_PASS, GEONETWORK_URL, GEONETWORK_USER, GEONETWORK_PASS
router = APIRouter()
@router.get("/status")
async def server_status():
utc_time = datetime.utcnow()
wib_time = utc_time + timedelta(hours=7)
formatted_time = wib_time.strftime("%d-%m-%Y %H:%M:%S")
return {
"status": "success",
"message": "Server is running smoothly ✅",
"data": {
"service": "upload_automation",
"timestamp": f"{formatted_time} WIB"
},
"meta": {"version": API_VERSION, "environment": "deployment"}
}
@router.get("/status/geoserver")
async def check_geoserver_auth():
url = f"{GEOSERVER_URL}/rest/about/version."
auth = (GEOSERVER_USER, GEOSERVER_PASS)
try:
async with httpx.AsyncClient() as client:
response = await client.get(url, auth=auth, timeout=5)
return {
"status": "OK" if response.status_code == 200 else "ERROR",
"code": response.status_code,
"response": response.text
}
except Exception as e:
return {"status": "FAILED", "error": str(e)}
@router.get("/status/geonetwork")
def test_geonetwork_connection():
url = f"{GEONETWORK_URL}/srv/api/site"
headers = {
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest"
}
try:
response = requests.get(
url,
auth=(GEONETWORK_USER, GEONETWORK_PASS),
headers=headers,
timeout=10
)
if response.status_code == 401:
return {
"status": "ERROR",
"message": "Unauthorized — cek username/password GeoNetwork."
}
if response.status_code == 403:
return {
"status": "ERROR",
"message": "Forbidden — akun tidak punya akses ke API."
}
if response.status_code != 200:
return {
"status": "ERROR",
"message": "GeoNetwork merespon dengan error.",
"code": response.status_code,
"detail": response.text
}
return {
"status": "OK",
"code": response.status_code,
"message": "Terhubung ke GeoNetwork.",
"geonetwork_info": response.json()
}
except requests.exceptions.ConnectionError:
return {
"status": "ERROR",
"message": "Tidak dapat terhubung ke GeoNetwork (server offline / URL salah)"
}
except requests.exceptions.Timeout:
return {
"status": "ERROR",
"message": "Timeout menghubungi GeoNetwork."
}
except Exception as e:
return {
"status": "ERROR",
"message": "Unexpected error",
"detail": str(e)
}