Skip to content

Commit

Permalink
Merge pull request #7 from hxg2050/dev
Browse files Browse the repository at this point in the history
add debug options and change package of handler apis
  • Loading branch information
hxg2050 authored Aug 27, 2024
2 parents 7d6c518 + e1c4bda commit e7225f2
Show file tree
Hide file tree
Showing 7 changed files with 1,519 additions and 1,863 deletions.
7 changes: 7 additions & 0 deletions .changeset/dull-seals-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@feiyun/handler": minor
"feiyun": minor
"@feiyun/server": minor
---

add debug,change apis
89 changes: 64 additions & 25 deletions packages/handler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "node:path";
import { plainToInstance } from "class-transformer";
import { validate } from "class-validator";

import type { Context, FeiyunMiddleware } from '@feiyun/server'
import type { Context, Feiyun, FeiyunMiddleware } from '@feiyun/server'
const PATH_METADATA = 'path';
const METHOD_METADATA = 'method';
const PARAM_METADATA = 'param';
Expand Down Expand Up @@ -76,7 +76,7 @@ export const ParamType: ParameterDecorator = (target, key, index) => {

const METHOD_DOC_METADATA = 'method_doc';

type Options = {
type DocOptions = {
/**
* 标题
*/
Expand All @@ -89,7 +89,7 @@ type Options = {
/**
* 文档
*/
export const ApiDoc = (options: Options): MethodDecorator => {
export const ApiDoc = (options: DocOptions): MethodDecorator => {
return (target, key, descriptor: any) => {
Reflect.defineMetadata(METHOD_DOC_METADATA, options, descriptor.value);
}
Expand Down Expand Up @@ -141,6 +141,21 @@ export class ResponseError extends Error {
super(msg);
}
}
export const getHandlers = async (rule = '**/*.handler.ts') => {
const handlerPaths = await glob(rule);

const handlers = [];

for (let i = 0; i < handlerPaths.length; i++) {
const handler = await import(handlerPaths[i]);
if (handler.default) {
handlers.push(handler.default);
}
}

return handlers;
}


const runHandler = async (handler: any, ctx: Context) => {
if (handler.validate) {
Expand Down Expand Up @@ -174,31 +189,55 @@ const runHandler = async (handler: any, ctx: Context) => {
}
}

/**
* 自动导入应用文件
* @param baseDir 应用文件夹
* @param rule 导入路径规则
* @returns
*/
export const include = async (rule = '**/*.handler.ts'): Promise<FeiyunMiddleware> => {
const handlerPaths = await glob(rule);
export type IncludeOptions = {
docPath: string
}

const handlers = [];
export type HandlerOptoons = { rule: string } & IncludeOptions;

for (let i = 0; i < handlerPaths.length; i++) {
const handler = await import(handlerPaths[i]);
if (handler.default) {
handlers.push(handler.default);
export const createHandler = (app: Feiyun, options: HandlerOptoons) => {
options = Object.assign({}, {
docPath: 'doc'
}, options)
const isDebug = app.config.debug;


/**
* 自动导入应用文件
* @param baseDir 应用文件夹
* @param rule 导入路径规则
* @returns
*/
const include = async (): Promise<FeiyunMiddleware> => {

const handlers = await getHandlers(options.rule);

const mapRoute = useMapRoute(handlers);

const apis: {
path: string,
doc: DocOptions
}[] = [];
if (app.config.debug && options.docPath) {
mapRoute.forEach((handler, path) => {
apis.push({
path,
doc: handler.doc
});
});
}
}
return async (ctx, next) => {
const handler = mapRoute.get(ctx.request.url);
if (handler) {
await runHandler(handler, ctx);
}

const mapRoute = useMapRoute(handlers);
if (app.config.debug && ctx.request.url === options.docPath) {
ctx.response.data = apis;
}
next();
};
}

return async (ctx, next) => {
const handler = mapRoute.get(ctx.request.url);
if (handler) {
await runHandler(handler, ctx);
}
next();
};
return include();
}
3 changes: 2 additions & 1 deletion packages/server/src/IServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Socket } from "./socket"

export interface IServer {

isDebug: boolean;

start(): void

handlerCallback?: (client: Socket, data: string) => void
Expand Down
4 changes: 3 additions & 1 deletion packages/server/src/feiyun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { IServer } from './IServer'
export interface ApplicationConfig {
host: string
port: number
customServer?: (config: ApplicationConfig) => IServer
customServer?: (config: ApplicationConfig) => IServer,
debug: boolean
}

export class Request {
Expand All @@ -33,6 +34,7 @@ export class Feiyun {
public config: ApplicationConfig = {
host: '0.0.0.0',
port: 3000,
debug: false
}

public server!: IServer
Expand Down
9 changes: 0 additions & 9 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,4 @@ export class Server implements IServer {
}

isDebug = false

doc?: string

debug(isDebug: boolean, config: {
doc?: string
}) {
this.isDebug = isDebug
this.doc = config.doc
}
}
2 changes: 2 additions & 0 deletions packages/server/src/tcp/TcpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ export class TcpServer extends BaseServer<TcpSocket<SocketData>> implements ISer
send(socket: TcpSocket<SocketData>, name: string | number, data: any): void {
socket.write(JSON.stringify([1, name, data]));
}

isDebug: boolean = false;
}
Loading

0 comments on commit e7225f2

Please sign in to comment.