Skip to content

Commit 434218e

Browse files
committed
Update healthcheck
1 parent c4ad00e commit 434218e

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

app/package-lock.json

Lines changed: 10 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "notesx-api",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"scripts": {
55
"dev": "ts-node src/index.ts",
66
"build": "npx tsc",
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"@hono/node-server": "^1.13.8",
12-
"better-sqlite3": "^11.8.1",
12+
"better-sqlite3": "^12.2.0",
1313
"dotenv": "^16.4.5",
1414
"hono": "^4.7.1",
1515
"node-cron": "^3.0.3",

app/src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { serve } from '@hono/node-server'
33
import { serveStatic } from '@hono/node-server/serve-static'
44
import { cors } from 'hono/cors'
55
import { etag } from 'hono/etag'
6-
import { App, StatusCodes } from './types'
6+
import { App, serverError, ServerErrors, StatusCodes } from './types'
77
import db from './v1/Database'
88
import Cloudflare from './v1/Cloudflare'
99
import log from './v1/Log'
@@ -12,6 +12,7 @@ import { router as accountRouter } from './v1/routes/account'
1212
import { HTTPException } from 'hono/http-exception'
1313
import { Cron } from './v1/Cron'
1414
import { trackView } from './v1/routes/middleware'
15+
import fs from 'fs'
1516

1617
require('dotenv').config()
1718

@@ -31,7 +32,16 @@ const app = new Hono()
3132
app.use('/v1/*', cors()) // CORS for all API routes
3233
app.route('/v1/file', fileRouter)
3334
app.route('/v1/account', accountRouter)
34-
app.get('/v1/ping', () => new Response('ok'))
35+
app.get('/v1/ping', async () => {
36+
try {
37+
// Check to make sure the upload location exists and is writeable
38+
await fs.promises.access(appInstance.baseFolder, fs.constants.W_OK)
39+
return new Response('ok')
40+
} catch (e) {
41+
console.log(e)
42+
return new Response('', { status: serverError(ServerErrors.FILESYSTEM_NOT_WRITABLE) })
43+
}
44+
})
3545

3646
// Add etags for all files
3747
app.use('*', etag())
@@ -86,7 +96,7 @@ app.all('*', (c) => {
8696

8797
app.onError((error, c) => {
8898
const err = error as HTTPException
89-
const status = typeof err?.status === 'number' ? err.status : 500
99+
const status = err.status || 500
90100
log.event(c, {
91101
status,
92102
endpoint: c.req.path

app/src/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import Cloudflare from './v1/Cloudflare';
2-
import { Log } from './v1/Log';
1+
import Cloudflare from './v1/Cloudflare'
2+
import { Log } from './v1/Log'
33
import { SQLite } from './v1/Database'
44
import { ContentfulStatusCode } from 'hono/dist/types/utils/http-status'
55

@@ -25,7 +25,8 @@ export enum ServerErrors {
2525
FILE_FAILED_TO_UPLOAD,
2626
USER_FAILED_TO_SAVE,
2727
API_KEY_FAILED_TO_SAVE,
28-
TURNSTILE_NO_VERIFY
28+
TURNSTILE_NO_VERIFY,
29+
FILESYSTEM_NOT_WRITABLE
2930
}
3031

3132
export const StatusCodes: { [key: number]: string } = {
@@ -40,7 +41,7 @@ export const StatusCodes: { [key: number]: string } = {
4041
461: 'I am currently performing maintenance on the server. Service will return to normal soon.',
4142
462: 'Invalid API key, you should automatically be redirected to your browser to request a new one', // 462 will automatically get a new key
4243
463: 'Invalid authentication token'
43-
};
44+
}
4445

4546
export function serverError (error: ServerErrors) {
4647
return (560 + error) as ContentfulStatusCode

0 commit comments

Comments
 (0)