feat: Complete NestJS backend scaffold — 22 modules, 39 entities, WebSocket gateway
All checks were successful
Test Asgard Runner / test (push) Successful in 3s
All checks were successful
Test Asgard Runner / test (push) Successful in 3s
Full backend rewrite from Rust/Axum to NestJS/TypeScript. - 22 feature modules (auth, servers, wipes, maps, plugins, players, console, chat, team, notifications, settings, schedules, analytics, alerts, status, store, webstore, admin, setup, migration, users, licenses) - 39 TypeORM entities matching PostgreSQL schema (12 migrations) - Common infrastructure: JWT/RBAC guards, decorators, exception filter - NATS service with pub/sub/request-reply - Socket.IO WebSocket gateway with NATS bridge - Docker: NestJS Dockerfile + updated docker-compose.yml - Zero compile errors (npx tsc --noEmit clean) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
73
backend-nest/src/services/nats.service.ts
Normal file
73
backend-nest/src/services/nats.service.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { Injectable, OnModuleInit, OnModuleDestroy, Logger } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { connect, NatsConnection, StringCodec, Subscription } from 'nats';
|
||||
|
||||
@Injectable()
|
||||
export class NatsService implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(NatsService.name);
|
||||
private nc: NatsConnection | null = null;
|
||||
private sc = StringCodec();
|
||||
|
||||
constructor(private config: ConfigService) {}
|
||||
|
||||
async onModuleInit() {
|
||||
try {
|
||||
const url = this.config.get<string>('nats.url') || 'nats://localhost:4222';
|
||||
this.nc = await connect({ servers: url });
|
||||
this.logger.log(`Connected to NATS at ${url}`);
|
||||
} catch (err) {
|
||||
this.logger.warn(`NATS connection failed — running in offline mode: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
if (this.nc) {
|
||||
await this.nc.drain();
|
||||
}
|
||||
}
|
||||
|
||||
async publish(subject: string, data: Record<string, unknown>): Promise<void> {
|
||||
if (!this.nc) {
|
||||
this.logger.debug(`[OFFLINE] Would publish to ${subject}: ${JSON.stringify(data)}`);
|
||||
return;
|
||||
}
|
||||
this.nc.publish(subject, this.sc.encode(JSON.stringify(data)));
|
||||
}
|
||||
|
||||
async request(subject: string, data: Record<string, unknown>, timeout = 5000): Promise<unknown> {
|
||||
if (!this.nc) {
|
||||
this.logger.debug(`[OFFLINE] Would request ${subject}: ${JSON.stringify(data)}`);
|
||||
return null;
|
||||
}
|
||||
const msg = await this.nc.request(subject, this.sc.encode(JSON.stringify(data)), { timeout });
|
||||
return JSON.parse(this.sc.decode(msg.data));
|
||||
}
|
||||
|
||||
subscribe(subject: string, callback: (data: unknown, subject: string) => void): Subscription | null {
|
||||
if (!this.nc) {
|
||||
this.logger.debug(`[OFFLINE] Would subscribe to ${subject}`);
|
||||
return null;
|
||||
}
|
||||
const sub = this.nc.subscribe(subject);
|
||||
(async () => {
|
||||
for await (const msg of sub) {
|
||||
try {
|
||||
const parsed = JSON.parse(this.sc.decode(msg.data));
|
||||
callback(parsed, msg.subject);
|
||||
} catch {
|
||||
callback(this.sc.decode(msg.data), msg.subject);
|
||||
}
|
||||
}
|
||||
})();
|
||||
return sub;
|
||||
}
|
||||
|
||||
/** Publish a command to a specific license's server */
|
||||
async sendServerCommand(licenseId: string, action: string, payload: Record<string, unknown> = {}): Promise<void> {
|
||||
await this.publish(`corrosion.${licenseId}.cmd.server`, {
|
||||
action,
|
||||
...payload,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user