1
0
mirror of https://github.com/elk-zone/elk synced 2024-11-30 15:58:06 +09:00
elk/server/shared.ts

76 lines
2.3 KiB
TypeScript
Raw Normal View History

// @ts-expect-error unstorage needs to provide backwards-compatible subpath types
import _fs from 'unstorage/drivers/fs'
// @ts-expect-error unstorage needs to provide backwards-compatible subpath types
import _kv from 'unstorage/drivers/cloudflare-kv-http'
2023-01-02 04:30:39 +09:00
import { stringifyQuery } from 'ufo'
import { $fetch } from 'ofetch'
import type { Storage } from 'unstorage'
import cached from './cache-driver'
2022-11-16 00:48:23 +09:00
import type { AppInfo } from '~/types'
import { APP_NAME } from '~/constants'
2022-11-15 23:29:46 +09:00
2022-11-28 03:39:40 +09:00
const config = useRuntimeConfig()
const fs = _fs as typeof import('unstorage/dist/drivers/fs')['default']
const kv = _kv as typeof import('unstorage/dist/drivers/cloudflare-kv-http')['default']
2022-11-15 23:29:46 +09:00
const storage = useStorage() as Storage
if (config.storage.driver === 'fs') {
storage.mount('servers', fs({ base: config.storage.fsBase }))
}
else if (config.storage.driver === 'cloudflare') {
storage.mount('servers', cached(kv({
accountId: config.cloudflare.accountId,
namespaceId: config.cloudflare.namespaceId,
apiToken: config.cloudflare.apiToken,
})))
}
2023-01-02 04:30:39 +09:00
export function getRedirectURI(origin: string, server: string) {
return `${origin}/api/${server}/oauth?${stringifyQuery({ origin })}`
}
2023-01-02 04:30:39 +09:00
async function fetchAppInfo(origin: string, server: string) {
const app: AppInfo = await $fetch(`https://${server}/api/v1/apps`, {
method: 'POST',
body: {
2023-01-04 22:26:30 +09:00
client_name: APP_NAME + (config.public.env !== 'release' ? ` (${config.public.env})` : ''),
website: 'https://elk.zone',
2023-01-02 04:30:39 +09:00
redirect_uris: getRedirectURI(origin, server),
scopes: 'read write follow push',
},
2022-11-16 00:48:23 +09:00
})
return app
}
2023-01-02 04:30:39 +09:00
export async function getApp(origin: string, server: string) {
const key = `servers:${origin.replace(/[^\w\d]/g, '-')}:${server}.json`
try {
if (await storage.hasItem(key))
return await storage.getItem(key) as Promise<AppInfo>
2023-01-02 04:30:39 +09:00
const appInfo = await fetchAppInfo(origin, server)
await storage.setItem(key, appInfo)
return appInfo
}
catch {
return null
}
2022-11-15 23:29:46 +09:00
}
2022-12-24 09:07:38 +09:00
export async function listServers() {
2022-12-28 23:03:32 +09:00
const keys = await storage.getKeys('servers:')
2022-12-24 09:07:38 +09:00
const servers = new Set<string>()
for await (const key of keys) {
const id = key.split(':').pop()!.replace(/\.json$/, '')
if (id)
2022-12-24 09:51:45 +09:00
servers.add(id.toLocaleLowerCase())
2022-12-24 09:07:38 +09:00
}
return Array.from(servers).sort()
}