1
0
mirror of https://github.com/elk-zone/elk synced 2024-11-27 14:28:10 +09: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 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_POST_CHARS_LIMIT = 500
export const DEFAULT_SERVER = 'mas.to' export const DEFAULT_SERVER = 'mas.to'

View File

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

View File

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

View File

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

View File

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