mirror of
https://github.com/jiangrui1994/CloudSaver.git
synced 2026-01-12 16:18:45 +08:00
Refactoring the backend
This commit is contained in:
21
backend/src/core/ApiResponse.ts
Normal file
21
backend/src/core/ApiResponse.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
19
backend/src/core/ServiceRegistry.ts
Normal file
19
backend/src/core/ServiceRegistry.ts
Normal 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);
|
||||
}
|
||||
}
|
||||
16
backend/src/core/container.ts
Normal file
16
backend/src/core/container.ts
Normal 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
18
backend/src/core/types.ts
Normal 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"),
|
||||
};
|
||||
Reference in New Issue
Block a user