refactor: introduce env-var
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
"@types/bun": "latest"
|
||||
},
|
||||
"dependencies": {
|
||||
"env-var": "^7.5.0",
|
||||
"gotify": "^1.1.0",
|
||||
"ts3-nodejs-library": "^3.4.1",
|
||||
"winston": "^3.8.2"
|
||||
|
||||
30
src/app.ts
30
src/app.ts
@@ -2,8 +2,10 @@ import { Gotify } from "gotify";
|
||||
import { QueryProtocol, TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library"
|
||||
import { createLogger, transports, format } from "winston";
|
||||
|
||||
import { GOTIFY_TITLE, GOTIFY_TOKEN, GOTIFY_URL, LOG_LEVEL, MODE, TS3_HOST, TS3_NICKNAME, TS3_PASSWORD, TS3_QUERY_PORT, TS3_SERVER_PORT, TS3_USERNAME } from "./env";
|
||||
|
||||
const logger = createLogger({
|
||||
level: process.env.LOG_LEVEL,
|
||||
level: LOG_LEVEL,
|
||||
transports: [new transports.Console()],
|
||||
format: format.combine(
|
||||
format.colorize(),
|
||||
@@ -12,22 +14,22 @@ const logger = createLogger({
|
||||
})
|
||||
|
||||
const gotify = new Gotify({
|
||||
server: process.env.GOTIFY_URL,
|
||||
server: GOTIFY_URL,
|
||||
})
|
||||
|
||||
const gotifyConfig = {
|
||||
app: process.env.GOTIFY_TOKEN,
|
||||
title: process.env.GOTIFY_TITLE || "ts3gotify"
|
||||
app: GOTIFY_TOKEN,
|
||||
title: GOTIFY_TITLE
|
||||
}
|
||||
|
||||
function getModes() {
|
||||
const modeIsProvided = process.env.MODE != undefined
|
||||
|
||||
return {
|
||||
connect: modeIsProvided ? process.env.MODE?.includes("connect") || false : true,
|
||||
disconnect: process.env.MODE?.includes("disconnect") || false,
|
||||
moved: process.env.MODE?.includes("moved") || false,
|
||||
message: process.env.MODE?.includes("message") || false
|
||||
connect: MODE?.includes("connect") || false,
|
||||
disconnect: MODE?.includes("disconnect") || false,
|
||||
moved: MODE?.includes("moved") || false,
|
||||
message: MODE?.includes("message") || false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,13 +58,13 @@ function handleMessage(message: string) {
|
||||
}
|
||||
|
||||
TeamSpeak.connect({
|
||||
host: process.env.TS3_HOST || "info",
|
||||
queryport: process.env.TS3_QUERY_PORT || 10011,
|
||||
serverport: process.env.TS3_SERVER_PORT || 9987,
|
||||
host: TS3_HOST,
|
||||
queryport: TS3_QUERY_PORT,
|
||||
serverport: TS3_SERVER_PORT,
|
||||
protocol: QueryProtocol.RAW,
|
||||
username: process.env.TS3_USERNAME,
|
||||
password: process.env.TS3_PASSWORD,
|
||||
nickname: process.env.TS3_NICKNAME || "ts3gotify",
|
||||
username: TS3_USERNAME,
|
||||
password: TS3_PASSWORD,
|
||||
nickname: TS3_NICKNAME,
|
||||
}).then((teamspeak) => {
|
||||
const mode = getModes()
|
||||
|
||||
|
||||
51
src/env.ts
Normal file
51
src/env.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { from } from "env-var";
|
||||
|
||||
const envVar = from(process.env, {
|
||||
asLogLevel: (value) => {
|
||||
const logLevels = ["error", "info", "debug"];
|
||||
if (logLevels.includes(value)) {
|
||||
return value;
|
||||
} else {
|
||||
throw new Error("Invalid log level");
|
||||
}
|
||||
},
|
||||
asTs3GotifyMode: (value) => {
|
||||
const modes = ["connect", "disconnect", "moved", "message"];
|
||||
if (modes.includes(value)) {
|
||||
return value;
|
||||
} else {
|
||||
throw new Error("Invalid mode");
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
export const LOG_LEVEL = envVar.get("LOG_LEVEL").default("info").asLogLevel();
|
||||
|
||||
export const TS3_HOST = envVar.get("TS3_HOST").required().asString();
|
||||
export const TS3_QUERY_PORT = envVar
|
||||
.get("TS3_QUERY_PORT")
|
||||
.default(10011)
|
||||
.asPortNumber();
|
||||
export const TS3_SERVER_PORT = envVar
|
||||
.get("TS3_SERVER_PORT")
|
||||
.default(9987)
|
||||
.asPortNumber();
|
||||
export const TS3_USERNAME = envVar.get("TS3_USERNAME").required().asString();
|
||||
export const TS3_PASSWORD = envVar.get("TS3_PASSWORD").required().asString();
|
||||
export const TS3_NICKNAME = envVar
|
||||
.get("TS3_NICKNAME")
|
||||
.default("ts3gotify")
|
||||
.asString();
|
||||
|
||||
export const GOTIFY_URL = envVar.get("GOTIFY_URL").required().asUrlString();
|
||||
export const GOTIFY_TOKEN = envVar.get("GOTIFY_TOKEN").required().asString();
|
||||
export const GOTIFY_TITLE = envVar
|
||||
.get("GOTIFY_TITLE")
|
||||
.default("ts3gotify")
|
||||
.asString();
|
||||
|
||||
export const MODE = envVar
|
||||
.get("MODE")
|
||||
.default("['connect']")
|
||||
.asJsonArray()
|
||||
.map((value) => value.asTs3GotifyMode());
|
||||
19
src/environment.d.ts
vendored
19
src/environment.d.ts
vendored
@@ -1,19 +0,0 @@
|
||||
export { };
|
||||
|
||||
declare global {
|
||||
namespace NodeJS {
|
||||
interface ProcessEnv {
|
||||
LOG_LEVEL?: "error" | "info" | "debug"
|
||||
TS3_HOST: string;
|
||||
TS3_QUERY_PORT?: number;
|
||||
TS3_SERVER_PORT?: number;
|
||||
TS3_USERNAME: string;
|
||||
TS3_PASSWORD: string;
|
||||
TS3_NICKNAME?: string;
|
||||
GOTIFY_URL: string;
|
||||
GOTIFY_TOKEN: string;
|
||||
GOTIFY_TITLE?: string;
|
||||
MODE?: Array<"connect" | "disconnect" | "moved" | "message">
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user