web search done right
Fix CORS issues when adding an optic. When adding an optic, we first make sure that we can actually fetch the optic. This check was performed client-side before, which would cause some CORS errors if it was against the CORS policy of the server hosting the optic. This commit introduces a simple endpoint on our frontend server, so the request to the optic server now doesn't come directly from the client.
Mikkel Denkeraa4e59cb6e84
diff --git a/crates/core/src/api/search.rs b/crates/core/src/api/search.rsindex 3677fc04..94bf3c20 100644--- a/crates/core/src/api/search.rs+++ b/crates/core/src/api/search.rs@@ -116,7 +116,7 @@ pub async fn search(if let Err(err) = query {tracing::error!("{:?}", err);- return Ok(err.to_string().into_response());+ return Err(StatusCode::BAD_REQUEST);}let mut query = query.unwrap();diff --git a/frontend/src/routes/search/+page.svelte b/frontend/src/routes/search/+page.svelteindex 71be269d..affdb847 100644--- a/frontend/src/routes/search/+page.svelte+++ b/frontend/src/routes/search/+page.svelte@@ -12,7 +12,6 @@import Result from './Result.svelte';import Sidebar from './Sidebar.svelte';import Widget from './Widget.svelte';- import DirectAnswer from './DirectAnswer.svelte';import Discussions from './Discussions.svelte';import { page } from '$app/stores';import { updateQueryId } from '$lib/improvements';diff --git a/frontend/src/routes/settings/optics/+page.svelte b/frontend/src/routes/settings/optics/+page.svelteindex a0b55d13..35578235 100644--- a/frontend/src/routes/settings/optics/+page.svelte+++ b/frontend/src/routes/settings/optics/+page.svelte@@ -4,7 +4,7 @@import Eye from '~icons/heroicons/eye';import Button from '$lib/components/Button.svelte';import { opticsShowStore, opticsStore } from '$lib/stores';- import { DEFAULT_OPTICS, fetchRemoteOptic, opticKey, type OpticOption } from '$lib/optics';+ import { DEFAULT_OPTICS, opticKey, type OpticOption } from '$lib/optics';import { derived } from 'svelte/store';import Callout from '$lib/components/Callout.svelte';@@ -17,11 +17,21 @@const addOptic = async () => {error = void 0;try {- await fetchRemoteOptic({- opticUrl: url,- fetch,+ const res = await fetch('optics/validate', {+ method: 'post',+ headers: {+ 'Content-Type': 'application/json',+ },+ body: JSON.stringify({+ opticUrl: url,+ }),});+ if (res.status != 200) {+ const desc = await res.text();+ throw TypeError(desc);+ }+opticsShowStore.update(($opticsShow) => ({...$opticsShow,[opticKey({ name, url, description })]: true,@@ -138,7 +148,7 @@disabled={!removable}><MinusCircle- class="text-neutral transition group-enabled:text-error group-enabled:group-hover:text-error"+ class="text-neutral group-enabled:text-error group-enabled:group-hover:text-error transition"/></button><label class="flex w-6 items-start hover:cursor-pointer">@@ -147,8 +157,8 @@bind:checked={$opticsShowStore[opticKey(optic)]}class="peer hidden"/>- <EyeSlash class="inline-flex text-neutral transition peer-checked:hidden" />- <Eye class="hidden text-neutral transition peer-checked:inline-flex" />+ <EyeSlash class="text-neutral inline-flex transition peer-checked:hidden" />+ <Eye class="text-neutral hidden transition peer-checked:inline-flex" /></label><div class="text-sm">{optic.name}</div><div class="text-sm">diff --git a/frontend/src/routes/settings/optics/validate/+server.ts b/frontend/src/routes/settings/optics/validate/+server.tsnew file mode 100644index 00000000..fbb3d266--- /dev/null+++ b/frontend/src/routes/settings/optics/validate/+server.ts@@ -0,0 +1,31 @@+import { fetchRemoteOptic } from '$lib/optics';+import { error, text, type RequestEvent } from '@sveltejs/kit';++export async function POST({ request }: RequestEvent): Promise<Response> {+ const {+ opticUrl,+ }: {+ opticUrl: string;+ } = await request.json();++ try {+ const optic = await fetchRemoteOptic({+ opticUrl,+ fetch,+ });++ if (optic == undefined) {+ throw error(400, 'failed to fetch optic from url');+ }+ } catch (e) {+ if (e instanceof TypeError) {+ throw error(400, 'failed to fetch optic from url');+ }++ throw e;+ }++ // TODO: maybe validate optics syntax here?++ return text('success');+}diff --git a/tools/ranking-annotation/src/routes/api/annotate/[slug]/+server.ts b/tools/ranking-annotation/src/routes/api/annotate/[slug]/+server.tsindex c1730632..ed99ccb8 100644--- a/tools/ranking-annotation/src/routes/api/annotate/[slug]/+server.ts+++ b/tools/ranking-annotation/src/routes/api/annotate/[slug]/+server.ts@@ -1,14 +1,17 @@import type { SearchResult } from "$lib/db";import { saveSearchResults } from "$lib/db";+import type { RequestEvent } from "@sveltejs/kit";-export async function POST({request}): Promise<Response> {- const {qid, results}: {- qid: string;- results: SearchResult[];- } = await request.json();+export async function POST({ request }: RequestEvent): Promise<Response> {+ const {+ qid,+ results,+ }: {+ qid: string;+ results: SearchResult[];+ } = await request.json();- saveSearchResults(qid, results);+ saveSearchResults(qid, results);- return new Response("OK");+ return new Response("OK");}-\ No newline at end of filediff --git a/tools/ranking-annotation/src/routes/api/data/+server.ts b/tools/ranking-annotation/src/routes/api/data/+server.tsindex 9b71cc80..a1d93f46 100644--- a/tools/ranking-annotation/src/routes/api/data/+server.ts+++ b/tools/ranking-annotation/src/routes/api/data/+server.ts@@ -1,6 +1,7 @@+import type { RequestEvent } from "@sveltejs/kit";import { getDataset } from "$lib/dataset";-export async function GET({}): Promise<Response> {+export async function GET({}: RequestEvent): Promise<Response> {const data = getDataset();if (data) {