将工厂改成黑盒模式,降低用户使用成本
This commit is contained in:
@@ -49,22 +49,29 @@ now := datetime.Now()
|
||||
str := datetime.FormatDateTime(now)
|
||||
```
|
||||
|
||||
#### HTTP响应
|
||||
#### HTTP响应(Handler黑盒模式)
|
||||
|
||||
```go
|
||||
import "git.toowon.com/jimmy/go-common/http"
|
||||
import (
|
||||
"net/http"
|
||||
commonhttp "git.toowon.com/jimmy/go-common/http"
|
||||
)
|
||||
|
||||
http.Success(w, data)
|
||||
http.SuccessPage(w, list, total, page, pageSize)
|
||||
http.Error(w, 1001, "业务错误")
|
||||
func GetUser(h *commonhttp.Handler) {
|
||||
id := h.GetQueryInt64("id", 0)
|
||||
h.Success(data)
|
||||
}
|
||||
|
||||
http.HandleFunc("/user", commonhttp.HandleFunc(GetUser))
|
||||
```
|
||||
|
||||
#### 中间件
|
||||
|
||||
```go
|
||||
import (
|
||||
"net/http"
|
||||
"git.toowon.com/jimmy/go-common/middleware"
|
||||
"git.toowon.com/jimmy/go-common/http"
|
||||
commonhttp "git.toowon.com/jimmy/go-common/http"
|
||||
)
|
||||
|
||||
// CORS + 时区中间件
|
||||
@@ -72,10 +79,13 @@ chain := middleware.NewChain(
|
||||
middleware.CORS(),
|
||||
middleware.Timezone,
|
||||
)
|
||||
handler := chain.ThenFunc(yourHandler)
|
||||
|
||||
// 在处理器中获取时区
|
||||
timezone := http.GetTimezone(r)
|
||||
handler := chain.ThenFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
h := commonhttp.NewHandler(w, r)
|
||||
// 在Handler中获取时区
|
||||
timezone := h.GetTimezone()
|
||||
h.Success(data)
|
||||
})
|
||||
```
|
||||
|
||||
#### 配置管理
|
||||
|
||||
Reference in New Issue
Block a user