Skip to content

Commit 0388622

Browse files
committed
health endpoint
1 parent 834f6f6 commit 0388622

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const softdeletion = process.env.softdeletion || true;
1010
const maxPasteIDLength = process.env.maxpasteidlength || 10;
1111
const agent = new https.Agent({ rejectUnauthorized: false }); // Disable SSL verification
1212

13+
let startupTime
14+
let downloads = 0;
15+
let deletions = 0;
16+
1317

1418
// Define the directory for paste storage
1519
const PASTES_DIR = path.join(__dirname, 'pastes');
@@ -83,12 +87,14 @@ const fetchPasteContent = (pasteId, headers, onSuccess, onError) => {
8387
res.on('end', () => {
8488
if (res.statusCode === 200) {
8589
console.log("Successfully got paste content for " + pasteId);
90+
downloads += 1;
8691
onSuccess(Buffer.concat(data).toString(), res.headers);
8792
} else if (res.statusCode === 404) {
8893
let filePath = getFilePath(pasteId);
8994
if(fs.existsSync(filePath) && softdeletion) {
9095
fs.rename(filePath, filePath + "-deleted", () => {
9196
console.log("Soft-deleted paste for " + pasteId);
97+
deletions += 1;
9298
onError(`Paste not found (status: ${res.statusCode})`);
9399
});
94100
} else {
@@ -116,6 +122,19 @@ app.get('/api/v1/pastes/:id/cache', async (req, res) => {
116122
}
117123
});
118124

125+
app.get("/api/v1/health", (req, res) => {
126+
const amountCached = fs.readdirSync(PASTES_DIR).length
127+
128+
129+
res.status(200).send({
130+
uptime: Date.now() - startupTime,
131+
downloads: downloads,
132+
deletions: deletions,
133+
amountcached: amountCached,
134+
status: "ok",
135+
});
136+
})
137+
119138
// Route to fetch live or cached paste content
120139
app.get('/api/v1/pastes/:id/raw', async (req, res) => {
121140
const pasteId = req.params.id;

0 commit comments

Comments
 (0)