kopia lustrzana https://github.com/transitive-bullshit/chatgpt-api
pull/715/head
rodzic
139f0695bf
commit
5fa7c5ae2d
|
@ -0,0 +1,40 @@
|
||||||
|
import { promisify } from 'node:util'
|
||||||
|
|
||||||
|
import type { ServerType } from '@hono/node-server'
|
||||||
|
import { asyncExitHook } from 'exit-hook'
|
||||||
|
import restoreCursor from 'restore-cursor'
|
||||||
|
|
||||||
|
import { db } from '@/db'
|
||||||
|
|
||||||
|
export function initExitHooks({
|
||||||
|
server,
|
||||||
|
timeoutMs = 10_000
|
||||||
|
}: {
|
||||||
|
server: ServerType
|
||||||
|
timeoutMs?: number
|
||||||
|
}) {
|
||||||
|
// Gracefully restore the cursor if run from a TTY
|
||||||
|
restoreCursor()
|
||||||
|
|
||||||
|
// Gracefully shutdown the HTTP server
|
||||||
|
asyncExitHook(
|
||||||
|
async function shutdownServerExitHook() {
|
||||||
|
await promisify(server.close)()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
wait: timeoutMs
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Gracefully shutdown the postgres database connection
|
||||||
|
asyncExitHook(
|
||||||
|
async function shutdownDbExitHook() {
|
||||||
|
await db.$client.end({
|
||||||
|
timeout: timeoutMs
|
||||||
|
})
|
||||||
|
},
|
||||||
|
{
|
||||||
|
wait: timeoutMs
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
|
@ -1,17 +1,13 @@
|
||||||
import { promisify } from 'node:util'
|
|
||||||
|
|
||||||
import { serve } from '@hono/node-server'
|
import { serve } from '@hono/node-server'
|
||||||
import { asyncExitHook } from 'exit-hook'
|
|
||||||
import { Hono } from 'hono'
|
import { Hono } from 'hono'
|
||||||
import { compress } from 'hono/compress'
|
import { compress } from 'hono/compress'
|
||||||
import { cors } from 'hono/cors'
|
import { cors } from 'hono/cors'
|
||||||
import restoreCursor from 'restore-cursor'
|
|
||||||
|
|
||||||
import { apiV1 } from '@/api-v1'
|
import { apiV1 } from '@/api-v1'
|
||||||
import { env } from '@/lib/env'
|
import { env } from '@/lib/env'
|
||||||
import * as middleware from '@/lib/middleware'
|
import * as middleware from '@/lib/middleware'
|
||||||
|
|
||||||
restoreCursor()
|
import { initExitHooks } from './lib/exit-hooks'
|
||||||
|
|
||||||
export const app = new Hono()
|
export const app = new Hono()
|
||||||
|
|
||||||
|
@ -27,11 +23,4 @@ const server = serve({
|
||||||
port: env.PORT
|
port: env.PORT
|
||||||
})
|
})
|
||||||
|
|
||||||
asyncExitHook(
|
initExitHooks({ server })
|
||||||
async () => {
|
|
||||||
await promisify(server.close)()
|
|
||||||
},
|
|
||||||
{
|
|
||||||
wait: 10_000
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
Ładowanie…
Reference in New Issue