Files
khb_microservices/packages/qbo_auth/src/main.ts
2026-11-16 22:19:06 +03:00

35 lines
1.4 KiB
TypeScript

import { createLogger, createPool, itNotify, loadServiceSettings } from "khb_common";
import { parseKey } from "./crypto.js";
import { buildApi } from "./http-api.js";
import { QboAuth } from "./service.js";
import { qboAuthSchema } from "./settings.js";
import { PgStore } from "./store.js";
const { common, service: s } = loadServiceSettings("qbo_auth", qboAuthSchema);
const log = createLogger("qbo_auth", common.logLevel);
const pool = createPool(common.database.url, 3);
const auth = new QboAuth({
store: new PgStore(pool, parseKey(s.encryptionKey)),
fetchFn: (url, init) => fetch(url, init),
env: s.environment,
clientId: s.clientId,
clientSecret: s.clientSecret,
redirectUri: s.redirectUri,
scope: s.scope,
keepAliveHours: s.keepAliveHours,
notify: async (text) => {
await itNotify(common, text).catch(() => false);
},
});
const app = buildApi(auth, s.http.token);
await app.listen({ host: s.http.host, port: s.http.port });
log.info({ port: s.http.port, environment: s.environment }, "qbo_auth запущен");
// поддержание токенов: раз в час проверяем, не пора ли обновить (скользящий refresh token)
const timer = setInterval(() => void auth.keepAlive().catch((e) => log.error({ err: String(e) }, "keepalive")), 60 * 60_000);
process.on("SIGTERM", () => {
clearInterval(timer);
void app.close();
});