将工厂改成黑盒模式,降低用户使用成本
This commit is contained in:
@@ -121,7 +121,7 @@ corsHandler := middleware.CORS(corsConfig)(handler)
|
||||
|
||||
- 从请求头 `X-Timezone` 读取时区
|
||||
- 如果未传递时区信息,使用默认时区 `AsiaShanghai`
|
||||
- 时区信息存储到context中,可通过 `http.GetTimezone()` 获取
|
||||
- 时区信息存储到context中,可通过Handler的`GetTimezone()`方法获取
|
||||
- 自动验证时区有效性,无效时区会回退到默认时区
|
||||
|
||||
### 使用方法
|
||||
@@ -132,19 +132,21 @@ corsHandler := middleware.CORS(corsConfig)(handler)
|
||||
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"
|
||||
"git.toowon.com/jimmy/go-common/datetime"
|
||||
)
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
// 从context获取时区
|
||||
timezone := http.GetTimezone(r)
|
||||
h := commonhttp.NewHandler(w, r)
|
||||
|
||||
// 从Handler获取时区
|
||||
timezone := h.GetTimezone()
|
||||
|
||||
// 使用时区
|
||||
now := datetime.Now(timezone)
|
||||
datetime.FormatDateTime(now, timezone)
|
||||
|
||||
http.Success(w, map[string]interface{}{
|
||||
h.Success(map[string]interface{}{
|
||||
"timezone": timezone,
|
||||
"time": datetime.FormatDateTime(now),
|
||||
})
|
||||
@@ -183,13 +185,15 @@ func main() {
|
||||
```go
|
||||
import (
|
||||
"net/http"
|
||||
"git.toowon.com/jimmy/go-common/http"
|
||||
commonhttp "git.toowon.com/jimmy/go-common/http"
|
||||
"git.toowon.com/jimmy/go-common/datetime"
|
||||
)
|
||||
|
||||
func GetUserList(w http.ResponseWriter, r *http.Request) {
|
||||
// 从请求context获取时区
|
||||
timezone := http.GetTimezone(r)
|
||||
h := commonhttp.NewHandler(w, r)
|
||||
|
||||
// 从Handler获取时区
|
||||
timezone := h.GetTimezone()
|
||||
|
||||
// 使用时区进行时间处理
|
||||
now := datetime.Now(timezone)
|
||||
@@ -199,7 +203,7 @@ func GetUserList(w http.ResponseWriter, r *http.Request) {
|
||||
endTime := datetime.EndOfDay(now, timezone)
|
||||
|
||||
// 返回数据
|
||||
http.Success(w, map[string]interface{}{
|
||||
h.Success(map[string]interface{}{
|
||||
"timezone": timezone,
|
||||
"startTime": datetime.FormatDateTime(startTime),
|
||||
"endTime": datetime.FormatDateTime(endTime),
|
||||
@@ -283,16 +287,18 @@ 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"
|
||||
"git.toowon.com/jimmy/go-common/datetime"
|
||||
)
|
||||
|
||||
func apiHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// 获取时区
|
||||
timezone := http.GetTimezone(r)
|
||||
h := commonhttp.NewHandler(w, r)
|
||||
|
||||
// 从Handler获取时区
|
||||
timezone := h.GetTimezone()
|
||||
now := datetime.Now(timezone)
|
||||
|
||||
http.Success(w, map[string]interface{}{
|
||||
h.Success(map[string]interface{}{
|
||||
"message": "Hello",
|
||||
"timezone": timezone,
|
||||
"time": datetime.FormatDateTime(now),
|
||||
@@ -332,7 +338,7 @@ 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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -352,15 +358,17 @@ func main() {
|
||||
}
|
||||
|
||||
func getUsers(w http.ResponseWriter, r *http.Request) {
|
||||
timezone := http.GetTimezone(r)
|
||||
h := commonhttp.NewHandler(w, r)
|
||||
timezone := h.GetTimezone()
|
||||
// 处理逻辑
|
||||
http.Success(w, nil)
|
||||
h.Success(nil)
|
||||
}
|
||||
|
||||
func getPosts(w http.ResponseWriter, r *http.Request) {
|
||||
timezone := http.GetTimezone(r)
|
||||
h := commonhttp.NewHandler(w, r)
|
||||
timezone := h.GetTimezone()
|
||||
// 处理逻辑
|
||||
http.Success(w, nil)
|
||||
h.Success(nil)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user