Skip to content

Commit

Permalink
✨ feat: support configuring multiple plugin index
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoQuote committed Mar 20, 2024
1 parent 6ce89a3 commit dfe3dfb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/self-hosting/environment-variables/basic.mdx
Expand Up @@ -146,7 +146,7 @@ Further reading:
### `PLUGINS_INDEX_URL`

- Type: Optional
- Description: Index address of the LobeChat plugin market. If you have deployed the plugin market service on your own, you can use this variable to override the default plugin market address.
- Description: Index address of the LobeChat plugin market. If you have deployed the plugin market service on your own, you can use this variable to override the default plugin market address, it supports comma-separated values to configure multiple index addresses.
- Default: `https://chat-plugins.lobehub.com`

### `PLUGIN_SETTINGS`
Expand Down
2 changes: 1 addition & 1 deletion docs/self-hosting/environment-variables/basic.zh-CN.mdx
Expand Up @@ -144,7 +144,7 @@ LobeChat 在部署时提供了一些额外的配置项,你可以使用环境
### `PLUGINS_INDEX_URL`

- 类型:可选
- 描述:LobeChat 插件市场的索引地址,如果你自行部署了插件市场的服务,可以使用该变量来覆盖默认的插件市场地址
- 描述:LobeChat 插件市场的索引地址,如果你自行部署了插件市场的服务,可以使用该变量来覆盖默认的插件市场地址, 支持用逗号分隔以配置多个索引地址.
- 默认值:`https://chat-plugins.lobehub.com`

### `PLUGIN_SETTINGS`
Expand Down
34 changes: 25 additions & 9 deletions src/app/api/plugin/store/route.ts
@@ -1,3 +1,7 @@
import { LobeChatPluginsMarketIndex } from '@lobehub/chat-plugin-sdk';
import { NextResponse } from 'next/server';

import { getServerConfig } from '@/config/server';
import { DEFAULT_LANG } from '@/const/locale';

import { PluginStore } from './Store';
Expand All @@ -7,15 +11,27 @@ export const runtime = 'edge';
export const GET = async (req: Request) => {
const locale = new URL(req.url).searchParams.get('locale');

const pluginStore = new PluginStore();

let res: Response;

res = await fetch(pluginStore.getPluginIndexUrl(locale as any), { next: { revalidate: 3600 } });

if (res.status === 404) {
res = await fetch(pluginStore.getPluginIndexUrl(DEFAULT_LANG));
const pluginBaseUrls = getServerConfig().PLUGINS_INDEX_URL.split(',');
let pluginIndex: LobeChatPluginsMarketIndex = {
plugins: [],
schemaVersion: 1,
};
for (const baseUrl of pluginBaseUrls) {
const pluginStore = new PluginStore(baseUrl);
let res: Response;
res = await fetch(pluginStore.getPluginIndexUrl(locale as any));

if (res.status === 404) {
res = await fetch(pluginStore.getPluginIndexUrl(DEFAULT_LANG));
}

if (res.status !== 200) {
continue;
}

const data: LobeChatPluginsMarketIndex = await res.json();
pluginIndex.plugins = pluginIndex.plugins.concat(data.plugins);
}

return res;
return NextResponse.json(pluginIndex);
};

0 comments on commit dfe3dfb

Please sign in to comment.