Refactoring the backend

This commit is contained in:
jiangrui
2025-03-10 18:33:47 +08:00
parent 755a424530
commit a78ea7e5bd
36 changed files with 974 additions and 474 deletions

View File

@@ -0,0 +1,21 @@
export class ApiResponse<T> {
success: boolean;
data?: T;
message?: string;
code: number;
private constructor(success: boolean, code: number, data?: T, message?: string) {
this.success = success;
this.code = code;
this.data = data;
this.message = message;
}
static success<T>(data?: T, message = "操作成功"): ApiResponse<T> {
return new ApiResponse(true, 200, data, message);
}
static error(message: string, code = 500): ApiResponse<null> {
return new ApiResponse(false, code, null, message);
}
}

View File

@@ -0,0 +1,19 @@
export class ServiceRegistry {
private static instance: ServiceRegistry;
private services: Map<string, any> = new Map();
static getInstance(): ServiceRegistry {
if (!ServiceRegistry.instance) {
ServiceRegistry.instance = new ServiceRegistry();
}
return ServiceRegistry.instance;
}
register(name: string, service: any): void {
this.services.set(name, service);
}
get<T>(name: string): T {
return this.services.get(name);
}
}

View File

@@ -0,0 +1,16 @@
import { Container } from "inversify";
import { TYPES } from "./types";
import { Cloud115Service } from "../services/Cloud115Service";
import { QuarkService } from "../services/QuarkService";
import { Searcher } from "../services/Searcher";
import { DatabaseService } from "../services/DatabaseService";
const container = new Container();
// 注册服务
container.bind<Cloud115Service>(TYPES.Cloud115Service).to(Cloud115Service);
container.bind<QuarkService>(TYPES.QuarkService).to(QuarkService);
container.bind<Searcher>(TYPES.Searcher).to(Searcher);
container.bind<DatabaseService>(TYPES.DatabaseService).to(DatabaseService);
export { container };

18
backend/src/core/types.ts Normal file
View File

@@ -0,0 +1,18 @@
export const TYPES = {
DatabaseService: Symbol.for("DatabaseService"),
Cloud115Service: Symbol.for("Cloud115Service"),
QuarkService: Symbol.for("QuarkService"),
Searcher: Symbol.for("Searcher"),
DoubanService: Symbol.for("DoubanService"),
ImageService: Symbol.for("ImageService"),
SettingService: Symbol.for("SettingService"),
UserService: Symbol.for("UserService"),
Cloud115Controller: Symbol.for("Cloud115Controller"),
QuarkController: Symbol.for("QuarkController"),
ResourceController: Symbol.for("ResourceController"),
DoubanController: Symbol.for("DoubanController"),
ImageController: Symbol.for("ImageController"),
SettingController: Symbol.for("SettingController"),
UserController: Symbol.for("UserController"),
};