Skip to content

Commit 37aca3b

Browse files
committed
#### Version 1.5.7
* New Feature: Add integration Timeout Middleware, support DotWeb.UseTimeoutHook to use it * Detail: - Provide DefaultTimeoutHookHandler to simplify use, it will auto write log the req info which time out - Example: ``` golang app.UseTimeoutHook(dotweb.DefaultTimeoutHookHandler, time.Second * 2) ``` * New Feature: Add Mock module, support DotWeb.SetMock to use it * Detail: - Provide StandardMock to simplify use, it implement Mock interface - also you can create custom implement - you can register MockHandle or register return string - register key only support route - special: mock mode only effective in DevelopMode - Example: ``` golang func AppMock() dotweb.Mock{ m := dotweb.NewStandardMock() m.RegisterString("/", "mock data") return m } app.SetMock(AppMock()) ``` * 2018-08-22 10:00
1 parent 30b0f5e commit 37aca3b

8 files changed

Lines changed: 160 additions & 69 deletions

File tree

consts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const (
1010
LogTarget_Default = "dotweb_default"
1111
LogTarget_HttpRequest = "dotweb_request"
1212
LogTarget_HttpServer = "dotweb_server"
13+
LogTarget_RequestTimeout = "dotweb_req_timeout"
1314

1415
LogLevel_Debug = "debug"
1516
LogLevel_Info = "info"

dotweb.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type (
3030
cache cache.Cache
3131
OfflineServer servers.Server
3232
Config *config.Config
33+
Mock Mock
3334
Middlewares []Middleware
3435
ExceptionHandler ExceptionHandle
3536
NotFoundHandler StandardHandle // NotFoundHandler 支持自定义404处理代码能力
@@ -214,6 +215,11 @@ func (app *DotWeb) UseTimeoutHook(handler StandardHandle, timeout time.Duration)
214215
})
215216
}
216217

218+
// SetMock set mock logic
219+
func (app *DotWeb) SetMock(mock Mock){
220+
app.Mock = mock
221+
}
222+
217223
// SetExceptionHandle set custom error handler
218224
func (app *DotWeb) SetExceptionHandle(handler ExceptionHandle) {
219225
app.ExceptionHandler = handler
@@ -310,6 +316,11 @@ func (app *DotWeb) ListenAndServe(addr string) error {
310316
app.IncludeDotwebGroup()
311317
}
312318

319+
//special, if run mode is not develop, auto stop mock
320+
if app.RunMode() != RunMode_Development{
321+
app.Mock = nil
322+
}
323+
313324
if app.HttpServer.ServerConfig().EnabledTLS {
314325
err := app.HttpServer.ListenAndServeTLS(addr, app.HttpServer.ServerConfig().TLSCertFile, app.HttpServer.ServerConfig().TLSKeyFile)
315326
return err
@@ -576,6 +587,12 @@ func (app *DotWeb) DefaultMethodNotAllowedHandler(ctx Context) {
576587
ctx.WriteStringC(http.StatusMethodNotAllowed, http.StatusText(http.StatusMethodNotAllowed))
577588
}
578589

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+
579596
// Close immediately stops the server.
580597
// It internally calls `http.Server#Close()`.
581598
func (app *DotWeb) Close() error {

example/cache/main.go

Lines changed: 0 additions & 69 deletions
This file was deleted.

example/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ func main() {
6565
ctx.WriteJsonC(http.StatusInternalServerError, err.Error())
6666
})
6767

68+
//设置超时钩子事件,当请求超过指定时间阀值,会自动调用传入的函数
69+
//不会终止请求,只作为旁路执行
70+
app.UseTimeoutHook(dotweb.DefaultTimeoutHookHandler, time.Second * 2)
71+
6872
//设置HttpModule
6973
//InitModule(app)
7074

example/mock/main.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/devfeel/dotweb"
6+
"strconv"
7+
)
8+
9+
func main() {
10+
//初始化DotServer
11+
app := dotweb.New()
12+
13+
//设置dotserver日志目录
14+
//如果不设置,默认不启用,且默认为当前目录
15+
app.SetEnabledLog(true)
16+
17+
//开启development模式
18+
app.SetDevelopmentMode()
19+
20+
//设置Mock逻辑
21+
app.SetMock(AppMock())
22+
23+
//设置路由
24+
InitRoute(app.HttpServer)
25+
26+
// 开始服务
27+
port := 8080
28+
fmt.Println("dotweb.StartServer => " + strconv.Itoa(port))
29+
err := app.StartServer(port)
30+
fmt.Println("dotweb.StartServer error => ", err)
31+
}
32+
33+
func Index(ctx dotweb.Context) error {
34+
ctx.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
35+
err := ctx.WriteString("index => ", ctx.Request().Url())
36+
return err
37+
}
38+
39+
func InitRoute(server *dotweb.HttpServer) {
40+
server.Router().GET("/", Index)
41+
}
42+
43+
44+
func AppMock() dotweb.Mock{
45+
m := dotweb.NewStandardMock()
46+
m.RegisterString("/", "mock data")
47+
return m
48+
}

mock.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package dotweb
2+
3+
const(
4+
requestHeader_UseMock_Key = "dotweb_req_mock"
5+
requestHeader_UseMock_Flag = "true"
6+
)
7+
8+
9+
type MockHandle func(ctx Context)
10+
11+
type Mock interface{
12+
// Register reg mock handler on route
13+
Register(route string, handler MockHandle)
14+
// RegisterString reg mock return string on route
15+
RegisterString(route string, resData string)
16+
// CheckNeedMock check is need do mock logic
17+
CheckNeedMock(Context) bool
18+
// Do do mock logic
19+
Do(Context)
20+
}
21+
22+
type StandardMock struct{
23+
routeMap map[string]MockHandle
24+
}
25+
26+
func NewStandardMock() *StandardMock{
27+
return &StandardMock{routeMap:make(map[string]MockHandle)}
28+
}
29+
30+
func (m *StandardMock) CheckNeedMock(ctx Context) bool{
31+
if ctx.Request().QueryHeader(requestHeader_UseMock_Key) == requestHeader_UseMock_Flag{
32+
return true
33+
}
34+
return false
35+
}
36+
37+
func (m *StandardMock) Do(ctx Context){
38+
handler, exists:=m.routeMap[ctx.RouterNode().Node().fullPath]
39+
if exists{
40+
handler(ctx)
41+
}
42+
}
43+
44+
func (m *StandardMock) Register(route string, handler MockHandle){
45+
m.routeMap[route] = handler
46+
}
47+
48+
func (m *StandardMock) RegisterString(route string, resData string){
49+
m.routeMap[route] = func(ctx Context) {
50+
ctx.WriteString(resData)
51+
ctx.Response().SetHeader(requestHeader_UseMock_Key, requestHeader_UseMock_Flag)
52+
ctx.End()
53+
}
54+
}

router.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ func (r *router) wrapRouterHandle(handler HttpHandle, isHijack bool) RouterHandl
322322
}
323323
}()
324324

325+
326+
//do mock, special, mock will ignore all middlewares
327+
if r.server.DotApp.Mock != nil && r.server.DotApp.Mock.CheckNeedMock(httpCtx){
328+
r.server.DotApp.Mock.Do(httpCtx)
329+
if httpCtx.isEnd{
330+
return
331+
}
332+
}
333+
325334
//处理用户handle
326335
var ctxErr error
327336
//if len(r.server.DotApp.Middlewares) > 0 {

version.MD

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
## dotweb版本记录:
22

3+
4+
#### Version 1.5.7
5+
* New Feature: Add integration Timeout Middleware, support DotWeb.UseTimeoutHook to use it
6+
* Detail:
7+
- Provide DefaultTimeoutHookHandler to simplify use, it will auto write log the req info which time out
8+
- Example:
9+
``` golang
10+
app.UseTimeoutHook(dotweb.DefaultTimeoutHookHandler, time.Second * 2)
11+
```
12+
* New Feature: Add Mock module, support DotWeb.SetMock to use it
13+
* Detail:
14+
- Provide StandardMock to simplify use, it implement Mock interface
15+
- also you can create custom implement
16+
- you can register MockHandle or register return string
17+
- register key only support route
18+
- special: mock mode only effective in DevelopMode
19+
- Example:
20+
``` golang
21+
func AppMock() dotweb.Mock{
22+
m := dotweb.NewStandardMock()
23+
m.RegisterString("/", "mock data")
24+
return m
25+
}
26+
app.SetMock(AppMock())
27+
```
28+
* 2018-08-22 10:00
29+
330
#### Version 1.5.6.1
431
* BugFixed: hystrix add doCleanHistoryCounter, used to clean history counter
532
* 2018-08-18 10:00

0 commit comments

Comments
 (0)