From 20ed3ce738e7a8c2bf8f7f7670b889068e88c3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Sat, 26 Nov 2022 00:57:29 +0800 Subject: [PATCH] feat: get endpoint url for netlify preview (#88) --- constants/index.ts | 4 ---- nuxt.config.ts | 1 + server/api/[server]/login.ts | 5 ++--- server/api/[server]/oauth.ts | 5 ++--- server/shared.ts | 22 ++++++++++------------ 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/constants/index.ts b/constants/index.ts index 2d1ecec9..faf0e166 100644 --- a/constants/index.ts +++ b/constants/index.ts @@ -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' diff --git a/nuxt.config.ts b/nuxt.config.ts index 6e35f92b..196d0778 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -38,6 +38,7 @@ export default defineNuxtConfig({ }, }, runtimeConfig: { + deployUrl: process.env.DEPLOY_URL, cloudflare: { accountId: '', namespaceId: '', diff --git a/server/api/[server]/login.ts b/server/api/[server]/login.ts index 28fdd312..3afef3f1 100644 --- a/server/api/[server]/login.ts +++ b/server/api/[server]/login.ts @@ -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 diff --git a/server/api/[server]/oauth.ts b/server/api/[server]/oauth.ts index 785fe36c..be7bd070 100644 --- a/server/api/[server]/oauth.ts +++ b/server/api/[server]/oauth.ts @@ -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 diff --git a/server/shared.ts b/server/shared.ts index 33a0bc7c..5c369749 100644 --- a/server/shared.ts +++ b/server/shared.ts @@ -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 try { - const appInfo = await fetchAppInfo(server) + const appInfo = await fetchAppInfo(host, server) await storage.setItem(key, appInfo) return appInfo }