mirror of
https://github.com/Routstr/routstrd.git
synced 2026-08-11 20:47:58 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
const HOME = process.env.HOME || process.env.USERPROFILE || "";
|
||||
|
||||
export const CONFIG_DIR = process.env.ROUTSTRD_DIR || `${HOME}/.routstrd`;
|
||||
export const SOCKET_PATH = process.env.ROUTSTRD_SOCKET || `${CONFIG_DIR}/routstrd.sock`;
|
||||
export const PID_FILE = process.env.ROUTSTRD_PID || `${CONFIG_DIR}/routstrd.pid`;
|
||||
export const DB_PATH = `${CONFIG_DIR}/routstr.db`;
|
||||
export const CONFIG_FILE = `${CONFIG_DIR}/config.json`;
|
||||
|
||||
export interface RoutstrdConfig {
|
||||
port: number;
|
||||
provider: string | null;
|
||||
cocodPath: string | null;
|
||||
}
|
||||
|
||||
export const DEFAULT_CONFIG: RoutstrdConfig = {
|
||||
port: 8008,
|
||||
provider: null,
|
||||
cocodPath: null,
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
import { appendFile, mkdir } from "fs/promises";
|
||||
import { existsSync } from "fs";
|
||||
import { join } from "path";
|
||||
|
||||
const HOME = process.env.HOME || process.env.USERPROFILE || "";
|
||||
const LOG_DIR = process.env.ROUTSTRD_DIR || `${HOME}/.routstrd`;
|
||||
const LOG_FILE = join(LOG_DIR, "routstrd.log");
|
||||
|
||||
async function ensureLogDir() {
|
||||
if (!existsSync(LOG_DIR)) {
|
||||
await mkdir(LOG_DIR, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function writeLog(level: string, ...args: unknown[]) {
|
||||
await ensureLogDir();
|
||||
const timestamp = new Date().toISOString();
|
||||
const message = args
|
||||
.map((a) => (typeof a === "object" ? JSON.stringify(a) : String(a)))
|
||||
.join(" ");
|
||||
const line = `[${timestamp}] [${level}] ${message}\n`;
|
||||
try {
|
||||
await appendFile(LOG_FILE, line);
|
||||
} catch (error) {
|
||||
console.error("Failed to write log:", error);
|
||||
}
|
||||
}
|
||||
|
||||
export const logger = {
|
||||
log: (...args: unknown[]) => {
|
||||
console.log(...args);
|
||||
writeLog("INFO", ...args);
|
||||
},
|
||||
error: (...args: unknown[]) => {
|
||||
console.error(...args);
|
||||
writeLog("ERROR", ...args);
|
||||
},
|
||||
info: (...args: unknown[]) => {
|
||||
console.log(...args);
|
||||
writeLog("INFO", ...args);
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user