Skip to content

Commit

Permalink
Merge pull request #304 from StampyAI/cors
Browse files Browse the repository at this point in the history
Set CORS headers
  • Loading branch information
mruwnik committed Aug 22, 2023
2 parents 205a070 + 9ec2d42 commit be64c90
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
6 changes: 4 additions & 2 deletions app/routes/questions/glossary.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {jsonCORS} from '../../server-utils/responses'
import {LoaderArgs} from '@remix-run/cloudflare'
import {reloadInBackgroundIfNeeded} from '~/server-utils/kv-cache'
import {loadGlossary} from '~/server-utils/stampy'

export const loader = async ({request}: LoaderArgs) => {
return await loadGlossary(request)
const data = await loadGlossary(request)
return jsonCORS<typeof data>(data)
}
type Data = ReturnType<typeof loader>
type Data = ReturnType<typeof loadGlossary>

export function fetchGlossary() {
const url = `/questions/glossary`
Expand Down
4 changes: 3 additions & 1 deletion app/routes/questions/search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {LoaderArgs} from '@remix-run/cloudflare'
import {jsonCORS} from '../../server-utils/responses'

export const loader = async ({request}: LoaderArgs) => {
const url = new URL(request.url)
Expand All @@ -7,7 +8,8 @@ export const loader = async ({request}: LoaderArgs) => {

if (!question) return []

return await search(question, onlyLive)
const results = await search(question, onlyLive)
return jsonCORS(results)
}

export function search(question: string, onlyLive: boolean) {
Expand Down
9 changes: 9 additions & 0 deletions app/server-utils/responses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {json} from '@remix-run/cloudflare'

export const jsonCORS = <T>(data: T) =>
json(data, {
headers: {
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Origin': ALLOW_ORIGINS,
},
})
1 change: 1 addition & 0 deletions remix.env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ declare const CODA_TOKEN: string
declare const CODA_INCOMING_TOKEN: string
declare const CODA_WRITES_TOKEN: string
declare const NLP_SEARCH_ENDPOINT: string
declare const ALLOW_ORIGINS: string
29 changes: 27 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
import {createEventHandler} from '@remix-run/cloudflare-workers'
import url from 'url'
import {createEventHandler, handleAsset} from '@remix-run/cloudflare-workers'
import * as build from '@remix-run/dev/server-build'

addEventListener('fetch', createEventHandler({build, mode: process.env.NODE_ENV}))
const CORS_ASSETS = ['/tfWorker.js']

const isCorsEnabledAsset = (event) => {
const parsedUrl = url.parse(event.request.url)
const pathname = parsedUrl.pathname

return CORS_ASSETS.includes(pathname)
}

const fetchCorsAsset = (event) => {
const resp = handleAsset(event, build)
return event.respondWith(
resp.then((res) => {
const headers = new Headers(res.headers)
headers.set('Access-Control-Allow-Origin', ALLOW_ORIGINS || '')
headers.set('Access-Control-Allow-Methods', 'GET, OPTIONS')
return new Response(res.body, {headers})
})
)
}

const handler = createEventHandler({build, mode: process.env.NODE_ENV})
addEventListener('fetch', async (event) =>
isCorsEnabledAsset(event) ? fetchCorsAsset(event) : handler(event)
)
1 change: 1 addition & 0 deletions wrangler.toml.template
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ CODA_TOKEN = "{CODA_TOKEN}"
CODA_INCOMING_TOKEN = "{CODA_INCOMING_TOKEN}"
CODA_WRITES_TOKEN = "{CODA_WRITES_TOKEN}"
NLP_SEARCH_ENDPOINT = "https://stampy-nlp-t6p37v2uia-uw.a.run.app/"
ALLOW_ORIGINS = "https://chat.aisafety.info"

0 comments on commit be64c90

Please sign in to comment.