From e58d7e8dda476e3b0460f8c9559c2b607c997f25 Mon Sep 17 00:00:00 2001 From: John Date: Fri, 28 Feb 2020 23:00:05 +0800 Subject: [PATCH] add internal log for gi18n; improve unit testing case for ghttp.Server --- .../ghttp/server/middleware/auth_exception.go | 2 +- .../net/ghttp/server/middleware/middleware.go | 12 +++--- .example/other/test.go | 43 +++++++++++++------ i18n/gi18n/gi18n_manager.go | 20 +++++++-- net/ghttp/ghttp_server_domain.go | 4 ++ net/ghttp/ghttp_unit_log_test.go | 7 ++- 6 files changed, 63 insertions(+), 25 deletions(-) diff --git a/.example/net/ghttp/server/middleware/auth_exception.go b/.example/net/ghttp/server/middleware/auth_exception.go index 106e241ba..f36bbe337 100644 --- a/.example/net/ghttp/server/middleware/auth_exception.go +++ b/.example/net/ghttp/server/middleware/auth_exception.go @@ -19,7 +19,7 @@ func MiddlewareAuth(r *ghttp.Request) { func main() { s := g.Server() s.Group("/admin", func(group *ghttp.RouterGroup) { - g.MiddlewarePattern("/*action", func(r *ghttp.Request) { + group.Middleware(func(r *ghttp.Request) { if action := r.GetRouterString("action"); action != "" { switch action { case "login": diff --git a/.example/net/ghttp/server/middleware/middleware.go b/.example/net/ghttp/server/middleware/middleware.go index 9f36efaa4..9bad81b1c 100644 --- a/.example/net/ghttp/server/middleware/middleware.go +++ b/.example/net/ghttp/server/middleware/middleware.go @@ -13,20 +13,20 @@ func main() { r.Middleware.Next() r.Response.Write("end") }) - g.Group("/order", func(group *ghttp.RouterGroup) { - g.GET("/list", func(r *ghttp.Request) { + group.Group("/order", func(group *ghttp.RouterGroup) { + group.GET("/list", func(r *ghttp.Request) { r.Response.Write("list") }) }) - g.Group("/user", func(group *ghttp.RouterGroup) { - g.GET("/info", func(r *ghttp.Request) { + group.Group("/user", func(group *ghttp.RouterGroup) { + group.GET("/info", func(r *ghttp.Request) { r.Response.Write("info") }) - g.POST("/edit", func(r *ghttp.Request) { + group.POST("/edit", func(r *ghttp.Request) { r.Response.Write("edit") }) }) - g.Group("/hook", func(group *ghttp.RouterGroup) { + group.Group("/hook", func(group *ghttp.RouterGroup) { group.Hook("/*", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) { r.Response.Write("hook any") }) diff --git a/.example/other/test.go b/.example/other/test.go index 6d92a4fd6..774c42627 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -1,18 +1,37 @@ package main import ( - "fmt" - "github.com/gogf/gf/os/glog" + "net/http" + + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/net/ghttp" ) -func main() { - fmt.Println(glog.LEVEL_ALL) - fmt.Println(glog.LEVEL_DEV) - fmt.Println(glog.LEVEL_PROD) - fmt.Println(glog.LEVEL_DEBU) - fmt.Println(glog.LEVEL_INFO) - fmt.Println(glog.LEVEL_NOTI) - fmt.Println(glog.LEVEL_WARN) - fmt.Println(glog.LEVEL_ERRO) - fmt.Println(glog.LEVEL_CRIT) +func MiddlewareAuth(r *ghttp.Request) { + token := r.Get("token") + if token == "123456" { + r.Response.Writeln("auth") + r.Middleware.Next() + } else { + r.Response.WriteStatus(http.StatusForbidden) + } +} + +func MiddlewareCORS(r *ghttp.Request) { + r.Response.Writeln("cors") + r.Response.CORSDefault() + r.Middleware.Next() +} + +func main() { + s := g.Server() + s.Use(MiddlewareCORS) + s.Group("/api.v2", func(group *ghttp.RouterGroup) { + group.Middleware(MiddlewareAuth) + group.ALL("/user/list", func(r *ghttp.Request) { + r.Response.Writeln("list") + }) + }) + s.SetPort(8199) + s.Run() } diff --git a/i18n/gi18n/gi18n_manager.go b/i18n/gi18n/gi18n_manager.go index d19aaaf76..277e874e4 100644 --- a/i18n/gi18n/gi18n_manager.go +++ b/i18n/gi18n/gi18n_manager.go @@ -9,6 +9,7 @@ package gi18n import ( "errors" "fmt" + "github.com/gogf/gf/internal/intlog" "strings" "sync" @@ -34,6 +35,7 @@ type Manager struct { options Options // configuration options. } +// Options is used for i18n object configuration. type Options struct { Path string // I18n files storage path. Language string // Local language. @@ -41,6 +43,7 @@ type Options struct { } var ( + // defaultDelimiters defines the key variable delimiters. defaultDelimiters = []string{"{#", "}"} ) @@ -55,7 +58,7 @@ func New(options ...Options) *Manager { if len(opts.Delimiters) == 0 { opts.Delimiters = defaultDelimiters } - return &Manager{ + m := &Manager{ options: opts, pattern: fmt.Sprintf( `%s(\w+)%s`, @@ -63,6 +66,8 @@ func New(options ...Options) *Manager { gregex.Quote(opts.Delimiters[1]), ), } + intlog.Printf(`New: %+v`, m) + return m } // DefaultOptions returns the default options for i18n manager. @@ -93,17 +98,20 @@ func (m *Manager) SetPath(path string) error { } m.options.Path = realPath } + intlog.Printf(`SetPath: %s`, m.options.Path) return nil } // SetLanguage sets the language for translator. func (m *Manager) SetLanguage(language string) { m.options.Language = language + intlog.Printf(`SetLanguage: %s`, m.options.Language) } // SetDelimiters sets the delimiters for translator. func (m *Manager) SetDelimiters(left, right string) { m.pattern = fmt.Sprintf(`%s(\w+)%s`, gregex.Quote(left), gregex.Quote(right)) + intlog.Printf(`SetDelimiters: %v`, m.pattern) } // T is alias of Translate. @@ -117,12 +125,13 @@ func (m *Manager) Translate(content string, language ...string) string { m.init() m.mu.RLock() defer m.mu.RUnlock() - var data map[string]string + transLang := m.options.Language if len(language) > 0 && language[0] != "" { - data = m.data[language[0]] + transLang = language[0] } else { - data = m.data[m.options.Language] + transLang = m.options.Language } + data := m.data[transLang] if data == nil { return content } @@ -137,9 +146,12 @@ func (m *Manager) Translate(content string, language ...string) string { } return match[0] }) + intlog.Printf(`Translate for language: %s`, transLang) return result } +// init initializes the manager for lazy initialization design. +// The i18n manager is only initialized once. func (m *Manager) init() { m.mu.RLock() if m.data != nil { diff --git a/net/ghttp/ghttp_server_domain.go b/net/ghttp/ghttp_server_domain.go index 1aebd801c..a7a8c8564 100644 --- a/net/ghttp/ghttp_server_domain.go +++ b/net/ghttp/ghttp_server_domain.go @@ -147,3 +147,7 @@ func (d *Domain) BindMiddlewareDefault(handlers ...HandlerFunc) { d.s.BindMiddleware(gDEFAULT_MIDDLEWARE_PATTERN+"@"+domain, handlers...) } } + +func (d *Domain) Use(handlers ...HandlerFunc) { + d.BindMiddlewareDefault(handlers...) +} diff --git a/net/ghttp/ghttp_unit_log_test.go b/net/ghttp/ghttp_unit_log_test.go index 40d2916ef..e540fa8aa 100644 --- a/net/ghttp/ghttp_unit_log_test.go +++ b/net/ghttp/ghttp_unit_log_test.go @@ -52,8 +52,11 @@ func Test_Log(t *testing.T) { gtest.Assert(gstr.Contains(gfile.GetContents(logPath1), "HANDLER"), true) logPath2 := gfile.Join(logDir, "access-"+gtime.Now().Format("Ymd")+".log") + fmt.Println(gfile.GetContents(logPath2)) gtest.Assert(gstr.Contains(gfile.GetContents(logPath2), " /hello "), true) - gtest.Assert(gstr.Contains(gfile.GetContents(logPath2), "[ERRO]"), true) - gtest.Assert(gstr.Contains(gfile.GetContents(logPath2), "custom error"), true) + + logPath3 := gfile.Join(logDir, "error-"+gtime.Now().Format("Ymd")+".log") + gtest.Assert(gstr.Contains(gfile.GetContents(logPath3), "[ERRO]"), true) + gtest.Assert(gstr.Contains(gfile.GetContents(logPath3), "custom error"), true) }) }