cgserver 9.1.10 → 9.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ 9.1.11
2
+ 1、部分httpserver的代码整理
1
3
  9.1.10
2
4
  1、删除一些无用配置和代码
3
5
  2、整理现有的代码,增加一些人性化的提示
@@ -85,61 +85,73 @@ class Engine {
85
85
  credentials: this._cfg.cors.credentials || false
86
86
  }));
87
87
  }
88
- this._app.all("/*", (_req, _res) => {
89
- this._all(_req, _res).catch((reason) => {
90
- Log_1.GLog.error(reason);
91
- let res = new Response_1.Response(_res, this._cfg);
92
- let method = _req.method.toLowerCase();
93
- if (method == "post") {
94
- res.renderJson500();
95
- }
96
- else if (method == "get") {
97
- res.render500();
88
+ this._app.all("/*", (req, res) => {
89
+ let time = Date.now();
90
+ this._onall(req, res).then(() => {
91
+ if (this._cfg.debug) {
92
+ let time_str = (Date.now() - time) + "ms";
93
+ Log_1.GLog.info("[" + time_str + "] " + req.method + " " + req.url);
98
94
  }
95
+ }).catch(() => {
96
+ res.sendStatus(500);
97
+ let exreq = new Request_1.Request(req, this._cfg);
98
+ let info = exreq.getDebugInfo();
99
+ info["tip"] = "server error";
100
+ Log_1.GLog.error(info);
99
101
  });
100
102
  });
101
103
  }
102
- async _all(_req, _res) {
104
+ async _onall(_req, _res) {
105
+ let httpinfo = this._preDoHttpInfo(_req, _res);
106
+ if (!httpinfo) {
107
+ return;
108
+ }
109
+ await this._callCtrAction(httpinfo.action_name, httpinfo.req, httpinfo.res);
110
+ }
111
+ _method_preactions = {
112
+ "on": "on",
113
+ "show": "show",
114
+ "onoptions": "onoptions",
115
+ "onhead": "onhead"
116
+ };
117
+ /**
118
+ * 预处理http请求信息
119
+ * @param _req
120
+ * @param _res
121
+ * @returns
122
+ */
123
+ _preDoHttpInfo(_req, _res) {
103
124
  let req = new Request_1.Request(_req, this._cfg);
104
125
  let res = new Response_1.Response(_res, this._cfg);
105
- let method = req.method.toLowerCase();
106
126
  //禁止访问
107
127
  if (!this._is_running) {
108
128
  res.baseRes.sendStatus(403);
109
129
  return;
110
130
  }
111
- if (method == "options") {
112
- _res.sendStatus(200);
113
- return;
114
- }
115
- if (method == "head") {
116
- _res.sendStatus(200);
117
- return;
118
- }
119
- if (method != "get" && method != "post") {
120
- return;
121
- }
122
- let pre_action = "show";
123
- let action_name = "";
124
- if (method == "post") {
125
- pre_action = "on";
126
- }
127
- //大小写还原
128
- action_name = ControllerManager_1.GCtrMgr.getActionName(req.module, req.controller, pre_action + req.action);
129
- //尝试一次on,变态支持
130
- if (!action_name && method == "get") {
131
- pre_action = "on";
132
- action_name = ControllerManager_1.GCtrMgr.getActionName(req.module, req.controller, pre_action + req.action);
131
+ let method = req.method.toLocaleLowerCase();
132
+ let pre_action = this._method_preactions[method];
133
+ if (!pre_action) {
134
+ res.baseRes.sendStatus(500);
135
+ let info = req.getDebugInfo();
136
+ info["tip"] = "not support method:" + method;
137
+ Log_1.GLog.error(info);
138
+ return null;
133
139
  }
140
+ let action_name = ControllerManager_1.GCtrMgr.getActionName(req.module, req.controller, pre_action + req.action);
134
141
  if (!action_name) {
135
- if (method == "get") {
136
- res.render404();
137
- }
138
- else if (method == "post") {
139
- res.renderJson404();
140
- }
141
- return;
142
- }
142
+ res.baseRes.sendStatus(500);
143
+ let info = req.getDebugInfo();
144
+ info["tip"] = "request has no action";
145
+ Log_1.GLog.error(info);
146
+ return null;
147
+ }
148
+ return {
149
+ action_name: action_name,
150
+ req: req,
151
+ res: res
152
+ };
153
+ }
154
+ async _callCtrAction(action_name, req, res) {
143
155
  let ctr = ControllerManager_1.GCtrMgr.getStaticCtr(req.module, req.controller);
144
156
  let cls_ctr = null;
145
157
  if (!ctr) {
@@ -206,10 +206,7 @@ class Request {
206
206
  this._params = this._params || {};
207
207
  return;
208
208
  }
209
- debugInfo() {
210
- if (!this._cfg.debug) {
211
- return;
212
- }
209
+ getDebugInfo() {
213
210
  let debuginfo = {
214
211
  module: this._module,
215
212
  controller: this._controller,
@@ -220,6 +217,13 @@ class Request {
220
217
  post: this.postData,
221
218
  method: this._req.method.toLowerCase()
222
219
  };
220
+ return debuginfo;
221
+ }
222
+ debugInfo(force = false) {
223
+ if (!this._cfg.debug && !force) {
224
+ return;
225
+ }
226
+ let debuginfo = this.getDebugInfo();
223
227
  Log_1.GLog.info(debuginfo);
224
228
  }
225
229
  }
@@ -17,7 +17,25 @@ export declare class Engine {
17
17
  get isrunning(): boolean;
18
18
  constructor(cfg: WebServerConfig, razorJs: RazorJs);
19
19
  start(): void;
20
- protected _all(_req: Express.Request, _res: Express.Response): Promise<void>;
20
+ protected _onall(_req: Express.Request, _res: Express.Response): Promise<void>;
21
+ protected _method_preactions: {
22
+ on: string;
23
+ show: string;
24
+ onoptions: string;
25
+ onhead: string;
26
+ };
27
+ /**
28
+ * 预处理http请求信息
29
+ * @param _req
30
+ * @param _res
31
+ * @returns
32
+ */
33
+ protected _preDoHttpInfo(_req: Express.Request, _res: Express.Response): {
34
+ action_name: string;
35
+ req: Request;
36
+ res: Response;
37
+ };
38
+ protected _callCtrAction(action_name: string, req: Request, res: Response): Promise<void>;
21
39
  pause(): void;
22
40
  resume(): void;
23
41
  getRenderHtml(req: Request, res: Response, datas: any): any;
@@ -36,5 +36,15 @@ export declare class Request {
36
36
  protected _init(): void;
37
37
  protected _addListener(event: string | symbol, listener: (...args: any[]) => void): void;
38
38
  protected _parseParams(path: string): void;
39
- debugInfo(): void;
39
+ getDebugInfo(): {
40
+ module: string;
41
+ controller: string;
42
+ action: string;
43
+ suffix: string;
44
+ file_url: any;
45
+ params: any;
46
+ post: any;
47
+ method: string;
48
+ };
49
+ debugInfo(force?: boolean): void;
40
50
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cgserver",
3
- "version": "9.1.10",
3
+ "version": "9.1.11",
4
4
  "author": "trojan",
5
5
  "type": "commonjs",
6
6
  "description": "free for all.Websocket or Http",