2023-03-19 08:26:47 +00:00
|
|
|
import * as SonicChannel from "sonic-channel";
|
|
|
|
import { dbLogger } from "./logger.js";
|
|
|
|
|
|
|
|
import config from "@/config/index.js";
|
|
|
|
|
|
|
|
const logger = dbLogger.createSubLogger("sonic", "gray", false);
|
|
|
|
|
|
|
|
logger.info("Connecting to Sonic");
|
|
|
|
|
2023-03-31 02:10:03 +00:00
|
|
|
const handlers = (type: string): SonicChannel.Handlers => ({
|
|
|
|
connected: () => {
|
|
|
|
logger.succ(`Connected to Sonic ${type}`);
|
|
|
|
},
|
|
|
|
disconnected: (error) => {
|
|
|
|
logger.warn(`Disconnected from Sonic ${type}, error: ${error}`);
|
|
|
|
},
|
|
|
|
error: (error) => {
|
|
|
|
logger.warn(`Sonic ${type} error: ${error}`);
|
|
|
|
},
|
|
|
|
retrying: () => {
|
|
|
|
logger.info(`Sonic ${type} retrying`);
|
|
|
|
},
|
|
|
|
timeout: () => {
|
|
|
|
logger.warn(`Sonic ${type} timeout`);
|
|
|
|
},
|
|
|
|
});
|
2023-03-19 08:26:47 +00:00
|
|
|
|
|
|
|
const hasConfig =
|
2023-03-31 02:10:03 +00:00
|
|
|
config.sonic && (config.sonic.host || config.sonic.port || config.sonic.auth);
|
2023-03-19 08:26:47 +00:00
|
|
|
|
|
|
|
const host = hasConfig ? config.sonic.host ?? "localhost" : "";
|
|
|
|
const port = hasConfig ? config.sonic.port ?? 1491 : 0;
|
|
|
|
const auth = hasConfig ? config.sonic.auth ?? "SecretPassword" : "";
|
|
|
|
const collection = hasConfig ? config.sonic.collection ?? "main" : "";
|
|
|
|
const bucket = hasConfig ? config.sonic.bucket ?? "default" : "";
|
|
|
|
|
|
|
|
export default hasConfig
|
|
|
|
? {
|
2023-03-31 02:10:03 +00:00
|
|
|
search: new SonicChannel.Search({ host, port, auth }).connect(
|
|
|
|
handlers("search"),
|
|
|
|
),
|
|
|
|
ingest: new SonicChannel.Ingest({ host, port, auth }).connect(
|
|
|
|
handlers("ingest"),
|
|
|
|
),
|
2023-03-19 08:26:47 +00:00
|
|
|
|
|
|
|
collection,
|
|
|
|
bucket,
|
2023-03-31 02:10:03 +00:00
|
|
|
}
|
2023-03-19 08:26:47 +00:00
|
|
|
: null;
|