1
0
Fork 1
mirror of https://github.com/elk-zone/elk.git synced 2024-11-07 01:19:57 +00:00

feat: get endpoint url for netlify preview (#88)

This commit is contained in:
三咲智子 Kevin Deng 2022-11-26 00:57:29 +08:00 committed by GitHub
parent 055bcf0e96
commit 20ed3ce738
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 22 deletions

View file

@ -1,9 +1,5 @@
export const APP_NAME = 'Elk'
export const HOST_DOMAIN = process.dev
? 'http://localhost:5314'
: 'https://elk.zone'
export const DEFAULT_POST_CHARS_LIMIT = 500
export const DEFAULT_SERVER = 'mas.to'

View file

@ -38,6 +38,7 @@ export default defineNuxtConfig({
},
},
runtimeConfig: {
deployUrl: process.env.DEPLOY_URL,
cloudflare: {
accountId: '',
namespaceId: '',

View file

@ -1,10 +1,9 @@
import { stringifyQuery } from 'ufo'
import { getApp } from '~/server/shared'
import { HOST_DOMAIN } from '~/constants'
import { HOST_DOMAIN, getApp } from '~/server/shared'
export default defineEventHandler(async ({ context, res }) => {
const server = context.params.server
const app = await getApp(server)
const app = await getApp(HOST_DOMAIN, server)
if (!app) {
res.statusCode = 400

View file

@ -1,11 +1,10 @@
import { getQuery } from 'ufo'
import { stringifyQuery } from 'vue-router'
import { getApp } from '~/server/shared'
import { HOST_DOMAIN } from '~/constants'
import { HOST_DOMAIN, getApp } from '~/server/shared'
export default defineEventHandler(async ({ context, req, res }) => {
const server = context.params.server
const app = await getApp(server)
const app = await getApp(HOST_DOMAIN, server)
if (!app) {
res.statusCode = 400

View file

@ -11,6 +11,10 @@ import cached from './cache-driver'
import type { AppInfo } from '~/types'
import { APP_NAME } from '~/constants'
const runtimeConfig = useRuntimeConfig()
export const HOST_DOMAIN = runtimeConfig.deployUrl
|| (process.dev ? 'http://localhost:5314' : 'https://elk.zone')
const fs = _fs as typeof import('unstorage/dist/drivers/fs')['default']
const kv = _kv as typeof import('unstorage/dist/drivers/cloudflare-kv-http')['default']
@ -28,16 +32,10 @@ else {
})))
}
const KNOWN_DOMAINS = [
'http://localhost:5314',
'https://elk.netlify.app',
'https://elk.zone',
]
async function fetchAppInfo(server: string) {
async function fetchAppInfo(host: string, server: string) {
const redirect_uris = [
'urn:ietf:wg:oauth:2.0:oob',
...KNOWN_DOMAINS.map(d => `${d}/api/${server}/oauth`),
`${host}/api/${server}/oauth`,
].join('\n')
const app: AppInfo = await $fetch(`https://${server}/api/v1/apps`, {
@ -51,15 +49,15 @@ async function fetchAppInfo(server: string) {
return app
}
const serverKey = (server: string) => `servers:${server}.json`
const serverKey = (host: string, server: string) => `servers:${host}:${server}.json`
export async function getApp(server: string) {
const key = serverKey(server)
export async function getApp(host: string, server: string) {
const key = serverKey(host, server)
if (await storage.hasItem(key))
return storage.getItem(key) as Promise<AppInfo>
try {
const appInfo = await fetchAppInfo(server)
const appInfo = await fetchAppInfo(host, server)
await storage.setItem(key, appInfo)
return appInfo
}