Skip to content

Commit

Permalink
chore: split kv & config
Browse files Browse the repository at this point in the history
  • Loading branch information
smarroufin committed Feb 5, 2024
1 parent 77e6800 commit 61f32f2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
3 changes: 2 additions & 1 deletion _nuxthub/modules/hub/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const DEFAULT_WRANGLER = `d1_databases = [
{ binding = "DB", database_name = "default", database_id = "default" },
]
kv_namespaces = [
{ binding = "KV", id = "default" },
{ binding = "KV", id = "user_default" },
{ binding = "CONFIG", id = "config_default" },
]
r2_buckets = [
{ binding = "BLOB", bucket_name = "default" },
Expand Down
10 changes: 10 additions & 0 deletions _nuxthub/server/api/_hub/config/[...path].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createH3StorageHandler } from 'unstorage/server'

export default eventHandler(async (event) => {
const storage = useConfigKV()
return createH3StorageHandler(storage, {
resolvePath(event) {
return event.context.params!.path || ''
}
})(event)
})
40 changes: 38 additions & 2 deletions _nuxthub/server/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { Storage } from 'unstorage'
import { defu } from 'defu'
import { createStorage } from 'unstorage'
import httpDriver from 'unstorage/drivers/http'
import cloudflareKVBindingDriver from 'unstorage/drivers/cloudflare-kv-binding'
import { joinURL } from 'ufo'

const defaults: Config = {
oauth: {
Expand All @@ -14,10 +19,41 @@ const defaults: Config = {
}
}

let _configKV: Storage

export function useConfigKV () {
if (!_configKV) {
if (import.meta.dev && process.env.NUXT_HUB_URL) {
// Use https://unstorage.unjs.io/drivers/http
_configKV = createStorage({
driver: httpDriver({
base: joinURL(process.env.NUXT_HUB_URL, '/api/_hub/config/'),
headers: {
Authorization: `Bearer ${process.env.NUXT_HUB_SECRET_KEY}`
}
})
})
} else {
const binding = process.env.CONFIG || globalThis.__env__?.CONFIG || globalThis.CONFIG
if (binding) {
_configKV = createStorage({
driver: cloudflareKVBindingDriver({
binding
})
})
} else {
throw createError('Missing Cloudflare binding CONFIG')
}
}
}

return _configKV
}

let _config: Config

export async function _fetchConfig() {
let configValue = await useKV().getItem<Config>('_config')
let configValue = await useConfigKV().getItem<Config>('config')
configValue = z.custom<Config>().parse(configValue)
_config = defu(configValue, defaults)

Expand All @@ -39,7 +75,7 @@ export async function setConfig(config: Config) {

let configValue = z.custom<Config>().parse(config)
configValue = defu(config, _config)
await useKV().setItem('_config', configValue)
await useConfigKV().setItem('config', configValue)
_config = configValue

return _config
Expand Down

0 comments on commit 61f32f2

Please sign in to comment.