feat: 完善安装部署与数字溯源码

This commit is contained in:
Nixevol
2026-07-06 23:26:21 +08:00
parent 7480e4d256
commit 7c6b0bff4c
59 changed files with 2174 additions and 166 deletions
+41 -25
View File
@@ -1,3 +1,5 @@
import { loadRuntimeConfig } from './runtime-config';
export interface AppConfig {
appName: string;
env: string;
@@ -8,6 +10,7 @@ export interface AppConfig {
clientOrigin: string;
traceUrlTemplate: string;
jwt: { secret: string; expiresIn: string };
settingsEncryptionKey: string;
redis: { host: string; port: number; password?: string };
storage: {
driver: 'local' | 'oss';
@@ -32,47 +35,60 @@ export interface AppConfig {
}
export default (): AppConfig => {
const clientOrigin = process.env.CLIENT_ORIGIN || 'http://localhost:5174';
const runtime = loadRuntimeConfig();
if (!process.env.DATABASE_URL && runtime.databaseUrl) process.env.DATABASE_URL = runtime.databaseUrl;
if (!process.env.BACKUP_DIR && runtime.backupDir) process.env.BACKUP_DIR = runtime.backupDir;
const storage = runtime.storage || {};
const oss = storage.oss || {};
const blockchain = runtime.blockchain || {};
const tianhe = blockchain.tianhe || {};
const clientOrigin = process.env.CLIENT_ORIGIN || runtime.clientOrigin || 'http://localhost:5174';
return {
appName: process.env.APP_NAME || '艺术品溯源平台',
appName: process.env.APP_NAME || runtime.appName || '艺术品溯源平台',
env: process.env.NODE_ENV || 'development',
port: parseInt(process.env.API_PORT || '3000', 10),
baseUrl: process.env.BASE_URL || 'http://localhost:3000',
port: parseInt(process.env.API_PORT || String(runtime.port || 3000), 10),
baseUrl: process.env.BASE_URL || runtime.baseUrl || 'http://localhost:3000',
publicTracePath: process.env.PUBLIC_TRACE_PATH || '/t',
adminOrigin: process.env.ADMIN_ORIGIN || 'http://localhost:5173',
adminOrigin: process.env.ADMIN_ORIGIN || runtime.adminOrigin || 'http://localhost:5173',
clientOrigin,
// 短链跳转到 C 端溯源页的地址模板,{code} 会被替换为短码
traceUrlTemplate: process.env.TRACE_URL_TEMPLATE || `${clientOrigin}/#/trace/{code}`,
jwt: {
secret: process.env.JWT_SECRET || 'artveris_dev_secret',
expiresIn: process.env.JWT_EXPIRES_IN || '7d',
secret: process.env.JWT_SECRET || runtime.jwt?.secret || 'artveris_dev_secret',
expiresIn: process.env.JWT_EXPIRES_IN || runtime.jwt?.expiresIn || '7d',
},
settingsEncryptionKey:
process.env.SETTING_ENCRYPTION_KEY ||
runtime.settingsEncryptionKey ||
process.env.JWT_SECRET ||
runtime.jwt?.secret ||
'artveris_dev_secret',
redis: {
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT || '6379', 10),
password: process.env.REDIS_PASSWORD || undefined,
host: process.env.REDIS_HOST || runtime.redis?.host || 'localhost',
port: parseInt(process.env.REDIS_PORT || String(runtime.redis?.port || 6379), 10),
password: process.env.REDIS_PASSWORD || runtime.redis?.password || undefined,
},
storage: {
driver: (process.env.STORAGE_DRIVER as 'local' | 'oss') || 'local',
localDir: process.env.STORAGE_LOCAL_DIR || './storage',
publicPrefix: process.env.STORAGE_PUBLIC_PREFIX || '/uploads',
cdnBaseUrl: process.env.CDN_BASE_URL || '',
driver: (process.env.STORAGE_DRIVER as 'local' | 'oss') || storage.driver || 'local',
localDir: process.env.STORAGE_LOCAL_DIR || storage.localDir || './storage',
publicPrefix: process.env.STORAGE_PUBLIC_PREFIX || storage.publicPrefix || '/uploads',
cdnBaseUrl: process.env.CDN_BASE_URL || storage.cdnBaseUrl || '',
oss: {
endpoint: process.env.OSS_ENDPOINT || '',
region: process.env.OSS_REGION || '',
bucket: process.env.OSS_BUCKET || '',
accessKeyId: process.env.OSS_ACCESS_KEY_ID || '',
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET || '',
endpoint: process.env.OSS_ENDPOINT || oss.endpoint || '',
region: process.env.OSS_REGION || oss.region || '',
bucket: process.env.OSS_BUCKET || oss.bucket || '',
accessKeyId: process.env.OSS_ACCESS_KEY_ID || oss.accessKeyId || '',
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET || oss.accessKeySecret || '',
},
},
blockchain: {
driver: (process.env.BLOCKCHAIN_DRIVER as 'mock' | 'tianhe') || 'mock',
driver: (process.env.BLOCKCHAIN_DRIVER as 'mock' | 'tianhe') || blockchain.driver || 'mock',
tianhe: {
baseUrl: process.env.TIANHE_BASE_URL || 'https://test.api.tichain.tianhecloud.com',
appId: process.env.TIANHE_APP_ID || '',
appKey: process.env.TIANHE_APP_KEY || '',
userId: process.env.TIANHE_USER_ID || '',
userKey: process.env.TIANHE_USER_KEY || '',
baseUrl: process.env.TIANHE_BASE_URL || tianhe.baseUrl || 'https://test.api.tichain.tianhecloud.com',
appId: process.env.TIANHE_APP_ID || tianhe.appId || '',
appKey: process.env.TIANHE_APP_KEY || tianhe.appKey || '',
userId: process.env.TIANHE_USER_ID || tianhe.userId || '',
userKey: process.env.TIANHE_USER_KEY || tianhe.userKey || '',
},
},
seedAdmin: {
+90
View File
@@ -0,0 +1,90 @@
import { existsSync, readFileSync } from 'fs';
import { dirname, resolve } from 'path';
export interface RuntimeConfigFile {
appName?: string;
port?: number;
baseUrl?: string;
adminOrigin?: string;
clientOrigin?: string;
databaseUrl?: string;
redis?: { host?: string; port?: number; password?: string };
jwt?: { secret?: string; expiresIn?: string };
settingsEncryptionKey?: string;
storage?: {
driver?: 'local' | 'oss';
localDir?: string;
publicPrefix?: string;
cdnBaseUrl?: string;
oss?: {
endpoint?: string;
region?: string;
bucket?: string;
accessKeyId?: string;
accessKeySecret?: string;
};
};
backupDir?: string;
blockchain?: {
driver?: 'mock' | 'tianhe';
tianhe?: { baseUrl?: string; appId?: string; appKey?: string; userId?: string; userKey?: string };
};
}
export function runtimeRoot(): string {
return resolve(process.env.ARTVERIS_RUNTIME_DIR || process.env.RUNTIME_DIR || resolve(process.cwd(), 'runtime'));
}
export function runtimeConfigPath(): string {
return resolve(process.env.ARTVERIS_CONFIG_PATH || resolve(runtimeRoot(), 'artveris.config.json'));
}
export function runtimeEnvPath(): string {
return resolve(dirname(runtimeConfigPath()), '.env');
}
export function loadRuntimeConfig(): RuntimeConfigFile {
const path = runtimeConfigPath();
if (!existsSync(path)) return {};
try {
return JSON.parse(readFileSync(path, 'utf8')) as RuntimeConfigFile;
} catch {
return {};
}
}
function loadEnvFile(path: string): void {
if (!existsSync(path)) return;
const text = readFileSync(path, 'utf8');
for (const rawLine of text.split(/\r?\n/)) {
const line = rawLine.trim();
if (!line || line.startsWith('#')) continue;
const eq = line.indexOf('=');
if (eq <= 0) continue;
const key = line.slice(0, eq).trim();
if (process.env[key] !== undefined) continue;
let value = line.slice(eq + 1).trim();
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
value = value.slice(1, -1);
}
process.env[key] = value;
}
}
export function preloadRuntimeEnv(): void {
loadEnvFile(resolve(process.cwd(), '.env'));
loadEnvFile(resolve(process.cwd(), '../../.env'));
loadEnvFile(runtimeEnvPath());
}
export function hasRuntimeConfig(): boolean {
return existsSync(runtimeConfigPath());
}
export function isInstallMode(): boolean {
preloadRuntimeEnv();
if (process.env.ARTVERIS_INSTALL_MODE === '1') return true;
if (process.env.ARTVERIS_INSTALL_MODE === '0') return false;
if (process.env.DATABASE_URL) return false;
return !hasRuntimeConfig();
}