|
| 1 | +// deno-lint-ignore-file no-explicit-any |
| 2 | +import { yelixClientLog } from "@/src/utils/logging.ts"; |
| 3 | +import type { Logger } from "./Logger.ts"; |
| 4 | +import version from "@/version.ts"; |
| 5 | +import type { Yelix } from "@/src/core/Yelix.ts"; |
| 6 | + |
| 7 | +export class ServerManager { |
| 8 | + private yelix: Yelix; |
| 9 | + private server: any; |
| 10 | + private sigintListener: any; |
| 11 | + private servedInformations: { title: string; description: string }[] = []; |
| 12 | + private logger: Logger; |
| 13 | + |
| 14 | + constructor(yelix: Yelix, logger: Logger) { |
| 15 | + this.yelix = yelix; |
| 16 | + this.logger = logger; |
| 17 | + } |
| 18 | + |
| 19 | + private addLocalInformationToInitiate(addr: any) { |
| 20 | + const hostname = addr.hostname; |
| 21 | + const isLocalhost = hostname === "0.0.0.0"; |
| 22 | + const port = addr.port; |
| 23 | + const addrStr = isLocalhost |
| 24 | + ? `http://localhost:${port}` |
| 25 | + : `http://${hostname}:${port}`; |
| 26 | + |
| 27 | + this.servedInformations.unshift({ |
| 28 | + title: "Local", |
| 29 | + description: addrStr, |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + onListen(addr: any) { |
| 34 | + this.addLocalInformationToInitiate(addr); |
| 35 | + |
| 36 | + const packageVersion = version; |
| 37 | + |
| 38 | + if (this.yelix.isFirstServe) { |
| 39 | + this.logger.clientLog(); |
| 40 | + this.logger.clientLog( |
| 41 | + " %c 𝕐 Yelix %c" + packageVersion, |
| 42 | + "color: orange;", |
| 43 | + "color: inherit", |
| 44 | + ); |
| 45 | + const maxLength = Math.max( |
| 46 | + ...this.servedInformations.map((i) => i.title.length), |
| 47 | + ); |
| 48 | + this.servedInformations.forEach((info) => { |
| 49 | + this.logger.clientLog( |
| 50 | + ` - ${info.title.padEnd(maxLength)}: ${info.description}`, |
| 51 | + ); |
| 52 | + }); |
| 53 | + this.logger.clientLog(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + addServedInformation(info: { title: string; description: string }) { |
| 58 | + this.servedInformations.push(info); |
| 59 | + } |
| 60 | + |
| 61 | + startServer(port: number, appFetch: any) { |
| 62 | + this.sigintListener = () => { |
| 63 | + yelixClientLog("interrupted!"); |
| 64 | + this.kill(); |
| 65 | + Deno.exit(); |
| 66 | + }; |
| 67 | + |
| 68 | + Deno.addSignalListener("SIGINT", this.sigintListener); |
| 69 | + |
| 70 | + return new Promise<void>((resolve) => { |
| 71 | + this.server = Deno.serve( |
| 72 | + { port, onListen: (addr: any) => this.onListen(addr) }, |
| 73 | + appFetch, |
| 74 | + ); |
| 75 | + resolve(); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + async kill(forceAfterMs = 3000) { |
| 80 | + if (this.server) { |
| 81 | + try { |
| 82 | + let timeoutId = 0; |
| 83 | + const timeoutPromise = new Promise<void>((resolve) => { |
| 84 | + timeoutId = setTimeout(() => { |
| 85 | + this.logger.warn("Server shutdown timed out, forcing close"); |
| 86 | + resolve(); |
| 87 | + }, forceAfterMs); |
| 88 | + }); |
| 89 | + |
| 90 | + const shutdownPromise = this.server.shutdown(); |
| 91 | + |
| 92 | + // Race between normal shutdown and timeout |
| 93 | + await Promise.race([shutdownPromise, timeoutPromise]); |
| 94 | + clearTimeout(timeoutId); |
| 95 | + |
| 96 | + Deno.removeSignalListener("SIGINT", this.sigintListener); |
| 97 | + |
| 98 | + // Clear the reference to prevent any lingering issues |
| 99 | + this.server = null; |
| 100 | + } catch (error) { |
| 101 | + this.logger.warn("Error during server shutdown:", error); |
| 102 | + Deno.removeSignalListener("SIGINT", this.sigintListener); |
| 103 | + this.server = null; |
| 104 | + } |
| 105 | + } else { |
| 106 | + yelixClientLog( |
| 107 | + "You tried to kill the server but it was not running. This is fine.", |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments