diff --git a/config.ts b/config.ts index 26715ef..ec80483 100644 --- a/config.ts +++ b/config.ts @@ -23,6 +23,7 @@ export type ConfigType = { thumbnail_dir: string; remove_previous_gallery: boolean; img_verify_secret?: string; + meili_hosts?: Record; }; export enum ThumbnailMethod { @@ -136,6 +137,18 @@ export class Config { get img_verify_secret() { return this._return_string("img_verify_secret"); } + get meili_hosts() { + if (typeof this._data.meili_hosts === "object") { + const hosts: Record = {}; + for (const i in this._data.meili_hosts) { + /**@ts-ignore Object*/ + const v = this._data.meili_hosts[i]; + if (typeof v === "string") hosts[i] = v; + } + return hosts; + } + return undefined; + } to_json(): ConfigType { return { cookies: typeof this.cookies === "string", @@ -159,6 +172,7 @@ export class Config { thumbnail_dir: this.thumbnail_dir, remove_previous_gallery: this.remove_previous_gallery, img_verify_secret: this.img_verify_secret, + meili_hosts: this.meili_hosts, }; } } diff --git a/routes/api/status.ts b/routes/api/status.ts index f1cb3cc..4be2e9e 100644 --- a/routes/api/status.ts +++ b/routes/api/status.ts @@ -1,7 +1,7 @@ import { Handlers } from "$fresh/server.ts"; import { get_task_manager } from "../../server.ts"; import type { StatusData } from "../../server/status.ts"; -import { return_data } from "../../server/utils.ts"; +import { get_host, return_data } from "../../server/utils.ts"; import { check_ffmpeg_binary } from "../../thumbnail/ffmpeg_binary.ts"; import type * as FFMPEG_API from "../../thumbnail/ffmpeg_api.ts"; @@ -18,7 +18,7 @@ async function check_ffmpeg_api() { } export const handler: Handlers = { - async GET(_req, ctx) { + async GET(req, ctx) { const m = get_task_manager(); const is_authed = ctx.state.user !== undefined || m.db.get_user_count() === 0; @@ -27,15 +27,25 @@ export const handler: Handlers = { ); const ffmpeg_api_enabled = await check_ffmpeg_api(); const meilisearch_enabled = m.meilisearch !== undefined; - const meilisearch = + let meilisearch; + if ( is_authed && meilisearch_enabled && m.cfg.meili_host && - m.cfg.meili_update_api_key - ? { - host: m.cfg.meili_host, - key: m.cfg.meili_search_api_key || - m.cfg.meili_update_api_key, + m.cfg.meili_update_api_key + ) { + const rhost = get_host(req); + const hosts = m.cfg.meili_hosts; + let host: string | undefined; + if (hosts !== undefined) { + if (hosts[rhost]) { + host = hosts[rhost]; } - : undefined; + } + if (!host) host = m.cfg.meili_host; + meilisearch = { + host, + key: m.cfg.meili_search_api_key || m.cfg.meili_update_api_key, + }; + } const no_user = m.db.get_user_count() === 0; return return_data({ ffmpeg_api_enabled,