Files
qipai/backend/src/server.ts
T
2026-06-25 11:51:02 +08:00

195 lines
6.8 KiB
TypeScript

import { buildApp } from './app.js';
import { loadConfig } from './config.js';
import { closeMySqlPool, createMySqlPool } from './db/mysql.js';
import { PlatformConfigRepository } from './tenancy/platform-config-repository.js';
import { AuthRepository } from './auth/auth-repository.js';
import { RbacRepository } from './auth/rbac-repository.js';
import { parseWechatAppSecrets, WechatHttpClient } from './auth/wechat-client.js';
import { UserManagementRepository } from './auth/user-management-repository.js';
import { StoreRoomRepository } from './stores/store-room-repository.js';
import { ContentRepository } from './content/content-repository.js';
import { MediaStorage } from './content/media-storage.js';
import { resolve } from 'node:path';
import { StoreDiscoveryRepository } from './stores/store-discovery-repository.js';
import { StoreAccessRepository } from './stores/access-repository.js';
import { PricingRepository } from './orders/pricing-repository.js';
import { OrderStateRepository } from './orders/order-state-repository.js';
import { OrderManagementRepository } from './orders/order-management-repository.js';
import { OrderShareRepository } from './orders/order-share-repository.js';
import { OrderQueryRepository } from './orders/order-query-repository.js';
import { PaymentRepository } from './payments/payment-repository.js';
import {
FetchWechatPayTransport, parseWechatPayCredentials, WechatPayClient
} from './payments/wechat-pay-client.js';
import { WechatPaymentService } from './payments/wechat-payment-service.js';
import { ProfitSharingService } from './payments/profit-sharing-service.js';
import {
FetchThirdPartyTransport, parseThirdPartyCredentials, ThirdPartyClient
} from './third-party/third-party-client.js';
import { ThirdPartyService } from './third-party/third-party-service.js';
import { MqttService } from './mqtt/mqtt-service.js';
import { DeviceRepository } from './devices/device-repository.js';
import { CustomerDeviceAccessRepository } from './devices/customer-device-access-repository.js';
import { IotMessageService } from './devices/iot-message-service.js';
import { DeviceCommandService } from './devices/device-command-service.js';
import { DeviceControlService } from './devices/device-control-service.js';
import { MemberProfileService } from './wallets/member-profile-service.js';
import { RechargeService } from './wallets/recharge-service.js';
import { WalletLedgerService } from './wallets/wallet-ledger-service.js';
const config = loadConfig();
const pool = createMySqlPool(config);
const authRepository = new AuthRepository(pool);
const accessControl = new RbacRepository(pool);
const orderManagementRepository = new OrderManagementRepository(pool);
const paymentRepository = new PaymentRepository(pool);
const walletLedgerService = new WalletLedgerService(pool);
const wechatCredentials = parseWechatPayCredentials(config.payment.wechatCredentialsJson);
const wechatPayClient = new WechatPayClient(new FetchWechatPayTransport());
const thirdPartyCredentials = parseThirdPartyCredentials(config.thirdParty.credentialsJson);
const iotMessages = new IotMessageService(pool);
const mqtt = new MqttService(config.mqtt, undefined, (topic, payload) =>
iotMessages.handle(topic, payload)
);
const deviceCommands = new DeviceCommandService(iotMessages, mqtt);
const app = await buildApp({
config,
mqtt,
platformConfigRepository: new PlatformConfigRepository(pool),
auth: {
repository: authRepository,
wechat: new WechatHttpClient(parseWechatAppSecrets(config.auth.wechatAppSecretsJson)),
jwtSecret: config.auth.jwtSecret,
accessTokenTtlSeconds: config.auth.accessTokenTtlSeconds,
sessionTtlSeconds: config.auth.sessionTtlSeconds,
accessControl
},
userManagement: {
repository: new UserManagementRepository(pool),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
storeRoom: {
repository: new StoreRoomRepository(pool),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
content: {
repository: new ContentRepository(pool),
mediaStorage: new MediaStorage(resolve(process.cwd(), 'shared', 'uploads')),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
storeDiscovery: {
repository: new StoreDiscoveryRepository(pool),
tenancy: authRepository
},
storeAccess: {
repository: new StoreAccessRepository(pool),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
pricing: {
repository: new PricingRepository(pool),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
orderState: {
repository: new OrderStateRepository(pool),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret,
cancellationPolicy: orderManagementRepository
},
orderQuery: {
repository: new OrderQueryRepository(pool),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
orderManagement: {
repository: orderManagementRepository,
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
orderShare: {
repository: new OrderShareRepository(pool),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
payment: {
repository: paymentRepository,
wechat: new WechatPaymentService(
pool,
paymentRepository,
wechatPayClient,
wechatCredentials
),
profitSharing: new ProfitSharingService(
pool,
wechatPayClient,
wechatCredentials,
config.payment.profitShareMockEnabled
),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret,
testAdapterEnabled: config.payment.testAdapterEnabled
},
thirdParty: {
service: new ThirdPartyService(
pool,
new PricingRepository(pool),
new ThirdPartyClient(new FetchThirdPartyTransport()),
thirdPartyCredentials
),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
devices: {
repository: new DeviceRepository(pool),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
deviceControl: {
service: new DeviceControlService(pool, deviceCommands),
customerAccess: new CustomerDeviceAccessRepository(pool),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
members: {
service: new MemberProfileService(pool),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
},
recharge: {
service: new RechargeService(pool, walletLedgerService),
authRepository,
accessControl,
jwtSecret: config.auth.jwtSecret
}
});
app.addHook('onClose', async () => {
await mqtt.stop();
await closeMySqlPool(pool);
});
try {
mqtt.start();
await app.listen({ host: config.host, port: config.port });
} catch (error) {
app.log.error(error);
process.exit(1);
}