Files
PandaX/pkg/transport/http_server.go
PandaX-Go 315c0a6c27 [优化]
2024-08-29 21:58:27 +08:00

67 lines
1.3 KiB
Go

package transport
import (
"context"
"net/http"
"pandax/pkg/global"
"github.com/emicklei/go-restful/v3"
)
type HttpServer struct {
Addr string
srv *http.Server
Container *restful.Container
}
func NewHttpServer(addr string) *HttpServer {
c := restful.NewContainer()
c.EnableContentEncoding(true)
restful.TraceLogger(&httpLog{})
restful.SetLogger(&httpLog{})
return &HttpServer{
Addr: addr,
Container: c,
srv: &http.Server{
Addr: addr,
Handler: c,
},
}
}
func (s *HttpServer) Type() Type {
return TypeHTTP
}
func (s *HttpServer) Start(ctx context.Context) error {
go func() {
if global.Conf.Server.Tls.Enable {
global.Log.Infof("HTTPS Server listen: %s", s.Addr)
if err := s.srv.ListenAndServeTLS(global.Conf.Server.Tls.CertFile, global.Conf.Server.Tls.KeyFile); err != nil {
global.Log.Errorf("error http serve: %s", err)
}
} else {
global.Log.Infof("HTTP Server listen: %s", s.Addr)
if err := s.srv.ListenAndServe(); err != nil {
global.Log.Errorf("error http serve: %s", err)
}
}
}()
return nil
}
func (s *HttpServer) Stop(ctx context.Context) error {
return s.srv.Shutdown(ctx)
}
type httpLog struct{}
func (t *httpLog) Print(v ...any) {
global.Log.Debug(v...)
}
func (t *httpLog) Printf(format string, v ...any) {
global.Log.Debugf(format, v...)
}