refactor(shared): centralize configuration and types
Migrate configuration validation and core moderation types from individual services to the `@bete/shared` package to ensure consistency across the monorepo. - Move `AppConfig` and moderation-related interfaces to `packages/shared`. - Replace service-specific Zod schemas with the centralized shared configuration. - Refactor `services/backend` and `services/discord-gateway` to consume shared config and types. - Remove redundant type definitions and local configuration logic in services. - Update `packages/shared` exports to include new `config` and `moderation-types` modules. - Clean up unused files and deprecated utility functions in `packages/shared`.
This commit is contained in:
@@ -24,7 +24,6 @@
|
||||
"ioredis": "^5.11.0",
|
||||
"pg": "^8.21.0",
|
||||
"pino": "^9.6.0",
|
||||
"pino-http": "^10.3.0",
|
||||
"prom-client": "^15.1.3",
|
||||
"ws": "^8.20.1",
|
||||
"zod": "^4.4.3"
|
||||
|
||||
@@ -1,117 +1,5 @@
|
||||
import "dotenv/config";
|
||||
import { ConfigError } from "@bete/shared/errors";
|
||||
import { z } from "zod";
|
||||
import { config as sharedConfig } from "@bete/shared/config";
|
||||
|
||||
const configSchema = z
|
||||
.object({
|
||||
// Server
|
||||
WEBSERVER_PORT: z.coerce.number().positive().default(3001),
|
||||
NODE_ENV: z
|
||||
.enum(["development", "production", "test"])
|
||||
.default("development"),
|
||||
LOG_LEVEL: z
|
||||
.enum(["error", "warn", "info", "http", "verbose", "debug", "silly"])
|
||||
.default("info"),
|
||||
VERBOSE: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform((v) => v === "true")
|
||||
.default(false),
|
||||
|
||||
// Database
|
||||
DATABASE_URL: z.string().url().optional(),
|
||||
DATABASE_HOST: z.string().default("localhost"),
|
||||
DATABASE_PORT: z.coerce.number().default(5432),
|
||||
DATABASE_NAME: z.string().default("discord_moderation"),
|
||||
DATABASE_USER: z.string().default("postgres"),
|
||||
DATABASE_PASSWORD: z.string().optional(),
|
||||
|
||||
// Redis (optional, for pub/sub)
|
||||
REDIS_URL: z.string().url().optional(),
|
||||
REDIS_HOST: z.string().default("localhost"),
|
||||
REDIS_PORT: z.coerce.number().default(6379),
|
||||
|
||||
// Discord
|
||||
MONITOR_GUILD_ID: z.string().min(1).optional(),
|
||||
|
||||
// Admin
|
||||
ADMIN_PASSWORD: z.string().optional(),
|
||||
|
||||
// Analytics
|
||||
BACKLOG_SYNC_HOURS: z.coerce.number().positive().default(24),
|
||||
BACKLOG_SYNC_BATCH_SIZE: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.positive()
|
||||
.max(100)
|
||||
.default(100),
|
||||
|
||||
// AI Moderation
|
||||
AI_ANALYSIS_ENABLED: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform((v) => v === "true")
|
||||
.default(false),
|
||||
OPENAI_MODERATION_API_KEY: z.string().optional(),
|
||||
OPENAI_MODERATION_BASE_URL: z
|
||||
.string()
|
||||
.url()
|
||||
.default("https://api.openai.com/v1"),
|
||||
OPENAI_MODERATION_MODEL: z.string().default("omni-moderation-latest"),
|
||||
AI_LLM_API_KEY: z.string().optional(),
|
||||
AI_LLM_BASE_URL: z
|
||||
.string()
|
||||
.url()
|
||||
.default("https://9router.asepharyana.my.id/v1"),
|
||||
AI_LLM_MODEL: z.string().default("text"),
|
||||
AI_LLM_VISION_MODEL: z.string().optional(),
|
||||
AI_LLM_MAX_CONCURRENT: z.coerce.number().int().positive().default(5),
|
||||
AI_LLM_IMAGE_MAX_DIMENSION: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.positive()
|
||||
.default(1024),
|
||||
AI_LLM_TEXT_BATCH_SIZE: z.coerce.number().int().positive().default(20),
|
||||
AI_LLM_MEDIA_ANALYSIS_TIMEOUT_MS: z.coerce
|
||||
.number()
|
||||
.int()
|
||||
.positive()
|
||||
.default(60000),
|
||||
|
||||
// Attachments
|
||||
ATTACHMENT_UPLOAD_TIMEOUT_MS: z.coerce.number().positive().default(30000),
|
||||
ATTACHMENT_MAX_SIZE_MB: z.coerce.number().positive().default(100),
|
||||
ATTACHMENT_RETRY_ATTEMPTS: z.coerce.number().positive().default(3),
|
||||
TELE_UPLOAD_URL: z
|
||||
.string()
|
||||
.url()
|
||||
.default("https://upload.asepharyana.my.id/api/upload"),
|
||||
})
|
||||
.superRefine((value, ctx) => {
|
||||
if (!value.DATABASE_URL && !value.DATABASE_HOST) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ["DATABASE_URL"],
|
||||
message: "Either DATABASE_URL or DATABASE_HOST must be provided",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export function loadConfig(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): z.infer<typeof configSchema> {
|
||||
try {
|
||||
return configSchema.parse(env);
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
const messages = error.issues
|
||||
.map((e) => `${e.path.join(".")}: ${e.message}`)
|
||||
.join("\n");
|
||||
throw new ConfigError(`Configuration validation failed:\n${messages}`);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export const config = loadConfig();
|
||||
export const config = sharedConfig;
|
||||
export type Config = typeof config;
|
||||
|
||||
@@ -16,7 +16,7 @@ export async function initializeDatabase() {
|
||||
|
||||
const databaseUrl =
|
||||
config.DATABASE_URL ||
|
||||
`postgresql://${config.DATABASE_USER}${config.DATABASE_PASSWORD ? `:${config.DATABASE_PASSWORD}` : ""}@${config.DATABASE_HOST}:${config.DATABASE_PORT}/${config.DATABASE_NAME}`;
|
||||
`postgresql://${config.POSTGRES_USER}${config.POSTGRES_PASSWORD ? `:${config.POSTGRES_PASSWORD}` : ""}@${config.POSTGRES_HOST}:${config.POSTGRES_PORT}/${config.POSTGRES_DB}`;
|
||||
|
||||
pool = new Pool({
|
||||
connectionString: databaseUrl,
|
||||
|
||||
@@ -60,7 +60,7 @@ export function requireParam(
|
||||
name: string,
|
||||
): string {
|
||||
if (typeof value !== "string" || value.length === 0) {
|
||||
throw new Error(`Missing ${kind}: ${name}`);
|
||||
throw new ValidationError(`Missing ${kind}: ${name}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -31,18 +31,11 @@ let publisherClient: Redis | null = null;
|
||||
let subscriberClient: Redis | null = null;
|
||||
|
||||
function ensureRedisConfig(): boolean {
|
||||
return !!(config.REDIS_URL || config.REDIS_HOST);
|
||||
return !!(config.REDIS_URL);
|
||||
}
|
||||
|
||||
function createClient(): Redis {
|
||||
if (config.REDIS_URL) {
|
||||
return new Redis(config.REDIS_URL, { keyPrefix: "" });
|
||||
}
|
||||
return new Redis({
|
||||
host: config.REDIS_HOST,
|
||||
port: config.REDIS_PORT,
|
||||
keyPrefix: "",
|
||||
});
|
||||
return new Redis(config.REDIS_URL, { keyPrefix: "" });
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Global broadcast functions for WebSocket events.
|
||||
* Broadcast functions for WebSocket events.
|
||||
*
|
||||
* These are assigned by ws/server.ts when the WebSocket server initializes.
|
||||
* These are injected by ws/server.ts when the WebSocket server initializes.
|
||||
* Other modules call them to push real-time events to connected frontend clients.
|
||||
*
|
||||
* Usage:
|
||||
@@ -13,38 +13,48 @@ type BroadcastFn = (data: unknown) => void;
|
||||
type BroadcastRawFn = (type: string, data: unknown) => void;
|
||||
type BroadcastBinaryFn = (data: Buffer) => void;
|
||||
|
||||
declare global {
|
||||
// biome-ignore lint/suspicious/noAssignInExpressions: intentional global broadcast registry
|
||||
var __broadcastFns:
|
||||
| {
|
||||
messageCreated: BroadcastFn;
|
||||
messageUpdated: BroadcastFn;
|
||||
messageDeleted: BroadcastFn;
|
||||
attachmentUploaded: BroadcastFn;
|
||||
raw: BroadcastRawFn;
|
||||
binary: BroadcastBinaryFn;
|
||||
}
|
||||
| undefined;
|
||||
export interface BroadcastFunctions {
|
||||
messageCreated: BroadcastFn;
|
||||
messageUpdated: BroadcastFn;
|
||||
messageDeleted: BroadcastFn;
|
||||
attachmentUploaded: BroadcastFn;
|
||||
raw: BroadcastRawFn;
|
||||
binary: BroadcastBinaryFn;
|
||||
}
|
||||
|
||||
const noop: BroadcastFn = () => {};
|
||||
const noopRaw: BroadcastRawFn = () => {};
|
||||
const noopBinary: BroadcastBinaryFn = () => {};
|
||||
|
||||
let _fns: BroadcastFunctions | null = null;
|
||||
|
||||
/**
|
||||
* Inject broadcast functions from the WebSocket server initializer.
|
||||
* Must be called once during server startup before any broadcast is used.
|
||||
*/
|
||||
export function setBroadcastFunctions(fns: BroadcastFunctions): void {
|
||||
_fns = fns;
|
||||
}
|
||||
|
||||
/** Clear injected functions (used during cleanup). */
|
||||
export function clearBroadcastFunctions(): void {
|
||||
_fns = null;
|
||||
}
|
||||
|
||||
export const broadcastMessageCreated: BroadcastFn = (data) =>
|
||||
(globalThis.__broadcastFns?.messageCreated ?? noop)(data);
|
||||
(_fns?.messageCreated ?? noop)(data);
|
||||
|
||||
export const broadcastMessageUpdated: BroadcastFn = (data) =>
|
||||
(globalThis.__broadcastFns?.messageUpdated ?? noop)(data);
|
||||
(_fns?.messageUpdated ?? noop)(data);
|
||||
|
||||
export const broadcastMessageDeleted: BroadcastFn = (data) =>
|
||||
(globalThis.__broadcastFns?.messageDeleted ?? noop)(data);
|
||||
(_fns?.messageDeleted ?? noop)(data);
|
||||
|
||||
export const broadcastAttachmentUploaded: BroadcastFn = (data) =>
|
||||
(globalThis.__broadcastFns?.attachmentUploaded ?? noop)(data);
|
||||
(_fns?.attachmentUploaded ?? noop)(data);
|
||||
|
||||
export const broadcastRaw: BroadcastRawFn = (type, data) =>
|
||||
(globalThis.__broadcastFns?.raw ?? noopRaw)(type, data);
|
||||
(_fns?.raw ?? noopRaw)(type, data);
|
||||
|
||||
export const broadcastBinary: BroadcastBinaryFn = (data) =>
|
||||
(globalThis.__broadcastFns?.binary ?? noopBinary)(data);
|
||||
(_fns?.binary ?? noopBinary)(data);
|
||||
|
||||
@@ -32,14 +32,7 @@ const SUBSCRIPTIONS: ChannelMapping[] = [
|
||||
let subscriber: Redis | null = null;
|
||||
|
||||
function createSubscriber(): Redis {
|
||||
if (config.REDIS_URL) {
|
||||
return new Redis(config.REDIS_URL, { keyPrefix: "" });
|
||||
}
|
||||
return new Redis({
|
||||
host: config.REDIS_HOST,
|
||||
port: config.REDIS_PORT,
|
||||
keyPrefix: "",
|
||||
});
|
||||
return new Redis(config.REDIS_URL, { keyPrefix: "" });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,7 +86,7 @@ function handleSubscriptionMessage(channel: string, message: string): void {
|
||||
}
|
||||
|
||||
export async function startRedisBridge(): Promise<void> {
|
||||
if (!config.REDIS_URL && !config.REDIS_HOST) {
|
||||
if (!config.REDIS_URL) {
|
||||
logger.info("Redis not configured, skipping Redis bridge");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { Server } from "node:http";
|
||||
import { createChildLogger } from "@bete/shared/logger";
|
||||
import { WebSocket, WebSocketServer } from "ws";
|
||||
import { setBroadcastFunctions } from "./broadcast.js";
|
||||
|
||||
const logger = createChildLogger("ws.server");
|
||||
|
||||
@@ -149,7 +150,7 @@ export function createWebSocketServer(server: Server): WebSocketServer {
|
||||
// Don't let the interval keep the process alive after wss closes
|
||||
heartbeatInterval.unref();
|
||||
|
||||
// Expose broadcast functions on globalThis
|
||||
// Defines broadcast functions and injects them via setBroadcastFunctions
|
||||
function broadcast(event: Omit<BroadcastEvent, "timestamp">) {
|
||||
const payload = JSON.stringify({
|
||||
...event,
|
||||
@@ -166,7 +167,7 @@ export function createWebSocketServer(server: Server): WebSocketServer {
|
||||
}
|
||||
}
|
||||
|
||||
function broadcastRaw(data: Buffer) {
|
||||
function broadcastBinary(data: Buffer) {
|
||||
for (const client of clients) {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
try {
|
||||
@@ -178,7 +179,7 @@ export function createWebSocketServer(server: Server): WebSocketServer {
|
||||
}
|
||||
}
|
||||
|
||||
globalThis.__broadcastFns = {
|
||||
setBroadcastFunctions({
|
||||
messageCreated: (data: unknown) =>
|
||||
broadcast({ type: "message_created", data }),
|
||||
messageUpdated: (data: unknown) =>
|
||||
@@ -188,13 +189,12 @@ export function createWebSocketServer(server: Server): WebSocketServer {
|
||||
attachmentUploaded: (data: unknown) =>
|
||||
broadcast({ type: "attachment_uploaded", data }),
|
||||
raw: (type: string, data: unknown) => broadcast({ type, data }),
|
||||
binary: broadcastRaw,
|
||||
};
|
||||
binary: broadcastBinary,
|
||||
});
|
||||
|
||||
// Cleanup on close
|
||||
wss.on("close", () => {
|
||||
clearInterval(heartbeatInterval);
|
||||
globalThis.__broadcastFns = undefined;
|
||||
});
|
||||
|
||||
logger.info({ path: "/ws" }, "WebSocket server created");
|
||||
|
||||
Reference in New Issue
Block a user