mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-29 12:31:26 +08:00
【更新】更新restful
This commit is contained in:
48
pkg/transport/grpc_server.go
Normal file
48
pkg/transport/grpc_server.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"google.golang.org/grpc"
|
||||
"net"
|
||||
"pandax/pkg/global"
|
||||
)
|
||||
|
||||
type GrpcServer struct {
|
||||
Addr string
|
||||
srv *grpc.Server
|
||||
}
|
||||
|
||||
func NewServer(addr string) *GrpcServer {
|
||||
return &GrpcServer{
|
||||
Addr: addr,
|
||||
srv: grpc.NewServer(),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *GrpcServer) GetServe() *grpc.Server {
|
||||
return s.srv
|
||||
}
|
||||
|
||||
func (s *GrpcServer) Type() Type {
|
||||
return TypeGRPC
|
||||
}
|
||||
|
||||
func (s *GrpcServer) Start(ctx context.Context) error {
|
||||
l, err := net.Listen("tcp", s.Addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error listen addr: %w", err)
|
||||
}
|
||||
global.Log.Debugf("GRPC Server listen: %s", s.Addr)
|
||||
go func() {
|
||||
if err := s.srv.Serve(l); err != nil {
|
||||
global.Log.Errorf("error http serve: %s", err)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *GrpcServer) Stop(ctx context.Context) error {
|
||||
s.srv.Stop()
|
||||
return nil
|
||||
}
|
||||
59
pkg/transport/http_server.go
Normal file
59
pkg/transport/http_server.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/XM-GO/PandaKit/logger"
|
||||
"github.com/emicklei/go-restful/v3"
|
||||
"net/http"
|
||||
"pandax/pkg/global"
|
||||
)
|
||||
|
||||
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 {
|
||||
global.Log.Debugf("HTTP Server listen: %s", s.Addr)
|
||||
go func() {
|
||||
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) {
|
||||
logger.Log.Debug(v...)
|
||||
}
|
||||
|
||||
func (t *httpLog) Printf(format string, v ...any) {
|
||||
logger.Log.Debugf(format, v...)
|
||||
}
|
||||
19
pkg/transport/transport.go
Normal file
19
pkg/transport/transport.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Type string
|
||||
|
||||
const (
|
||||
TypeHTTP Type = "HTTP"
|
||||
TypeGRPC Type = "GRPC"
|
||||
)
|
||||
|
||||
// Server is transport server.
|
||||
type Server interface {
|
||||
Type() Type
|
||||
Start(context.Context) error
|
||||
Stop(context.Context) error
|
||||
}
|
||||
Reference in New Issue
Block a user