freetravel/src/index.ts

90 lines
4.3 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 "./instrument.js";
import * as Sentry from "@sentry/node";
import { OwnerAuth } from "./auth/session.js";
import { Notifier, type TelegramProblem } from "./notify/notifier.js";
import { JsonSettingsStore } from "./notify/settingsStore.js";
import { BotApiTelegramClient, MockTelegramClient, type TelegramClient } from "./notify/telegram.js";
import { ClaudeSearchProvider } from "./search/claudeSearchProvider.js";
import { mockSearchProvider } from "./search/mockSearchProvider.js";
import type { SearchProvider } from "./search/provider.js";
import { SearchService } from "./search/searchService.js";
import { buildServer } from "./server.js";
import { emptyForumCatalog } from "./domain/forum.js";
import { emptyTrainingCatalog } from "./domain/training.js";
import { ForumSearch } from "./forums/forumSearch.js";
import { forumSearchConfig } from "./search/forumSearchConfig.js";
import { mockForumProvider } from "./search/mockForumProvider.js";
import { mockTrainingProvider } from "./search/mockTrainingProvider.js";
import { trainingSearchConfig } from "./search/trainingSearchConfig.js";
import { JsonCatalogStore, JsonFileStore } from "./store/catalogStore.js";
import { TrainingSearch } from "./training/trainingSearch.js";
const useMock = process.env.MOCK_SEARCH === "1" || !process.env.ANTHROPIC_API_KEY;
const provider: SearchProvider = useMock ? mockSearchProvider({ delayMs: 1500 }) : new ClaudeSearchProvider();
const store = new JsonCatalogStore(process.env.DATA_DIR ?? "./data");
const search = new SearchService({ store, provider, onError: (error) => Sentry.captureException(error) });
let telegram: TelegramClient = new MockTelegramClient();
let telegramProblem: TelegramProblem = "no_token";
if (process.env.TELEGRAM_BOT_TOKEN) {
// Имя бота берём у самого Телеграма; TELEGRAM_BOT_USERNAME — лишь запасной вариант.
const bot = new BotApiTelegramClient(process.env.TELEGRAM_BOT_TOKEN, process.env.TELEGRAM_BOT_USERNAME ?? "");
telegramProblem = (await bot.resolveUsername()) ? null : "bad_token";
telegram = bot;
}
if (telegramProblem === "no_token") console.warn("Telegram: нет TELEGRAM_BOT_TOKEN в окружении — бот работает как пример");
if (telegramProblem === "bad_token") console.error("Telegram: Телеграм не принял TELEGRAM_BOT_TOKEN (getMe)");
const settings = new JsonSettingsStore(process.env.DATA_DIR ?? "./data");
const trainingStore = new JsonFileStore(process.env.DATA_DIR ?? "./data", "trainings.json", emptyTrainingCatalog);
const trainingProvider: SearchProvider = useMock
? mockTrainingProvider({ delayMs: 1500 })
: new ClaudeSearchProvider(undefined, trainingSearchConfig);
const trainings = new TrainingSearch({
store: trainingStore,
provider: trainingProvider,
settings,
telegram,
onError: (error) => Sentry.captureException(error),
});
const forumStore = new JsonFileStore(process.env.DATA_DIR ?? "./data", "forums.json", emptyForumCatalog);
const forumProvider: SearchProvider = useMock
? mockForumProvider({ delayMs: 1500 })
: new ClaudeSearchProvider(undefined, forumSearchConfig);
const forums = new ForumSearch({
store: forumStore,
provider: forumProvider,
settings,
telegram,
onError: (error) => Sentry.captureException(error),
});
const notifier = new Notifier({
settings,
catalog: store,
trainings,
forums,
search,
telegram,
telegramProblem,
onError: (error) => Sentry.captureException(error),
});
notifier.start();
const app = await buildServer({
store,
search,
notifier,
trainings: { search: trainings, store: trainingStore, providerName: trainingProvider.name },
forums: { search: forums, store: forumStore, providerName: forumProvider.name },
auth: new OwnerAuth(process.env.OWNER_PASSWORD || undefined),
secureCookies: Boolean(process.env.PEPPA_HOST),
providerName: provider.name,
logger: true,
setup: (instance) => Sentry.setupFastifyErrorHandler(instance),
});
const port = Number(process.env.PORT ?? 3000);
await app.listen({ port, host: "0.0.0.0" });
app.log.info(`search provider: ${provider.name}, telegram: ${telegram.name}`);
if (!process.env.OWNER_PASSWORD) app.log.warn("OWNER_PASSWORD не задан — вход владельца недоступен, поиск и Телеграм закрыты");