Explorar el Código

ref: load api version through env var

Sv443 hace 2 meses
padre
commit
dfc1e151e3
Se han modificado 3 ficheros con 16 adiciones y 13 borrados
  1. 4 0
      src/constants.ts
  2. 7 12
      src/server.ts
  3. 5 1
      test/consts.ts

+ 4 - 0
src/constants.ts

@@ -3,6 +3,7 @@ import type { IRateLimiterOptions } from "rate-limiter-flexible";
 import type { axios } from "@src/axios.js";
 import type { ResponseFormat } from "@src/types.js";
 import packageJson from "@root/package.json" with { type: "json" };
+import { getEnvVar } from "./env.js";
 
 // for @linkcode in tsdoc comments
 void [{} as typeof axios];
@@ -44,6 +45,9 @@ export const splitVersion = packageJson.version.split(".").map(v => Number(v)) a
 /** Major, minor, and patch version numbers */
 export const [verMajor, verMinor, verPatch] = splitVersion;
 
+/** Version identifier for the API, used in the base URL */
+export const apiVersion = getEnvVar("API_VERSION", "string", `v${verMajor}`);
+
 /** Map of response formats and their corresponding MIME types */
 export const mimeTypeMap = {
   json: "application/json",

+ 7 - 12
src/server.ts

@@ -6,7 +6,6 @@ import { RateLimiterMemory, RateLimiterRes } from "rate-limiter-flexible";
 import k from "kleur";
 import cors from "cors";
 import { getClientIp } from "request-ip";
-
 import { error } from "@src/error.js";
 import { hashStr, respond } from "@src/utils.js";
 import { envVarEquals, getEnvVar } from "@src/env.js";
@@ -125,23 +124,19 @@ export async function init() {
   });
 
   const listener = app.listen(port, host, () => {
-    registerRoutes();
+    try {
+      initRouter(app);
 
-    console.log(k.green(`geniURL is listening on ${host}:${port}\n`));
+      console.log(k.green(`geniURL was successfully started on ${k.blue(k.underline(`http://127.0.0.1:${port}`))}\n`));
+    }
+    catch(err) {
+      error("Error while initializing router", err instanceof Error ? err : undefined, true);
+    }
   });
 
   listener.on("error", (err) => error("General server error", err, true));
 }
 
-function registerRoutes() {
-  try {
-    initRouter(app);
-  }
-  catch(err) {
-    error("Error while initializing router", err instanceof Error ? err : undefined, true);
-  }
-}
-
 /** Sets all rate-limiting related headers on a response given a RateLimiterRes object */
 function setRateLimitHeaders(res: Response, rateLimiterRes: RateLimiterRes) {
   if(rateLimiterRes.remainingPoints === 0)

+ 5 - 1
test/consts.ts

@@ -1,6 +1,10 @@
 import "dotenv/config";
 
-export const baseUrl = `http://127.0.0.1:${process.env.HTTP_PORT}/v2`;
+/** API version to use in the base URL */
+export const apiVersion = process.env.API_VERSION ?? "v2";
+
+/** Base URL for the locally hosted API */
+export const baseUrl = `http://127.0.0.1:${process.env.HTTP_PORT}/${apiVersion}`;
 
 /** Max results that can be returned by geniURL - should be consistent with `maxResultsAmt` in `src/constants.ts` */
 export const maxResultsAmt = 10;