Skip to content

Commit 1e8c186

Browse files
committed
consolidate cache util
1 parent 67d7b25 commit 1e8c186

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

app/routes/admin+/cache_.sqlite.server.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { json, redirect, type ActionFunctionArgs } from '@remix-run/node'
2+
import { z } from 'zod'
3+
import { cache } from '#app/utils/cache.server.ts'
14
import {
25
getInstanceInfo,
36
getInternalInstanceDomain,
@@ -27,3 +30,29 @@ export async function updatePrimaryCacheValue({
2730
body: JSON.stringify({ key, cacheValue }),
2831
})
2932
}
33+
34+
export async function action({ request }: ActionFunctionArgs) {
35+
const { currentIsPrimary, primaryInstance } = await getInstanceInfo()
36+
if (!currentIsPrimary) {
37+
throw new Error(
38+
`${request.url} should only be called on the primary instance (${primaryInstance})}`,
39+
)
40+
}
41+
const token = process.env.INTERNAL_COMMAND_TOKEN
42+
const isAuthorized =
43+
request.headers.get('Authorization') === `Bearer ${token}`
44+
if (!isAuthorized) {
45+
// nah, you can't be here...
46+
return redirect('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
47+
}
48+
const { key, cacheValue } = z
49+
.object({ key: z.string(), cacheValue: z.unknown().optional() })
50+
.parse(await request.json())
51+
if (cacheValue === undefined) {
52+
await cache.delete(key)
53+
} else {
54+
// @ts-expect-error - we don't reliably know the type of cacheValue
55+
await cache.set(key, cacheValue)
56+
}
57+
return json({ success: true })
58+
}

app/routes/admin+/cache_.sqlite.tsx

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1 @@
1-
import { json, redirect, type ActionFunctionArgs } from '@remix-run/node'
2-
import { z } from 'zod'
3-
import { cache } from '#app/utils/cache.server.ts'
4-
import { getInstanceInfo } from '#app/utils/litefs.server'
5-
6-
export async function action({ request }: ActionFunctionArgs) {
7-
const { currentIsPrimary, primaryInstance } = await getInstanceInfo()
8-
if (!currentIsPrimary) {
9-
throw new Error(
10-
`${request.url} should only be called on the primary instance (${primaryInstance})}`,
11-
)
12-
}
13-
const token = process.env.INTERNAL_COMMAND_TOKEN
14-
const isAuthorized =
15-
request.headers.get('Authorization') === `Bearer ${token}`
16-
if (!isAuthorized) {
17-
// nah, you can't be here...
18-
return redirect('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
19-
}
20-
const { key, cacheValue } = z
21-
.object({ key: z.string(), cacheValue: z.unknown().optional() })
22-
.parse(await request.json())
23-
if (cacheValue === undefined) {
24-
await cache.delete(key)
25-
} else {
26-
// @ts-expect-error - we don't reliably know the type of cacheValue
27-
await cache.set(key, cacheValue)
28-
}
29-
return json({ success: true })
30-
}
1+
export { action } from './cache_.sqlite.server.ts'

0 commit comments

Comments
 (0)