customer/src/index.ts
2026-11-15 14:51:57 +05:00

81 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { initSentry, flushSentry } from "./sentry.ts";
import { buildApp } from "./app.ts";
import { createPool } from "./db/db.ts";
import { Keyring } from "./modules/privacy/crypto.ts";
import { createProdamus } from "./modules/billing/prodamus.ts";
import { createAnthropicLlm } from "./modules/audits/llm.ts";
import { createTelegramClient } from "./modules/trainer/telegram.ts";
import { readFileSync } from "node:fs";
import { parsePort } from "./config.ts";
async function main(): Promise<void> {
let port: number;
try {
port = parsePort(process.env.PORT);
} catch (err) {
console.error((err as Error).message);
process.exit(1);
}
let keys: Keyring | undefined;
try {
keys = Keyring.fromEnv(process.env);
} catch (err) {
console.error((err as Error).message);
process.exit(1);
}
initSentry();
const databaseUrl = process.env.DATABASE_URL;
const db = databaseUrl ? createPool(databaseUrl) : undefined;
const { PRODAMUS_SECRET_KEY: secretKey, PRODAMUS_PAY_URL: payUrl } = process.env;
const { ANTHROPIC_API_KEY: llmKey, REPORT_FONT_PATH: fontPath } = process.env;
const { TELEGRAM_BOT_TOKEN: tgToken, TELEGRAM_WEBHOOK_SECRET: tgSecret, TELEGRAM_BOT_USERNAME: tgBot } = process.env;
const telegram = tgToken && tgSecret && tgBot ? { client: createTelegramClient({ token: tgToken }), webhookSecret: tgSecret, botUsername: tgBot } : undefined;
const app = await buildApp({
db,
privacy: { keys },
billing: secretKey && payUrl ? { provider: createProdamus({ secretKey, payUrl }), publicBaseUrl: process.env.PUBLIC_BASE_URL } : undefined,
kits: process.env.KITS_LINK_SECRET ? { linkSecret: process.env.KITS_LINK_SECRET, adminToken: process.env.KITS_ADMIN_TOKEN } : undefined,
audits:
llmKey && fontPath
? { llm: createAnthropicLlm({ apiKey: llmKey, model: process.env.AUDIT_LLM_MODEL }), fontData: readFileSync(fontPath) }
: undefined,
trainer: llmKey
? { llm: createAnthropicLlm({ apiKey: llmKey, model: process.env.TRAINER_LLM_MODEL ?? process.env.AUDIT_LLM_MODEL }), telegram }
: undefined,
hr: fontPath ? { fontData: readFileSync(fontPath) } : undefined,
logger: {
level: process.env.LOG_LEVEL ?? "info",
// В логах — только метод и путь: без IP клиента, query и заголовков (ПДн).
serializers: {
req: (req: { method: string; url: string }) => ({ method: req.method, path: req.url.split("?")[0] }),
},
},
});
const shutdown = async (signal: string): Promise<void> => {
app.log.info({ signal }, "shutting down");
await app.close();
await db?.close();
await flushSentry();
process.exit(0);
};
process.once("SIGTERM", () => void shutdown("SIGTERM"));
process.once("SIGINT", () => void shutdown("SIGINT"));
await app.listen({ port, host: "0.0.0.0" });
const publicUrl = process.env.PUBLIC_BASE_URL;
if (telegram && publicUrl) {
// Без токена в логах: ошибка TelegramError содержит только метод и статус.
await telegram.client
.setWebhook(`${publicUrl}/api/trainer/telegram/webhook`, telegram.webhookSecret)
.catch((err: unknown) => app.log.warn({ err: (err as Error).message }, "telegram setWebhook failed"));
}
}
main().catch((err: unknown) => {
console.error("Failed to start:", err instanceof Error ? err.message : err);
process.exit(1);
});