11import os
22from contextlib import asynccontextmanager
3- from fastapi import FastAPI
3+ from fastapi import FastAPI , Request
4+ from fastapi .responses import JSONResponse
45from fastapi .staticfiles import StaticFiles
56from starlette .middleware .sessions import SessionMiddleware
67from starlette .middleware .cors import CORSMiddleware
@@ -50,6 +51,12 @@ async def lifespan(app: FastAPI):
5051 await database .disconnect ()
5152
5253
54+ service_status = getattr (settings , "SERVICE_STATUS" , "ok" )
55+ if service_status != "ok" :
56+ warning_message = f"---\n > ⚠️ **_NOTE:_** _{ service_status } _\n ---\n \n "
57+ description = warning_message + description
58+
59+
5360app = FastAPI (
5461 title = "EBRAINS Neuromorphic Computing Job Queue API" ,
5562 description = description ,
@@ -66,6 +73,21 @@ async def lifespan(app: FastAPI):
6673 allow_headers = ["*" ],
6774)
6875
76+
77+ @app .middleware ("http" )
78+ async def check_service_status (request : Request , call_next ):
79+ if request .url .path != "/" and (
80+ "down" in service_status
81+ or ("read-only" in service_status and request .method in ("POST" , "PUT" , "PATCH" ))
82+ ):
83+ return JSONResponse (
84+ status_code = 503 ,
85+ content = {"error" : service_status },
86+ )
87+ response = await call_next (request )
88+ return response
89+
90+
6991app .include_router (for_users .router , tags = ["For all users" ])
7092app .include_router (for_providers .router , tags = ["For use by computing system providers" ])
7193app .include_router (for_admins .router , tags = ["For use by administrators" ])
0 commit comments