|
30 | 30 | cache cache.Cache |
31 | 31 | OfflineServer servers.Server |
32 | 32 | Config *config.Config |
| 33 | + Mock Mock |
33 | 34 | Middlewares []Middleware |
34 | 35 | ExceptionHandler ExceptionHandle |
35 | 36 | NotFoundHandler StandardHandle // NotFoundHandler 支持自定义404处理代码能力 |
@@ -214,6 +215,11 @@ func (app *DotWeb) UseTimeoutHook(handler StandardHandle, timeout time.Duration) |
214 | 215 | }) |
215 | 216 | } |
216 | 217 |
|
| 218 | +// SetMock set mock logic |
| 219 | +func (app *DotWeb) SetMock(mock Mock){ |
| 220 | + app.Mock = mock |
| 221 | +} |
| 222 | + |
217 | 223 | // SetExceptionHandle set custom error handler |
218 | 224 | func (app *DotWeb) SetExceptionHandle(handler ExceptionHandle) { |
219 | 225 | app.ExceptionHandler = handler |
@@ -310,6 +316,11 @@ func (app *DotWeb) ListenAndServe(addr string) error { |
310 | 316 | app.IncludeDotwebGroup() |
311 | 317 | } |
312 | 318 |
|
| 319 | + //special, if run mode is not develop, auto stop mock |
| 320 | + if app.RunMode() != RunMode_Development{ |
| 321 | + app.Mock = nil |
| 322 | + } |
| 323 | + |
313 | 324 | if app.HttpServer.ServerConfig().EnabledTLS { |
314 | 325 | err := app.HttpServer.ListenAndServeTLS(addr, app.HttpServer.ServerConfig().TLSCertFile, app.HttpServer.ServerConfig().TLSKeyFile) |
315 | 326 | return err |
@@ -576,6 +587,12 @@ func (app *DotWeb) DefaultMethodNotAllowedHandler(ctx Context) { |
576 | 587 | ctx.WriteStringC(http.StatusMethodNotAllowed, http.StatusText(http.StatusMethodNotAllowed)) |
577 | 588 | } |
578 | 589 |
|
| 590 | +func DefaultTimeoutHookHandler(ctx Context){ |
| 591 | + realDration := ctx.Items().GetTimeDuration(ItemKeyHandleDuration) |
| 592 | + logs := fmt.Sprintf("req %v, cost %v", ctx.Request().Url(), realDration.Seconds()) |
| 593 | + logger.Logger().Warn(logs, LogTarget_RequestTimeout) |
| 594 | +} |
| 595 | + |
579 | 596 | // Close immediately stops the server. |
580 | 597 | // It internally calls `http.Server#Close()`. |
581 | 598 | func (app *DotWeb) Close() error { |
|
0 commit comments