调整项目结构,factory只负责暴露方法,不实现业务细节

This commit is contained in:
2025-12-07 00:04:01 +08:00
parent b66f345281
commit 339920a940
23 changed files with 2165 additions and 1231 deletions

View File

@@ -136,7 +136,7 @@ import (
"net/http"
"git.toowon.com/jimmy/go-common/middleware"
commonhttp "git.toowon.com/jimmy/go-common/http"
"git.toowon.com/jimmy/go-common/datetime"
"git.toowon.com/jimmy/go-common/tools"
)
func handler(w http.ResponseWriter, r *http.Request) {
@@ -146,12 +146,12 @@ func handler(w http.ResponseWriter, r *http.Request) {
timezone := h.GetTimezone()
// 使用时区
now := datetime.Now(timezone)
datetime.FormatDateTime(now, timezone)
now := tools.Now(timezone)
tools.FormatDateTime(now, timezone)
h.Success(map[string]interface{}{
"timezone": timezone,
"time": datetime.FormatDateTime(now),
"time": tools.FormatDateTime(now),
})
}
@@ -168,7 +168,7 @@ func main() {
import (
"net/http"
"git.toowon.com/jimmy/go-common/middleware"
"git.toowon.com/jimmy/go-common/datetime"
"git.toowon.com/jimmy/go-common/tools"
)
func handler(w http.ResponseWriter, r *http.Request) {
@@ -177,7 +177,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
func main() {
// 使用自定义默认时区
handler := middleware.TimezoneWithDefault(datetime.UTC)(http.HandlerFunc(handler))
handler := middleware.TimezoneWithDefault(tools.UTC)(http.HandlerFunc(handler))
http.Handle("/api", handler)
http.ListenAndServe(":8080", nil)
}
@@ -185,11 +185,43 @@ func main() {
#### 在业务代码中使用时区
**推荐方式:通过 factory 使用(黑盒模式)**
```go
import (
"net/http"
"git.toowon.com/jimmy/go-common/factory"
)
func GetUserList(w http.ResponseWriter, r *http.Request) {
fac, _ := factory.NewFactoryFromFile("config.json")
// 从请求中获取时区
timezone := fac.GetTimezone(r)
// 使用时区进行时间处理
now := fac.Now(timezone)
// 查询数据时使用时区
startTime := fac.StartOfDay(now, timezone)
endTime := fac.EndOfDay(now, timezone)
// 返回数据
fac.Success(w, map[string]interface{}{
"timezone": timezone,
"startTime": fac.FormatDateTime(startTime),
"endTime": fac.FormatDateTime(endTime),
})
}
```
**或者直接使用 tools 包:**
```go
import (
"net/http"
commonhttp "git.toowon.com/jimmy/go-common/http"
"git.toowon.com/jimmy/go-common/datetime"
"git.toowon.com/jimmy/go-common/tools"
)
func GetUserList(w http.ResponseWriter, r *http.Request) {
@@ -199,17 +231,17 @@ func GetUserList(w http.ResponseWriter, r *http.Request) {
timezone := h.GetTimezone()
// 使用时区进行时间处理
now := datetime.Now(timezone)
now := tools.Now(timezone)
// 查询数据时使用时区
startTime := datetime.StartOfDay(now, timezone)
endTime := datetime.EndOfDay(now, timezone)
startTime := tools.StartOfDay(now, timezone)
endTime := tools.EndOfDay(now, timezone)
// 返回数据
h.Success(map[string]interface{}{
"timezone": timezone,
"startTime": datetime.FormatDateTime(startTime),
"endTime": datetime.FormatDateTime(endTime),
"startTime": tools.FormatDateTime(startTime),
"endTime": tools.FormatDateTime(endTime),
})
}
```
@@ -658,7 +690,7 @@ import (
"git.toowon.com/jimmy/go-common/middleware"
"git.toowon.com/jimmy/go-common/logger"
commonhttp "git.toowon.com/jimmy/go-common/http"
"git.toowon.com/jimmy/go-common/datetime"
"git.toowon.com/jimmy/go-common/tools"
)
func apiHandler(w http.ResponseWriter, r *http.Request) {
@@ -666,12 +698,12 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
// 从Handler获取时区
timezone := h.GetTimezone()
now := datetime.Now(timezone)
now := tools.Now(timezone)
h.Success(map[string]interface{}{
"message": "Hello",
"timezone": timezone,
"time": datetime.FormatDateTime(now),
"time": tools.FormatDateTime(now),
})
}
@@ -756,7 +788,7 @@ import (
"git.toowon.com/jimmy/go-common/middleware"
commonhttp "git.toowon.com/jimmy/go-common/http"
"git.toowon.com/jimmy/go-common/datetime"
"git.toowon.com/jimmy/go-common/tools"
)
func apiHandler(w http.ResponseWriter, r *http.Request) {
@@ -764,12 +796,12 @@ func apiHandler(w http.ResponseWriter, r *http.Request) {
// 从Handler获取时区
timezone := h.GetTimezone()
now := datetime.Now(timezone)
now := tools.Now(timezone)
h.Success(map[string]interface{}{
"message": "Hello",
"timezone": timezone,
"time": datetime.FormatDateTime(now),
"time": tools.FormatDateTime(now),
})
}