feat: 本地Docker支持源码热更新和独立追溯地址
This commit is contained in:
@@ -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(/\/$/, '');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user