mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
add internal log for gi18n; improve unit testing case for ghttp.Server
This commit is contained in:
@ -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":
|
||||
|
||||
@ -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")
|
||||
})
|
||||
|
||||
@ -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()
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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...)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user