Skip to content

Commit

Permalink
Config: Refactor config into proper object.
Browse files Browse the repository at this point in the history
Secrets are now separate from config, and are located in the file
`src/utils/secrets.ts`.
  • Loading branch information
filiphsps committed Sep 1, 2023
1 parent 29e91de commit 48a333b
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 25 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
DISCORD_TOKEN=

# OPTIONAL - Log Level
# Defaults to `info`
# Defaults to `info` in production, `debug` in development
LOG_LEVEL=

# OPTIONAL - API Port
# Defaults to 80 in production, 3000 in development
PORT=
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Express } from 'express';
import type { Logger } from 'tslog';
import type Nordy from './nordy';
import type Nordy from '@/nordy';
import { Server } from 'http';
import express from 'express';

Expand Down
4 changes: 2 additions & 2 deletions src/commands/about-command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SlashCommandBuilder } from 'discord.js';
import { Command } from './command';
import type { CommandConstructorProps, CommandHandleCommandProps } from './command';
import { Command } from '@/commands/command';
import type { CommandConstructorProps, CommandHandleCommandProps } from '@/commands/command';
import type { Logger } from 'tslog';

export class AboutCommand extends Command {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/debug-command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SlashCommandBuilder } from 'discord.js';
import { Command } from '@/commands/command';
import type { CommandConstructorProps, CommandHandleCommandProps } from '@/commands/command';
import { PRODUCTION } from '@/utils/config';
import { config } from '@/utils/config';
import type { Logger } from 'tslog';

export class DebugCommand extends Command {
Expand All @@ -15,7 +15,7 @@ export class DebugCommand extends Command {
}

override enabled() {
return !PRODUCTION;
return !config.production;
}

override data() {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './about-command';
export * from './debug-command';
export * from '@/commands/about-command';
export * from '@/commands/debug-command';
2 changes: 1 addition & 1 deletion src/handlers/command-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Commands from '../commands/index';
import * as Commands from '@/commands';

import type { HandlerConstructorProps, HandlerHandleProps, HandlerRegisterProps } from './handler';

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { CommandHandler } from './command-handler';
import { CommandHandler } from '@/handlers/command-handler';

export { CommandHandler };
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'dotenv/config';

import API from './api';
import { HTTP_PORT } from './utils/config';
import API from '@/api';
import Logger from '@/utils/logger';
import Nordy from './nordy';
import Nordy from '@/nordy';
import { config } from '@/utils/config';

const nordy = new Nordy({ logger: Logger.getSubLogger({ name: Nordy.name }) });
await nordy.initializeHandlers();

const dashboard = new API({ logger: Logger.getSubLogger({ name: API.name }), nordy });
await dashboard.start({ port: HTTP_PORT });
await dashboard.start({ port: config.api.port });

await nordy.login();

Expand Down
2 changes: 1 addition & 1 deletion src/nordy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Handlers from '@/handlers';

import Discord, { ActivityType, Client, Events, GatewayIntentBits, Partials } from 'discord.js';

import { DISCORD_TOKEN } from '@/utils/config';
import { DISCORD_TOKEN } from '@/utils/secrets';
import type { Interaction } from 'discord.js';
import type { Handler } from '@/handlers/handler';
import type { Logger } from 'tslog';
Expand Down
16 changes: 12 additions & 4 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
export const HTTP_PORT = Number.parseInt(process.env.PORT || '3000');
export const PRODUCTION = process.env.NODE_ENV === 'production';
export const DISCORD_TOKEN = process.env.DISCORD_TOKEN;
export const LOG_LEVEL = process.env.LOG_LEVEL || 'info';
import type { LogLevel } from './logger';

const production = process.env.NODE_ENV === 'production';

export const config = {
production: production,
log_level: (process.env.LOG_LEVEL || (production && 'info') || 'debug') as LogLevel,

api: {
port: Number.parseInt(process.env.PORT || (production && '80') || '3000')
}
};
37 changes: 31 additions & 6 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { LOG_LEVEL, PRODUCTION } from './config';

import { Logger } from 'tslog';
import { config } from '@/utils/config';

export default new Logger({
type: (PRODUCTION && 'json') || 'pretty',
minLevel: 0,
const prettyConfig = {
prettyLogTemplate: '[{{hh}}:{{MM}}:{{ss}}:{{ms}} {{logLevelName}}]\t[{{name}}]: ',
prettyErrorTemplate: '\n{{errorName}} {{errorMessage}}\nerror stack:\n{{errorStack}}',
prettyErrorStackTemplate: ' • {{fileName}}\t{{method}}\n\t{{filePathWithLine}}',
Expand All @@ -31,5 +28,33 @@ export default new Logger({
errorName: ['bold', 'bgRedBright', 'whiteBright'],
fileName: ['yellow']
}
//minLevel: LOG_LEVEL
};

export type LogLevel = 'silly' | 'tracer' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
export const LogLevelToNumeric = (level: LogLevel) => {
switch (level) {
case 'silly':
return 0;
case 'tracer':
return 1;
case 'debug':
return 2;
case 'warn':
return 4;
case 'error':
return 5;
case 'fatal':
return 6;

case 'info':
default:
return 3;
}
};

export default new Logger({
type: (config.production && 'json') || 'pretty',
minLevel: LogLevelToNumeric(config.log_level),

...(((config.production && {}) || prettyConfig) as any)
});
4 changes: 4 additions & 0 deletions src/utils/secrets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const DISCORD_TOKEN = process.env.DISCORD_TOKEN;
if (!DISCORD_TOKEN) throw new Error('DISCORD_TOKEN is not set');

export { DISCORD_TOKEN };
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"rootDir": "./src/",
"baseUrl": ".",
"paths": {
"@/nordy": ["src/nordy"],
"@/api": ["src/api"],
"@/utils/*": ["src/utils/*"],
"@/handlers/*": ["src/handlers/*"],
"@/handlers": ["src/handlers/index"],
Expand Down

0 comments on commit 48a333b

Please sign in to comment.