Skip to content

Commit

Permalink
feat: updated health check endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
CS76 committed Aug 3, 2023
1 parent 7704bee commit a08b2e2
Show file tree
Hide file tree
Showing 5 changed files with 1,140 additions and 18 deletions.
58 changes: 46 additions & 12 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fastapi import FastAPI
import os
from fastapi import FastAPI, status
from fastapi.responses import RedirectResponse
from fastapi_versioning import VersionedFastAPI

Expand All @@ -8,6 +9,7 @@
from app.core import config, tasks

from prometheus_fastapi_instrumentator import Instrumentator
from app.schemas import HealthCheck

app = FastAPI(
title=config.PROJECT_NAME,
Expand All @@ -24,16 +26,6 @@
},
)

origins = ["*"]

app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)

app.include_router(chem.router)

app.add_event_handler("startup", tasks.create_start_app_handler(app))
Expand All @@ -58,7 +50,49 @@

Instrumentator().instrument(app).expose(app)

origins = ["*"]


@app.middleware("http")
async def add_cors_headers(request, call_next):
response = await call_next(request)
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS"
response.headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization"
return response


app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)


@app.get("/", include_in_schema=False)
async def root():
return RedirectResponse(url="https://nfdi4chem.github.io/nmrkit")
return RedirectResponse(url=os.getenv("HOMEPAGE_URL", "/latest/docs"))


@app.get(
"/health",
tags=["healthcheck"],
summary="Perform a Health Check",
response_description="Return HTTP Status Code 200 (OK)",
status_code=status.HTTP_200_OK,
response_model=HealthCheck,
)
def get_health() -> HealthCheck:
"""
## Perform a Health Check
Endpoint to perform a healthcheck on. This endpoint can primarily be used Docker
to ensure a robust container orchestration and management is in place. Other
services which rely on proper functioning of the API service will not deploy if this
endpoint returns any other HTTP status code except 200 (OK).
Returns:
HealthCheck: Returns a JSON response with the health status
"""
return HealthCheck(status="OK")
12 changes: 6 additions & 6 deletions app/routers/chem.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
retrieve,
RegistrationFailureReasons,
)
from app import schemas
from rdkit import Chem
from io import BytesIO
from app.schemas import HealthCheck

router = APIRouter(
prefix="/chem",
Expand All @@ -23,27 +23,27 @@
)


@router.get("/", include_in_schema=False)
@router.get(
"/health",
tags=["healthcheck"],
summary="Perform a Health Check on Chem Module",
response_description="Returns HTTP Status Code 200 (OK)",
response_description="Return HTTP Status Code 200 (OK)",
status_code=status.HTTP_200_OK,
response_model=schemas.HealthCheck,
include_in_schema=False,
response_model=HealthCheck,
)
def get_health() -> schemas.HealthCheck:
def get_health() -> HealthCheck:
"""
## Perform a Health Check
Endpoint to perform a healthcheck on. This endpoint can primarily be used Docker
to ensure a robust container orchestration and management is in place. Other
services which rely on proper functioning of the API service will not deploy if this
endpoint returns any other HTTP status code except 200 (OK).
Returns:
HealthCheck: Returns a JSON response with the health status
"""
return schemas.HealthCheck(status="OK")
return HealthCheck(status="OK")


@router.post(
Expand Down
39 changes: 39 additions & 0 deletions app/scripts/nmr-load-save/bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node

const yargs = require("yargs");
const loader = require("nmr-load-save");
const fileUtils = require("filelist-utils");

const options = yargs
.usage("Usage: -u <url>")
.option("u", { alias: "url", describe: "File URL", type: "string", demandOption: true })
.argv;

async function loadSpectrum(url) {
const {pathname:relativePath,origin:baseURL} = new URL(url);
const source = {
entries: [
{
relativePath,
}
],
baseURL
};
const fileCollection = await fileUtils.fileCollectionFromWebSource(source,{});

const {
nmriumState: { data },
} = await loader.read(fileCollection);
return data;
}



const url = options.u.split(" ");


loadSpectrum(options.u).then((result)=>{
console.log(JSON.stringify(result))
})


Loading

0 comments on commit a08b2e2

Please sign in to comment.