Skip to content

Commit

Permalink
✨ feat: add builtin search with SearXNG
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Oct 12, 2024
1 parent c59d8aa commit 6779a39
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/config/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { z } from 'zod';
export const getToolsConfig = () => {
return createEnv({
runtimeEnv: {
SEARXNG_URL: process.env.SEARXNG_URL || 'https://searx.tiekoetter.com/',
SEARXNG_URL: process.env.SEARXNG_URL,
},

server: {
SEARXNG_URL: z.string().url(),
SEARXNG_URL: z.string().url().optional(),
},
});
};
Expand Down
7 changes: 7 additions & 0 deletions src/libs/trpc/client/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import type { ToolsRouter } from '@/server/routers/tools';
export const toolsClient = createTRPCClient<ToolsRouter>({
links: [
httpBatchLink({
headers: async () => {
// dynamic import to avoid circular dependency
const { createHeaderWithAuth } = await import('@/services/_auth');

return createHeaderWithAuth();
},
maxURLLength: 2083,
transformer: superjson,
url: '/trpc/tools',
}),
Expand Down
3 changes: 2 additions & 1 deletion src/server/modules/SearXNG.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import qs from 'query-string';
import urlJoin from 'url-join';

import { SearchResponse } from '@/types/tool/search';

Expand All @@ -17,7 +18,7 @@ export class SearXNGClient {
q: query,
});

const response = await fetch(`${this.baseUrl}/search?${searchParams}`);
const response = await fetch(urlJoin(this.baseUrl, `/search?${searchParams}`));

return await response.json();
} catch (error) {
Expand Down
12 changes: 10 additions & 2 deletions src/server/routers/tools/search.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { TRPCError } from '@trpc/server';
import { z } from 'zod';

import { toolsEnv } from '@/config/tools';
import { authedProcedure, router } from '@/libs/trpc';
import { isServerMode } from '@/const/version';
import { authedProcedure, passwordProcedure, router } from '@/libs/trpc';
import { SearXNGClient } from '@/server/modules/SearXNG';

const searchProcedure = isServerMode ? authedProcedure : passwordProcedure;

export const searchRouter = router({
query: authedProcedure
query: searchProcedure
.input(
z.object({
query: z.string(),
searchEngine: z.array(z.string()).optional(),
}),
)
.query(async ({ input }) => {
if (!toolsEnv.SEARXNG_URL) {
throw new TRPCError({ code: 'SERVICE_UNAVAILABLE', message: 'SearXNG is not configured' });
}

const client = new SearXNGClient(toolsEnv.SEARXNG_URL);

return await client.search(input.query, input.searchEngine);
Expand Down

0 comments on commit 6779a39

Please sign in to comment.