elk/server/utils/shared.ts

96 lines
2.7 KiB
TypeScript
Raw Permalink Normal View History

2023-02-12 14:56:29 +03:00
import fs from 'unstorage/drivers/fs'
import memory from 'unstorage/drivers/memory'
import kv from 'unstorage/drivers/cloudflare-kv-http'
2024-01-02 13:58:37 +03:00
import vercelKVDriver from 'unstorage/drivers/vercel-kv'
import { $fetch } from 'ofetch'
import cached from '../cache-driver'
// @ts-expect-error virtual import
import { env } from '#build-info'
// @ts-expect-error virtual import
import { driver } from '#storage-config'
2022-11-15 18:48:23 +03:00
import type { AppInfo } from '~/types'
import { APP_NAME } from '~/constants'
2022-11-15 17:29:46 +03:00
const storage = useStorage<AppInfo>()
if (driver === 'fs') {
const config = useRuntimeConfig()
storage.mount('servers', fs({ base: config.storage.fsBase }))
}
else if (driver === 'cloudflare') {
const config = useRuntimeConfig()
storage.mount('servers', cached(kv({
accountId: config.cloudflare.accountId,
namespaceId: config.cloudflare.namespaceId,
apiToken: config.cloudflare.apiToken,
})))
}
2024-01-02 13:58:37 +03:00
else if (driver === 'vercel') {
const config = useRuntimeConfig()
storage.mount('servers', cached(vercelKVDriver({
url: config.vercel.url,
token: config.vercel.token,
env: config.vercel.env,
base: config.vercel.base,
})))
}
else if (driver === 'memory') {
storage.mount('servers', memory())
}
2023-01-01 22:30:39 +03:00
export function getRedirectURI(origin: string, server: string) {
origin = origin.replace(/\?.*$/, '')
return `${origin}/api/${server}/oauth/${encodeURIComponent(origin)}`
}
2023-01-01 22:30:39 +03:00
async function fetchAppInfo(origin: string, server: string) {
const app: AppInfo = await $fetch(`https://${server}/api/v1/apps`, {
method: 'POST',
body: {
client_name: APP_NAME + (env !== 'release' ? ` (${env})` : ''),
website: 'https://elk.zone',
2023-01-01 22:30:39 +03:00
redirect_uris: getRedirectURI(origin, server),
scopes: 'read write follow push',
},
2022-11-15 18:48:23 +03:00
})
return app
}
2023-01-01 22:30:39 +03:00
export async function getApp(origin: string, server: string) {
const host = origin.replace(/^https?:\/\//, '').replace(/[^\w\d]/g, '-').replace(/\?.*$/, '')
const key = `servers:v3:${server}:${host}.json`.toLowerCase()
try {
if (await storage.hasItem(key))
return (storage.getItem(key, {}) as Promise<AppInfo>)
2023-01-01 22:30:39 +03:00
const appInfo = await fetchAppInfo(origin, server)
await storage.setItem(key, appInfo)
return appInfo
}
catch {
return null
}
2022-11-15 17:29:46 +03:00
}
2022-12-24 03:07:38 +03:00
2023-01-15 12:34:17 +03:00
export async function deleteApp(server: string) {
const keys = (await storage.getKeys(`servers:v3:${server}:`))
2023-01-15 12:34:17 +03:00
for (const key of keys)
await storage.removeItem(key)
}
2022-12-24 03:07:38 +03:00
export async function listServers() {
const keys = await storage.getKeys('servers:v3:')
2022-12-24 03:07:38 +03:00
const servers = new Set<string>()
for await (const key of keys) {
const id = key.split(':')[2]
2022-12-24 03:07:38 +03:00
if (id)
2022-12-24 03:51:45 +03:00
servers.add(id.toLocaleLowerCase())
2022-12-24 03:07:38 +03:00
}
return Array.from(servers).sort()
}