|
| 1 | +/** |
| 2 | + * © Ocado Group |
| 3 | + * Created on 13/12/2024 at 12:15:05(+00:00). |
| 4 | + * |
| 5 | + * A server for an app in a live environment. |
| 6 | + * Based off: https://github.com/bluwy/create-vite-extra/blob/master/template-ssr-react-ts/server.js |
| 7 | + */ |
| 8 | + |
| 9 | +import fs from "node:fs/promises" |
| 10 | +import express from "express" |
| 11 | +import { Cache } from "memory-cache" |
| 12 | + |
| 13 | +export default class Server { |
| 14 | + constructor( |
| 15 | + /** @type {Partial<{ mode: "development" | "staging" | "production"; port: number; base: string }>} */ |
| 16 | + { mode, port, base } = {}, |
| 17 | + ) { |
| 18 | + /** @type {"development" | "staging" | "production"} */ |
| 19 | + this.mode = mode || process.env.MODE || "development" |
| 20 | + /** @type {number} */ |
| 21 | + this.port = port || (process.env.PORT ? Number(process.env.PORT) : 5173) |
| 22 | + /** @type {string} */ |
| 23 | + this.base = base || process.env.BASE || "/" |
| 24 | + |
| 25 | + /** @type {boolean} */ |
| 26 | + this.envIsProduction = process.env.NODE_ENV === "production" |
| 27 | + /** @type {string} */ |
| 28 | + this.templateHtml = "" |
| 29 | + /** @type {string} */ |
| 30 | + this.hostname = this.envIsProduction ? "0.0.0.0" : "127.0.0.1" |
| 31 | + |
| 32 | + /** @type {import('express').Express} */ |
| 33 | + this.app = express() |
| 34 | + /** @type {import('vite').ViteDevServer | undefined} */ |
| 35 | + this.vite = undefined |
| 36 | + /** @type {import('memory-cache').Cache<string, any>} */ |
| 37 | + this.cache = new Cache() |
| 38 | + |
| 39 | + /** @type {string} */ |
| 40 | + this.healthCheckCacheKey = "health-check" |
| 41 | + /** @type {number} */ |
| 42 | + this.healthCheckCacheTimeout = 30000 |
| 43 | + /** @type {Record<"healthy" | "startingUp" | "shuttingDown" | "unhealthy" | "unknown", number>} */ |
| 44 | + this.healthCheckStatusCodes = { |
| 45 | + // The app is running normally. |
| 46 | + healthy: 200, |
| 47 | + // The app is performing app-specific initialisation which must |
| 48 | + // complete before it will serve normal application requests |
| 49 | + // (perhaps the app is warming a cache or something similar). You |
| 50 | + // only need to use this status if your app will be in a start-up |
| 51 | + // mode for a prolonged period of time. |
| 52 | + startingUp: 503, |
| 53 | + // The app is shutting down. As with startingUp, you only need to |
| 54 | + // use this status if your app takes a prolonged amount of time |
| 55 | + // to shutdown, perhaps because it waits for a long-running |
| 56 | + // process to complete before shutting down. |
| 57 | + shuttingDown: 503, |
| 58 | + // The app is not running normally. |
| 59 | + unhealthy: 503, |
| 60 | + // The app is not able to report its own state. |
| 61 | + unknown: 503, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** @type {(request: import('express').Request) => { healthStatus: "healthy" | "startingUp" | "shuttingDown" | "unhealthy" | "unknown"; additionalInfo: string; details?: Array<{ name: string; description: string; health: "healthy" | "startingUp" | "shuttingDown" | "unhealthy" | "unknown" }> }} */ |
| 66 | + getHealthCheck(request) { |
| 67 | + return { |
| 68 | + healthStatus: "healthy", |
| 69 | + additionalInfo: "All healthy.", |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** @type {(request: import('express').Request, response: import('express').Response) => void} */ |
| 74 | + handleHealthCheck(request, response) { |
| 75 | + /** @type {{ appId: string; healthStatus: "healthy" | "startingUp" | "shuttingDown" | "unhealthy" | "unknown"; lastCheckedTimestamp: string; additionalInformation: string; startupTimestamp: string; appVersion: string; details: Array<{ name: string; description: string; health: "healthy" | "startingUp" | "shuttingDown" | "unhealthy" | "unknown" }> }} */ |
| 76 | + let value = this.cache.get(this.healthCheckCacheKey) |
| 77 | + if (value === null) { |
| 78 | + const healthCheck = this.getHealthCheck(request) |
| 79 | + |
| 80 | + if (healthCheck.healthStatus !== "healthy") { |
| 81 | + console.warn(`health check: ${JSON.stringify(healthCheck)}`) |
| 82 | + } |
| 83 | + |
| 84 | + value = { |
| 85 | + appId: process.env.APP_ID || "REPLACE_ME", |
| 86 | + healthStatus: healthCheck.healthStatus, |
| 87 | + lastCheckedTimestamp: new Date().toISOString(), |
| 88 | + additionalInformation: healthCheck.additionalInfo, |
| 89 | + startupTimestamp: new Date().toISOString(), |
| 90 | + appVersion: process.env.APP_VERSION || "REPLACE_ME", |
| 91 | + details: healthCheck.details || [], |
| 92 | + } |
| 93 | + |
| 94 | + this.cache.put( |
| 95 | + this.healthCheckCacheKey, |
| 96 | + value, |
| 97 | + this.healthCheckCacheTimeout, |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + response.status(this.healthCheckStatusCodes[value.healthStatus]).json(value) |
| 102 | + } |
| 103 | + |
| 104 | + /** @type {(request: import('express').Request, response: import('express').Response) => Promise<void>} */ |
| 105 | + async handleServeHtml(request, response) { |
| 106 | + try { |
| 107 | + const path = request.originalUrl.replace(this.base, "") |
| 108 | + |
| 109 | + /** @type {string} */ |
| 110 | + let template |
| 111 | + /** @type {(path: string) => Promise<{ head?: string; html?: string }>} */ |
| 112 | + let render |
| 113 | + if (this.envIsProduction) { |
| 114 | + render = (await import("../../../dist/server/entry-server.js")).render |
| 115 | + |
| 116 | + // Use cached template. |
| 117 | + template = this.templateHtml |
| 118 | + } else { |
| 119 | + render = (await this.vite.ssrLoadModule("/src/entry-server.tsx")).render |
| 120 | + |
| 121 | + // Always read fresh template. |
| 122 | + template = await fs.readFile("./index.html", "utf-8") |
| 123 | + template = await this.vite.transformIndexHtml(path, template) |
| 124 | + } |
| 125 | + |
| 126 | + const rendered = await render(path) |
| 127 | + |
| 128 | + const html = template |
| 129 | + .replace(`<!--app-head-->`, rendered.head ?? "") |
| 130 | + .replace(`<!--app-html-->`, rendered.html ?? "") |
| 131 | + |
| 132 | + response.status(200).set({ "Content-Type": "text/html" }).send(html) |
| 133 | + } catch (error) { |
| 134 | + this.vite?.ssrFixStacktrace(error) |
| 135 | + console.error(error.stack) |
| 136 | + response.status(500).end(this.envIsProduction ? undefined : error.stack) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + async run() { |
| 141 | + this.app.get("/health-check", (request, response) => { |
| 142 | + this.handleHealthCheck(request, response) |
| 143 | + }) |
| 144 | + |
| 145 | + if (this.envIsProduction) { |
| 146 | + const compression = (await import("compression")).default |
| 147 | + const sirv = (await import("sirv")).default |
| 148 | + |
| 149 | + this.templateHtml = await fs.readFile("./dist/client/index.html", "utf-8") |
| 150 | + |
| 151 | + this.app.use(compression()) |
| 152 | + this.app.use(this.base, sirv("./dist/client", { extensions: [] })) |
| 153 | + } else { |
| 154 | + const { createServer } = await import("vite") |
| 155 | + |
| 156 | + this.vite = await createServer({ |
| 157 | + server: { middlewareMode: true }, |
| 158 | + appType: "custom", |
| 159 | + base: this.base, |
| 160 | + mode: this.mode, |
| 161 | + }) |
| 162 | + |
| 163 | + this.app.use(this.vite.middlewares) |
| 164 | + } |
| 165 | + |
| 166 | + this.app.get("*", async (request, response) => { |
| 167 | + await this.handleServeHtml(request, response) |
| 168 | + }) |
| 169 | + |
| 170 | + this.app.listen(this.port, this.hostname, () => { |
| 171 | + let startMessage = |
| 172 | + "Server started.\n" + |
| 173 | + `url: http://${this.hostname}:${this.port}\n` + |
| 174 | + `environment: ${process.env.NODE_ENV}\n` |
| 175 | + |
| 176 | + if (!this.envIsProduction) startMessage += `mode: ${this.mode}\n` |
| 177 | + |
| 178 | + console.log(startMessage) |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
0 commit comments