-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdatabase-ready.ts
More file actions
40 lines (33 loc) · 1.1 KB
/
database-ready.ts
File metadata and controls
40 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type Hapi from "@hapi/hapi";
import type { Contracts as ApiDatabaseContracts } from "@mainsail/api-database";
import type { Contracts } from "@mainsail/contracts";
import { Identifiers as ApiDatabaseIdentifiers } from "@mainsail/api-database";
export const databaseReady = {
getOnRequestHandler(app: Contracts.Kernel.Application) {
const systemRepository = app.get<ApiDatabaseContracts.SystemRepositoryFactory>(
ApiDatabaseIdentifiers.SystemRepositoryFactory,
)();
const inMaintenance = async () => {
try {
return await systemRepository.inMaintenance();
} catch {
return true;
}
};
return async (request: Hapi.Request, h: Hapi.ResponseToolkit): Promise<Hapi.Lifecycle.ReturnValue> => {
if (await inMaintenance()) {
return h
.response({ error: "Service Unavailable", reason: "Database not ready" })
.code(503)
.header("Retry-After", "10")
.takeover();
}
return h.continue;
};
},
name: "database-ready",
register(server: Contracts.Api.ApiServer): void {
server.ext("onRequest", this.getOnRequestHandler(server.app.app));
},
version: "1.0.0",
};