mirror of
https://github.com/laochan-eacnet/backend.git
synced 2026-10-07 05:57:57 +03:00
rename functions to services due to layout change
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
import { singleton } from 'tsyringe';
|
||||
import { Context, ILaochanService } from '../types.js';
|
||||
import { kxml } from '../decorators/to-kxml.js';
|
||||
|
||||
@singleton()
|
||||
export class DefaultService implements ILaochanService {
|
||||
@kxml()
|
||||
async process(ctx: Context) {
|
||||
const [module, method] = ctx.service.split('.');
|
||||
|
||||
return {
|
||||
[module]: {
|
||||
$method: method,
|
||||
status: '0',
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import { kxmlTemplate } from '../../decorators/render-kxml.js';
|
||||
import { ILaochanService } from '../../types.js';
|
||||
import { singleton } from 'tsyringe';
|
||||
|
||||
@singleton()
|
||||
export default class implements ILaochanService {
|
||||
@kxmlTemplate('message/get')
|
||||
async process(): Promise<object> {
|
||||
return {
|
||||
status: 0,
|
||||
expire: 60,
|
||||
};
|
||||
}
|
||||
}
|
||||
+33
-15
@@ -3,13 +3,14 @@ import 'reflect-metadata';
|
||||
import Koa, { DefaultState } from 'koa';
|
||||
import { InjectionToken, Provider, container } from 'tsyringe';
|
||||
|
||||
import { ILaochanContext, ILaochanService } from './types.js';
|
||||
import { DefaultService } from './functions/default.js';
|
||||
import { ILaochanContext } from './types.js';
|
||||
import { DefaultService } from './services/default.js';
|
||||
import { Logger } from '@cordisjs/logger';
|
||||
import { crypto } from './middlewares/crypto.js';
|
||||
import { binaryBody } from './middlewares/binarybody.js';
|
||||
import { parseKxml } from './middlewares/parseKxml.js';
|
||||
import { decompress } from './middlewares/decompress.js';
|
||||
import config from './utils/config.js';
|
||||
|
||||
function tryResolve<T>(token: InjectionToken<T>): T | undefined {
|
||||
if (!container.isRegistered(token)) return undefined;
|
||||
@@ -57,24 +58,39 @@ async function main(): Promise<void> {
|
||||
throw new Error('invaild model');
|
||||
}
|
||||
|
||||
let [serviceName, method] = ctx.query.f.split('.');
|
||||
|
||||
ctx.model = ctx.query.model;
|
||||
ctx.service = ctx.query.f;
|
||||
ctx.service = {
|
||||
name: serviceName,
|
||||
method,
|
||||
};
|
||||
|
||||
ctx.logger = ctx.service
|
||||
? register(`logger:${ctx.service}`, {
|
||||
useValue: new Logger(ctx.service),
|
||||
? register(`logger:${ctx.service.name}`, {
|
||||
useValue: new Logger(ctx.service.name),
|
||||
})
|
||||
: logger;
|
||||
|
||||
let service: ILaochanService | undefined = ctx.service
|
||||
? tryResolve(ctx.service)
|
||||
let service: object | undefined = ctx.service
|
||||
? tryResolve(ctx.service.name)
|
||||
: container.resolve(DefaultService);
|
||||
|
||||
if (service === undefined) {
|
||||
try {
|
||||
const module = await import(
|
||||
`./functions/${ctx.service.replace('.', '/')}.js`
|
||||
);
|
||||
service = register(ctx.service, {
|
||||
let module: any = null;
|
||||
|
||||
try {
|
||||
module = await import(
|
||||
`./services/${ctx.service.name}/index.js`
|
||||
);
|
||||
} catch {
|
||||
module = await import(
|
||||
`./services/${ctx.service.name}.js`
|
||||
);
|
||||
}
|
||||
|
||||
service = register(ctx.service.name, {
|
||||
useClass: module.default,
|
||||
});
|
||||
} catch (e) {
|
||||
@@ -85,9 +101,11 @@ async function main(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
if (service === undefined) {
|
||||
logger.warn(`unimplemented service ${ctx.service}`);
|
||||
if (service === undefined || !(method in service)) {
|
||||
logger.warn(`unimplemented method ${ctx.service.method} in ${ctx.service.name}`);
|
||||
|
||||
service = container.resolve(DefaultService);
|
||||
method = 'default';
|
||||
}
|
||||
|
||||
logger.info(
|
||||
@@ -97,14 +115,14 @@ async function main(): Promise<void> {
|
||||
JSON.stringify(ctx.request.body),
|
||||
);
|
||||
|
||||
ctx.body = await service.process(ctx);
|
||||
ctx.body = await service[method](ctx);
|
||||
ctx.status = 200;
|
||||
|
||||
logger.info('%s [server]: status = %d', ctx.service, ctx.status);
|
||||
return await next();
|
||||
});
|
||||
|
||||
app.listen(10573);
|
||||
app.listen(config.port);
|
||||
}
|
||||
|
||||
main();
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { singleton } from 'tsyringe';
|
||||
import { Context } from '../types.js';
|
||||
import { kxml } from '../decorators/to-kxml.js';
|
||||
|
||||
@singleton()
|
||||
export class DefaultService {
|
||||
@kxml()
|
||||
async default(ctx: Context) {
|
||||
return {
|
||||
[ctx.service.name]: {
|
||||
$method: ctx.service.method,
|
||||
status: '0',
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
import { kxmlTemplate } from '../../decorators/render-kxml.js';
|
||||
import { ILaochanService } from '../../types.js';
|
||||
import { singleton } from 'tsyringe';
|
||||
|
||||
@singleton()
|
||||
export default class implements ILaochanService {
|
||||
export default class {
|
||||
@kxmlTemplate('facility/get')
|
||||
async process(): Promise<object> {
|
||||
async get(): Promise<object> {
|
||||
return {
|
||||
status: 0,
|
||||
location: {
|
||||
@@ -0,0 +1,16 @@
|
||||
import { kxml } from '../../decorators/to-kxml.js';
|
||||
import { singleton } from 'tsyringe';
|
||||
|
||||
@singleton()
|
||||
export default class {
|
||||
@kxml()
|
||||
async get(): Promise<object> {
|
||||
return {
|
||||
message: {
|
||||
$status: 0,
|
||||
$expire: 60,
|
||||
$method: 'get',
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,4 +2,6 @@ import { singleton } from 'tsyringe';
|
||||
import { DefaultService } from '../default.js';
|
||||
|
||||
@singleton()
|
||||
export default class extends DefaultService {}
|
||||
export default class extends DefaultService {
|
||||
list = this.default;
|
||||
}
|
||||
@@ -2,4 +2,6 @@ import { singleton } from 'tsyringe';
|
||||
import { DefaultService } from '../default.js';
|
||||
|
||||
@singleton()
|
||||
export default class extends DefaultService {}
|
||||
export default class extends DefaultService {
|
||||
put = this.default;
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
import { kxml } from '../../decorators/to-kxml.js';
|
||||
import { ILaochanService } from '../../types.js';
|
||||
import { singleton } from 'tsyringe';
|
||||
|
||||
@singleton()
|
||||
export default class implements ILaochanService {
|
||||
export default class {
|
||||
@kxml()
|
||||
async process(): Promise<object> {
|
||||
return {
|
||||
@@ -1,12 +1,11 @@
|
||||
import { ILaochanService } from '../../types.js';
|
||||
import { singleton } from 'tsyringe';
|
||||
import config from '../../utils/config.js';
|
||||
import { kxml } from '../../decorators/to-kxml.js';
|
||||
|
||||
@singleton()
|
||||
export default class implements ILaochanService {
|
||||
export default class {
|
||||
@kxml()
|
||||
async process() {
|
||||
async get() {
|
||||
const selfServices = [
|
||||
'services', 'dlstatus', 'cardmng',
|
||||
'eacoin', 'userdata', 'userid', 'pcbtracker', 'pcbevent',
|
||||
+4
-5
@@ -3,7 +3,10 @@ import { DefaultState, ParameterizedContext } from 'koa';
|
||||
|
||||
export interface ILaochanContext {
|
||||
model?: string;
|
||||
service?: string;
|
||||
service?: {
|
||||
name: string,
|
||||
method: string,
|
||||
};
|
||||
pcbid?: string;
|
||||
logger: Logger;
|
||||
|
||||
@@ -13,7 +16,3 @@ export interface ILaochanContext {
|
||||
}
|
||||
|
||||
export type Context = ParameterizedContext<DefaultState, ILaochanContext, any>;
|
||||
|
||||
export interface ILaochanService {
|
||||
process(ctx: Context): Promise<any>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user