feat: 本地Docker支持源码热更新和独立追溯地址

This commit is contained in:
Nixevol
2026-09-20 02:13:48 +08:00
parent 4dab600062
commit c4864992e8
10 changed files with 176 additions and 25 deletions
+3
View File
@@ -55,6 +55,9 @@ export default defineConfig({
server: {
port: 5173,
host: true,
strictPort: true,
watch: process.env.DOCKER_DEV === '1' ? { usePolling: true, interval: 300 } : undefined,
hmr: process.env.VITE_HMR_CLIENT_PORT ? { clientPort: Number(process.env.VITE_HMR_CLIENT_PORT) } : undefined,
// dev 下把 5173 打造成与生产一致的「单入口」:/api、/uploads、短链 /t 走 API,/trace 走 C 端 dev server。
// 生产由 API 单端口托管全部路径,短链 {BASE_URL}/t/xxx 天然可用;此处让本地行为对齐。
proxy: {
+12 -4
View File
@@ -8,6 +8,7 @@ export interface AppConfig {
publicTracePath: string;
adminOrigin: string;
clientOrigin: string;
developmentClientBaseUrl?: string;
traceUrlTemplate: string;
jwt: { secret: string; expiresIn: string };
settingsEncryptionKey: string;
@@ -36,17 +37,24 @@ export interface AppConfig {
export default (): AppConfig => {
preloadRuntimeEnv();
const clientOrigin = process.env.CLIENT_ORIGIN || 'http://localhost:5174';
// Only an explicitly configured local development origin overrides imported settings.
const developmentOrigin = process.env.NODE_ENV === 'development'
? (process.env.ARTVERIS_DEV_ORIGIN || '').replace(/\/$/, '') : '';
if (developmentOrigin && !/^http:\/\/(127\.0\.0\.1|localhost)(:\d+)?$/.test(developmentOrigin)) {
throw new Error('ARTVERIS_DEV_ORIGIN must be a local loopback HTTP origin');
}
const clientOrigin = developmentOrigin ? `${developmentOrigin}/trace` : process.env.CLIENT_ORIGIN || 'http://localhost:5174';
return {
appName: process.env.APP_NAME || '艺术品溯源平台',
env: process.env.NODE_ENV || 'development',
port: parseInt(process.env.API_PORT || '3000', 10),
baseUrl: process.env.BASE_URL || 'http://localhost:3000',
baseUrl: developmentOrigin || process.env.BASE_URL || 'http://localhost:3000',
publicTracePath: process.env.PUBLIC_TRACE_PATH || '/t',
adminOrigin: process.env.ADMIN_ORIGIN || 'http://localhost:5173',
adminOrigin: developmentOrigin || process.env.ADMIN_ORIGIN || 'http://localhost:5173',
clientOrigin,
developmentClientBaseUrl: developmentOrigin ? clientOrigin : undefined,
// 短链跳转到 C 端溯源页的地址模板,{code} 会被替换为短码
traceUrlTemplate: process.env.TRACE_URL_TEMPLATE || `${clientOrigin}/#/trace/{code}`,
traceUrlTemplate: developmentOrigin ? `${clientOrigin}/#/trace/{code}` : process.env.TRACE_URL_TEMPLATE || `${clientOrigin}/#/trace/{code}`,
jwt: {
secret: process.env.JWT_SECRET || 'artveris_dev_secret',
expiresIn: process.env.JWT_EXPIRES_IN || '7d',
@@ -1,6 +1,18 @@
import { SettingsService } from './settings.service';
describe('SettingsService.update', () => {
it('uses the explicit development URL for links and displayed settings after importing production data', async () => {
const prisma = { systemSetting: {
findMany: jest.fn().mockResolvedValue([{ key: 'clientBaseUrl', value: 'https://art.qclh.xyz/trace' }]),
findUnique: jest.fn().mockResolvedValue({ value: 'https://art.qclh.xyz/trace' }),
} };
const config = { get: jest.fn((key: string) => key === 'developmentClientBaseUrl' ? 'http://127.0.0.1:18080/trace' : undefined) };
const service = new SettingsService(prisma as any, config as any);
expect(await service.getClientBaseUrl()).toBe('http://127.0.0.1:18080/trace');
expect((await service.getAll()).clientBaseUrl).toBe('http://127.0.0.1:18080/trace');
config.get.mockImplementation(() => undefined);
expect(await service.getClientBaseUrl()).toBe('https://art.qclh.xyz/trace');
});
it('writes a multi-key settings change inside one database transaction', async () => {
const tx = {
systemSetting: { upsert: jest.fn().mockResolvedValue(undefined) },
@@ -101,6 +101,8 @@ export class SettingsService {
const value = await this.decryptSecret(r.value);
map[r.key] = r.key === 'appName' && value === '艺术品可信溯源与认证平台' ? '艺术品溯源平台' : value;
}
const localClient = this.config.get<string>('developmentClientBaseUrl');
if (localClient) map.clientBaseUrl = localClient;
if (map.appIconUrl === '/app-icon.svg') map.appIconUrl = '';
if (maskSecrets) {
for (const key of MASKED_KEYS) {
@@ -118,6 +120,8 @@ export class SettingsService {
}
async getClientBaseUrl(): Promise<string> {
const localClient = this.config.get<string>('developmentClientBaseUrl');
if (localClient) return localClient.replace(/\/$/, '');
const fromDb = await this.get('clientBaseUrl');
return (fromDb || this.config.get<string>('clientOrigin') || 'http://localhost:5174').replace(/\/$/, '');
}
+3
View File
@@ -38,6 +38,9 @@ export default defineConfig({
server: {
port: 5174,
host: true,
strictPort: true,
watch: process.env.DOCKER_DEV === '1' ? { usePolling: true, interval: 300 } : undefined,
hmr: process.env.VITE_HMR_CLIENT_PORT ? { clientPort: Number(process.env.VITE_HMR_CLIENT_PORT) } : undefined,
proxy: {
'/api': { target: 'http://localhost:3000', changeOrigin: true },
'/uploads': { target: 'http://localhost:3000', changeOrigin: true },