Skip to content

Commit

Permalink
feat: add /health-check endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcrazycoder committed Jul 20, 2023
1 parent 96486ee commit bee0dc9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/controllers/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { type Request, type Response } from "express";
import { type CID } from 'kubo-rpc-client'

import { saveContentData } from "../models/Content";
import { ipfsCore } from "../modules/IPFSCore";
import { generateGatewayURL, getContentMetadata } from "../utils";

async function healthCheck(_request: Request, response: Response): Promise<Response> {
const ts = Date.now()
const cid = await uploadSampleContent()
let contentGatewayStatus = false

if(cid !== false) {
contentGatewayStatus = await isContentRetrievableFromGateway(cid)
}

return response.status(200).send({
ts,
node: !!cid,
contentGateway: contentGatewayStatus
})
}

async function uploadSampleContent(): Promise<CID | false> {
try {
const content = 'data:text/plain;base64,SGVsbG8gV29ybGQh'; // Hello World!
const { buff, ...metadata } = getContentMetadata(content);
const cid = await ipfsCore.add(buff, true);
const pinned = await ipfsCore.isPinned(cid);
await saveContentData({ cid, pinned, metadata });

return cid
} catch(error) {
return false
}
}

async function isContentRetrievableFromGateway(cid: CID): Promise<boolean> {
try {
const response = await fetch(generateGatewayURL(cid), {
redirect: 'follow'
})

return response.status === 200
} catch(error) {
return false
}
}

export {
healthCheck
}
9 changes: 9 additions & 0 deletions src/routes/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Router } from "express";

import { healthCheck } from "../controllers/base";

const router = Router();

router.get("/health-check", healthCheck);

export default { basePath: "/", router };
3 changes: 2 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { type NextFunction, type Request, type Response, Router } from "express";

import ERRORS from "../errors";
import BaseRouter from './base'
import IPFSRouter from "./ipfs";

const applicationRouter = Router();
const routers = [IPFSRouter];
const routers = [IPFSRouter, BaseRouter];

// Dynamically bind routers to the main application router
routers.forEach((router) => {
Expand Down

0 comments on commit bee0dc9

Please sign in to comment.