-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96486ee
commit bee0dc9
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters