mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve package ghttp
This commit is contained in:
@ -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.
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user