Base utilities, add README, Redis, Discord integration, db keygroup planning
This commit is contained in:
110
src/config.ts
Normal file
110
src/config.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import Logging from "@proxnet/undead-logging";
|
||||
import * as fs from "node:fs";
|
||||
import process from "node:process";
|
||||
|
||||
const log = new Logging("Config");
|
||||
|
||||
type RedisConfiguration = {
|
||||
host: string,
|
||||
port: number,
|
||||
username: string,
|
||||
password: string,
|
||||
db: number
|
||||
}
|
||||
|
||||
type WebConfiguration = {
|
||||
port: number,
|
||||
host: string
|
||||
}
|
||||
|
||||
type PublicConfiguration = {
|
||||
serverName: string,
|
||||
owner: string,
|
||||
motd: string
|
||||
}
|
||||
|
||||
type LoggingConfiguration = {
|
||||
debug: boolean,
|
||||
network: boolean
|
||||
}
|
||||
|
||||
type DiscordConfiguration = {
|
||||
token: string,
|
||||
clientId: string,
|
||||
guildId: string
|
||||
}
|
||||
|
||||
type GalvanicConfiguration = {
|
||||
redis: RedisConfiguration,
|
||||
web: WebConfiguration,
|
||||
public: PublicConfiguration,
|
||||
logging: LoggingConfiguration,
|
||||
discord: DiscordConfiguration
|
||||
}
|
||||
|
||||
const defaultConfig: GalvanicConfiguration = {
|
||||
redis: {
|
||||
host: "127.0.0.1",
|
||||
port: 6379,
|
||||
username: "",
|
||||
password: "",
|
||||
db: 0
|
||||
},
|
||||
web: {
|
||||
port: 3000,
|
||||
host: "127.0.0.1"
|
||||
},
|
||||
public: {
|
||||
serverName: "Galvanic Corrosion",
|
||||
owner: "John Doe",
|
||||
motd: "The narwhal bacons at midnight"
|
||||
},
|
||||
logging: {
|
||||
debug: false,
|
||||
network: false
|
||||
},
|
||||
discord: {
|
||||
token: "replace-me",
|
||||
guildId: "replace-me",
|
||||
clientId: "replace-me"
|
||||
}
|
||||
}
|
||||
|
||||
/** The current configuration. Read and parsed only during startup. */
|
||||
let config: GalvanicConfiguration | undefined;
|
||||
try {
|
||||
if (!configurationExists()) generateDefaultConfig();
|
||||
config = JSON.parse(fs.readFileSync('./config.json').toString());
|
||||
} catch (err) {
|
||||
log.e(`Could not get config: ${err}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks for a certain file in the current directory that shouldn't exist on the first run.
|
||||
* Returns `false` when GC has ran at least once
|
||||
*/
|
||||
export function firstRun() {
|
||||
if (!fs.existsSync('./firstrun')) return true;
|
||||
else {
|
||||
fs.writeFile('./firstrun', "", () => {});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Does the configuration file exist on the disk? */
|
||||
export function configurationExists() {
|
||||
return fs.existsSync('./config.json');
|
||||
}
|
||||
|
||||
/** Place the default configuration in the current directory. */
|
||||
export function generateDefaultConfig() {
|
||||
fs.writeFileSync('./config.json', JSON.stringify(defaultConfig, undefined, ' '));
|
||||
}
|
||||
|
||||
/** Get current server configuration */
|
||||
export function getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
export * as Config from './config.ts';
|
||||
Reference in New Issue
Block a user