From 5c2574da7c0e91cb84824a0af2c9fed8d76fd39b Mon Sep 17 00:00:00 2001 From: jflyfox Date: Wed, 6 Jan 2021 00:39:50 +0800 Subject: [PATCH] improve package ghttp --- net/ghttp/ghttp_server.go | 17 ++++++++--------- net/ghttp/ghttp_server_config.go | 4 ++-- net/ghttp/ghttp_server_config_static.go | 8 ++++---- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/net/ghttp/ghttp_server.go b/net/ghttp/ghttp_server.go index b1ea3228e..1b17e0c4d 100644 --- a/net/ghttp/ghttp_server.go +++ b/net/ghttp/ghttp_server.go @@ -8,9 +8,8 @@ package ghttp import ( "bytes" - "errors" - "fmt" "github.com/gogf/gf/debug/gdebug" + "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "net/http" "os" @@ -125,13 +124,13 @@ func (s *Server) Start() error { // Server can only be run once. if s.Status() == ServerStatusRunning { - return errors.New("[ghttp] server is already running") + return gerror.New("server is already running") } // Logging path setting check. if s.config.LogPath != "" { if err := s.config.Logger.SetPath(s.config.LogPath); err != nil { - return errors.New(fmt.Sprintf("[ghttp] set log path '%s' error: %v", s.config.LogPath, err)) + return gerror.Wrapf(err, `set logging path "%s" failed`, s.config.LogPath) } } // Default session storage. @@ -141,7 +140,7 @@ func (s *Server) Start() error { path = gfile.Join(s.config.SessionPath, s.name) if !gfile.Exists(path) { if err := gfile.Mkdir(path); err != nil { - return errors.New(fmt.Sprintf("[ghttp] mkdir failed for '%s': %v", path, err)) + return gerror.Wrapf(err, `mkdir failed for "%s"`, path) } } } @@ -175,7 +174,7 @@ func (s *Server) Start() error { // If there's no route registered and no static service enabled, // it then returns an error of invalid usage of server. if len(s.routesMap) == 0 && !s.config.FileServerEnabled { - return errors.New(`[ghttp] there's no route set or static feature enabled, did you forget import the router?`) + return gerror.New(`there's no route set or static feature enabled, did you forget import the router?`) } // Start the HTTP server. @@ -196,7 +195,7 @@ func (s *Server) Start() error { if gproc.IsChild() { gtimer.SetTimeout(2*time.Second, func() { if err := gproc.Send(gproc.PPid(), []byte("exit"), adminGProcCommGroup); err != nil { - //glog.Error("[ghttp] server error in process communication:", err) + //glog.Error("server error in process communication:", err) } }) } @@ -320,7 +319,7 @@ func (s *Server) Run() { p.Remove() } } - s.Logger().Printf("[ghttp] %d: all servers shutdown", gproc.Pid()) + s.Logger().Printf("%d: all servers shutdown", gproc.Pid()) } // Wait blocks to wait for all servers done. @@ -338,7 +337,7 @@ func Wait() { } return true }) - glog.Printf("[ghttp] %d: all servers shutdown", gproc.Pid()) + glog.Printf("%d: all servers shutdown", gproc.Pid()) } // startServer starts the underlying server listening. diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index 3cc4a1b95..edd63d4ea 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -393,7 +393,7 @@ func (s *Server) EnableHTTPS(certFile, keyFile string, tlsConfig ...*tls.Config) certFileRealPath = certFile } if certFileRealPath == "" { - s.Logger().Fatal(fmt.Sprintf(`[ghttp] EnableHTTPS failed: certFile "%s" does not exist`, certFile)) + s.Logger().Fatal(fmt.Sprintf(`EnableHTTPS failed: certFile "%s" does not exist`, certFile)) } keyFileRealPath := gfile.RealPath(keyFile) if keyFileRealPath == "" { @@ -407,7 +407,7 @@ func (s *Server) EnableHTTPS(certFile, keyFile string, tlsConfig ...*tls.Config) keyFileRealPath = keyFile } if keyFileRealPath == "" { - s.Logger().Fatal(fmt.Sprintf(`[ghttp] EnableHTTPS failed: keyFile "%s" does not exist`, keyFile)) + s.Logger().Fatal(fmt.Sprintf(`EnableHTTPS failed: keyFile "%s" does not exist`, keyFile)) } s.config.HTTPSCertPath = certFileRealPath s.config.HTTPSKeyPath = keyFileRealPath diff --git a/net/ghttp/ghttp_server_config_static.go b/net/ghttp/ghttp_server_config_static.go index 8056acf3a..f4c5164e0 100644 --- a/net/ghttp/ghttp_server_config_static.go +++ b/net/ghttp/ghttp_server_config_static.go @@ -53,12 +53,12 @@ func (s *Server) SetServerRoot(root string) { realPath := root if !gres.Contains(realPath) { if p, err := gfile.Search(root); err != nil { - s.Logger().Fatal(fmt.Sprintf(`[ghttp] SetServerRoot failed: %v`, err)) + s.Logger().Fatal(fmt.Sprintf(`SetServerRoot failed: %v`, err)) } else { realPath = p } } - s.Logger().Debug("[ghttp] SetServerRoot path:", realPath) + s.Logger().Debug("SetServerRoot path:", realPath) s.config.SearchPaths = []string{strings.TrimRight(realPath, gfile.Separator)} s.config.FileServerEnabled = true } @@ -68,7 +68,7 @@ func (s *Server) AddSearchPath(path string) { realPath := path if !gres.Contains(realPath) { if p, err := gfile.Search(path); err != nil { - s.Logger().Fatal(fmt.Sprintf(`[ghttp] AddSearchPath failed: %v`, err)) + s.Logger().Fatal(fmt.Sprintf(`AddSearchPath failed: %v`, err)) } else { realPath = p } @@ -82,7 +82,7 @@ func (s *Server) AddStaticPath(prefix string, path string) { realPath := path if !gres.Contains(realPath) { if p, err := gfile.Search(path); err != nil { - s.Logger().Fatal(fmt.Sprintf(`[ghttp] AddStaticPath failed: %v`, err)) + s.Logger().Fatal(fmt.Sprintf(`AddStaticPath failed: %v`, err)) } else { realPath = p }