Parcourir la source

部署:双应用支持 Nginx 子路径访问

zhouhao il y a 2 semaines
Parent
commit
ebd6ff993d

+ 1 - 0
.gitignore

@@ -7,6 +7,7 @@ frontend/node_modules/
 frontend/dist/
 viewer/frontend/node_modules/
 viewer/frontend/dist/
+runtime/
 .env
 .env.*
 !.env.example

+ 2 - 2
frontend/src/api.ts

@@ -20,8 +20,8 @@ import type {
   WaveWindowResponse,
 } from './types'
 
-const apiBase = import.meta.env.VITE_API_BASE ?? ''
-const TOKEN_KEY = 'auth_token'
+const apiBase = import.meta.env.VITE_API_BASE ?? (import.meta.env.DEV ? '' : '/query')
+const TOKEN_KEY = 'query_auth_token'
 
 export function getToken(): string | null {
   return localStorage.getItem(TOKEN_KEY)

+ 3 - 2
frontend/vite.config.ts

@@ -1,7 +1,8 @@
 import { defineConfig } from 'vite'
 import vue from '@vitejs/plugin-vue'
 
-export default defineConfig({
+export default defineConfig(({ mode }) => ({
+  base: mode === 'production' ? '/query/' : '/',
   plugins: [vue()],
   server: {
     port: 5173,
@@ -10,4 +11,4 @@ export default defineConfig({
       '/api': 'http://127.0.0.1:8000',
     },
   },
-})
+}))

+ 5 - 2
viewer/frontend/src/api/http.ts

@@ -2,8 +2,11 @@ import axios from 'axios'
 
 import { getToken, setToken } from './token'
 
-/** 同源请求:开发由 Vite 代理到后端,生产由 FastAPI 统一托管。 */
-export const http = axios.create({ baseURL: '', timeout: 60000 })
+/** 开发由 Vite 代理,生产由 Nginx 在 /viewer 下转发。 */
+export const http = axios.create({
+  baseURL: import.meta.env.VITE_API_BASE ?? (import.meta.env.DEV ? '' : '/viewer'),
+  timeout: 60000,
+})
 
 http.interceptors.request.use((config) => {
   const token = getToken()

+ 2 - 1
viewer/frontend/src/api/models.ts

@@ -6,7 +6,8 @@ export async function fetchModels(): Promise<string[]> {
 }
 
 export function modelUrl(name: string): string {
-  return `/models/${encodeURIComponent(name)}`
+  const base = import.meta.env.VITE_API_BASE ?? (import.meta.env.DEV ? '' : '/viewer')
+  return `${base}/models/${encodeURIComponent(name)}`
 }
 
 export interface RenamePartResult {

+ 1 - 1
viewer/frontend/src/api/token.ts

@@ -1,4 +1,4 @@
-const TOKEN_KEY = 'auth_token'
+const TOKEN_KEY = 'viewer_auth_token'
 
 export function getToken(): string | null {
   return localStorage.getItem(TOKEN_KEY)

+ 3 - 2
viewer/frontend/vite.config.ts

@@ -1,7 +1,8 @@
 import { defineConfig } from 'vite'
 import vue from '@vitejs/plugin-vue'
 
-export default defineConfig({
+export default defineConfig(({ mode }) => ({
+  base: mode === 'production' ? '/viewer/' : '/',
   plugins: [vue()],
   server: {
     port: 5183,
@@ -15,4 +16,4 @@ export default defineConfig({
     outDir: 'dist',
     chunkSizeWarningLimit: 2000,
   },
-})
+}))