diff --git a/src/meilisearch_endpoint/index.ts b/src/meilisearch_endpoint/index.ts index fda64bb..a1962e5 100644 --- a/src/meilisearch_endpoint/index.ts +++ b/src/meilisearch_endpoint/index.ts @@ -3,7 +3,7 @@ import { defineEndpoint } from '@directus/extensions-sdk'; export default defineEndpoint({ id: 'meilisearch', handler: (router, context) => { - router.get('/', (_req, res) => res.send('Hello, World!')); + router.get('/', (_req, res) => res.send('Hello, MeiliSearch!')); router.get('/collections', async (_req, res) => { const { services, getSchema } = context; const { CollectionsService } = services; @@ -22,6 +22,29 @@ export default defineEndpoint({ })); res.json(result); - }) + }); + router.post('/connection-test', async (req, res) => { + const { host, apiKey } = req.body; + console.log(req.body); + try { + const response = await fetch(`${host}/health`, { + method: 'GET', + headers: { + 'Authorization': `Bearer ${apiKey}`, + }, + }); + if (!response.ok) { + throw new Error(`MeiliSearch health check failed with status ${response.status}`); + } + const data = await response.json(); + if (data.status === 'available') { + return res.json({ success: true, message: '连接成功' }); + } else { + return res.json({ success: false, message: `返回状态异常:${JSON.stringify(data)}` }); + } + } catch (error) { + return res.json({ success: false, message: `连接失败:${(error as Error).message}` }); + } + }); } });