mirror of
https://github.com/kirameki-cafe/Yumi.git
synced 2026-09-13 18:59:19 +00:00
More env checks
This commit is contained in:
@@ -1,24 +1,54 @@
|
||||
import * as path from 'path';
|
||||
import * as dotenv from 'dotenv';
|
||||
import validator from 'validator';
|
||||
|
||||
import Logger from '../libs/Logger';
|
||||
|
||||
const requiredENV = ['NODE_ENV', 'DATABASE_URL', 'DISCORD_TOKEN', 'PRIVATE_BOT', 'OSU_API_KEY'];
|
||||
const requiredENV = ['NODE_ENV', 'DATABASE_URL', 'DISCORD_TOKEN', 'PRIVATE_BOT'];
|
||||
|
||||
class Environment {
|
||||
public init(): void {
|
||||
dotenv.config({ path: path.resolve(__dirname, '../../.env') });
|
||||
|
||||
for (let param of requiredENV) {
|
||||
if (this.isUndefinedOrEmpty(process.env[param]))
|
||||
throw new Error(`.env ${param} is undefined`);
|
||||
if (this.isUndefinedOrEmpty(process.env[param])) throw new Error(`.env ${param} is undefined`);
|
||||
}
|
||||
|
||||
// NODE_ENV Checks
|
||||
if (this.get().NODE_ENV != 'production' && this.get().NODE_ENV != 'development')
|
||||
throw new Error('.env NODE_ENV must be either "production" or "development"');
|
||||
|
||||
// TODO: Discord token check
|
||||
// DISCORD_TOKEN Checks
|
||||
if (this.get().DISCORD_TOKEN.length != 59)
|
||||
throw new Error('.env DISCORD_TOKEN is not a valid discord bot token');
|
||||
|
||||
// DEVELOPER_IDS Checks
|
||||
if (this.get().DEVELOPER_IDS) {
|
||||
let developerIds = this.get().DEVELOPER_IDS.split(',');
|
||||
for (let id of developerIds) {
|
||||
if (isNaN(parseInt(id))) throw new Error(`.env DEVELOPER_IDS contains an invalid id: ${id}`);
|
||||
}
|
||||
}
|
||||
|
||||
// PRIVATE_BOT Checks
|
||||
if (this.get().PRIVATE_BOT.toLowerCase() != 'true' && this.get().PRIVATE_BOT.toLowerCase() != 'false')
|
||||
throw new Error('.env PRIVATE_BOT must be either "true" or "false"');
|
||||
|
||||
// SUPPORT_URL Checks
|
||||
if (this.get().SUPPORT_URL && !validator.isURL(this.get().SUPPORT_URL))
|
||||
throw new Error('.env SUPPORT_URL is not a valid URL');
|
||||
|
||||
// WEB_HOST Checks
|
||||
if (this.get().WEB_HOST && this.get().WEB_HOST.toLowerCase() != 'localhost' && !validator.isIP(this.get().WEB_HOST))
|
||||
throw new Error('.env WEB_HOST must be either localhost or a valid IP address');
|
||||
|
||||
// WEB_PORT Checks
|
||||
if (this.get().WEB_PORT && !validator.isPort(this.get().WEB_PORT))
|
||||
throw new Error('.env WEB_PORT must be a valid port number');
|
||||
|
||||
// YOUTUBE_COOKIE_BASE64 Checks
|
||||
if (this.get().YOUTUBE_COOKIE_BASE64 && !validator.isBase64(this.get().YOUTUBE_COOKIE_BASE64))
|
||||
throw new Error('.env YOUTUBE_COOKIE_BASE64 must be a valid base64 string');
|
||||
|
||||
Logger.log('info', `Running in ${process.env.NODE_ENV} environment`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user