File size: 1,186 Bytes
6b3405c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { PreviewServer, ViteDevServer } from "vite";
import { getSearchesSinceLastRestart } from "./searchesSinceLastRestart";
import { getVerifiedTokensAmount } from "./verifiedTokens";
import prettyMilliseconds from "pretty-ms";

const serverStartTime = new Date().getTime();

export function statusEndpointServerHook<
  T extends ViteDevServer | PreviewServer,
>(server: T) {
  server.middlewares.use(async (request, response, next) => {
    if (!request.url.startsWith("/status")) return next();

    const sessions = getVerifiedTokensAmount();
    const searches = getSearchesSinceLastRestart();
    const averageSearchesPerSession = searches / sessions || 0;

    const status = {
      uptime: prettyMilliseconds(new Date().getTime() - serverStartTime, {
        verbose: true,
      }),
      sessions,
      searches,
      averageSearchesPerSession,
      build: {
        timestamp: new Date(
          server.config.define.VITE_BUILD_DATE_TIME,
        ).toISOString(),
        gitCommit: JSON.parse(server.config.define.VITE_COMMIT_SHORT_HASH),
      },
    };

    response.setHeader("Content-Type", "application/json");

    response.end(JSON.stringify(status));
  });
}