From fd93175e1ae7b89f2079c2e6bb901101617686a5 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Tue, 22 Mar 2022 19:15:48 +0000 Subject: [PATCH] Move PORT from confjson5 to env --- bot/example/conf.json5 | 1 - bot/example/example.env | 3 ++- bot/src/config.ts | 7 +++++-- bot/src/main.ts | 4 ++-- bot/src/server/server.ts | 4 ++-- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/bot/example/conf.json5 b/bot/example/conf.json5 index ee38eee6c..13756f443 100644 --- a/bot/example/conf.json5 +++ b/bot/example/conf.json5 @@ -5,7 +5,6 @@ TACHI_SERVER_LOCATION: "https://tachi.example.com", HTTP_SERVER: { URL: "https://tachi-bot.example.com", - PORT: 8000, }, OAUTH: { CLIENT_SECRET: "CSfoobar", diff --git a/bot/example/example.env b/bot/example/example.env index c7c7311d9..fd4325086 100644 --- a/bot/example/example.env +++ b/bot/example/example.env @@ -1,2 +1,3 @@ MONGO_URL="127.0.0.1:27017/tachi_bot_db" -NODE_ENV="dev" \ No newline at end of file +NODE_ENV="dev" +PORT=8000 \ No newline at end of file diff --git a/bot/src/config.ts b/bot/src/config.ts index d45b10de3..a1bdaf66e 100644 --- a/bot/src/config.ts +++ b/bot/src/config.ts @@ -23,7 +23,6 @@ export interface BotConfig { TACHI_SERVER_LOCATION: string; HTTP_SERVER: { URL: string; - PORT: integer; }; OAUTH: { CLIENT_SECRET: string; @@ -53,7 +52,6 @@ function ParseBotConfig(fileLoc = "conf.json5"): BotConfig { TACHI_SERVER_LOCATION: "string", HTTP_SERVER: { URL: "string", - PORT: p.isPositiveNonZeroInteger, }, OAUTH: { CLIENT_SECRET: "string", @@ -95,6 +93,7 @@ function ParseBotConfig(fileLoc = "conf.json5"): BotConfig { export interface ProcessEnvironment { nodeEnv: "production" | "dev" | "staging" | "test"; mongoUrl: string; + port: integer; } function ParseEnvVars() { @@ -107,6 +106,9 @@ function ParseEnvVars() { p.isIn("debug", "verbose", "info", "warn", "error", "severe", "crit") ), MONGO_URL: "string", + PORT: (self) => + p.isPositiveInteger(Number(self)) || + "Should be a string representing a whole integer port.", }, {}, { allowExcessKeys: true } @@ -121,6 +123,7 @@ function ParseEnvVars() { return { nodeEnv: process.env.NODE_ENV, mongoUrl: process.env.MONGO_URL, + port: Number(process.env.PORT), } as ProcessEnvironment; } diff --git a/bot/src/main.ts b/bot/src/main.ts index 77d73fe03..571ae9291 100644 --- a/bot/src/main.ts +++ b/bot/src/main.ts @@ -1,5 +1,5 @@ import { Client, CommandInteraction, Intents, SelectMenuInteraction } from "discord.js"; -import { BotConfig, ServerConfig } from "./config"; +import { BotConfig, ProcessEnv, ServerConfig } from "./config"; import { LoggerLayers, METALLIC_MIND_SPLASHES } from "./data/data"; import { GetUserAndTokenForDiscordID } from "./database/queries"; import { handleIsCommand } from "./interactionHandlers/handleIsCommand"; @@ -74,7 +74,7 @@ We've sent you a DM with instructions on how to link your account.` logger.info(`Logged in successfully to ${client.guilds.cache.size} guilds.`); // Mount our express server. - app.listen(BotConfig.HTTP_SERVER.PORT); + app.listen(ProcessEnv.port); logger.info( `Invite URL: https://discord.com/api/oauth2/authorize?client_id=${ diff --git a/bot/src/server/server.ts b/bot/src/server/server.ts index b8bacd09c..e073541b6 100644 --- a/bot/src/server/server.ts +++ b/bot/src/server/server.ts @@ -1,7 +1,7 @@ import path from "path"; import express, { Express } from "express"; import { APITokenDocument, PublicUserDocument, WebhookEvents } from "tachi-common"; -import { BotConfig } from "../config"; +import { BotConfig, ProcessEnv } from "../config"; import { LoggerLayers } from "../data/data"; import db from "../database/mongo"; import { TachiServerV1Request, RequestTypes, TachiServerV1Get } from "../utils/fetchTachi"; @@ -214,4 +214,4 @@ const MainExpressErrorHandler: express.ErrorRequestHandler = (err, req, res, _ne app.use(MainExpressErrorHandler); -logger.info(`Starting express server on port ${BotConfig.HTTP_SERVER.PORT}.`); +logger.info(`Starting express server on port ${ProcessEnv.port}.`);