diff --git a/go.mod b/go.mod index 1cababf..11c2c62 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/onsi/gomega v1.13.0 // indirect github.com/opentracing/opentracing-go v1.2.0 github.com/smacker/opentracing-gorm v0.0.0-20181207094635-cd4974441042 // indirect + github.com/soheilhy/cmux v0.1.5 github.com/spf13/viper v1.7.1 github.com/swaggo/gin-swagger v1.3.0 github.com/swaggo/swag v1.7.0 @@ -29,6 +30,7 @@ require ( github.com/viletyy/yolk v1.0.1 go.uber.org/atomic v1.8.0 // indirect go.uber.org/zap v1.17.0 + golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect google.golang.org/grpc v1.38.0 google.golang.org/protobuf v1.26.0 gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect diff --git a/initialize/grpc.go b/initialize/grpc.go deleted file mode 100644 index cb8f314..0000000 --- a/initialize/grpc.go +++ /dev/null @@ -1,36 +0,0 @@ -/* - * @Date: 2021-06-17 00:19:32 - * @LastEditors: viletyy - * @LastEditTime: 2021-06-19 22:41:24 - * @FilePath: /potato/initialize/grpc.go - */ -package initialize - -import ( - "net" - - "github.com/viletyy/potato/global" - pb "github.com/viletyy/potato/proto/basic" - "github.com/viletyy/potato/server/basic" - "github.com/viletyy/yolk/convert" - "google.golang.org/grpc" - "google.golang.org/grpc/reflection" -) - -func RunGrpc() { - server := grpc.NewServer() - pb.RegisterVendorServiceServer(server, basic.NewVendorServer()) - reflection.Register(server) - - go func() { - listen, err := net.Listen("tcp", ":"+convert.ToString(global.GO_CONFIG.Server.RpcPort)) - if err != nil { - global.GO_LOG.Sugar().Fatalf("net.Listen err: %v", err) - } - - err = server.Serve(listen) - if err != nil { - global.GO_LOG.Sugar().Fatalf("server.Serve err: %v", err) - } - }() -} diff --git a/initialize/grpc_server.go b/initialize/grpc_server.go new file mode 100644 index 0000000..173c2fe --- /dev/null +++ b/initialize/grpc_server.go @@ -0,0 +1,33 @@ +/* + * @Date: 2021-06-17 00:19:32 + * @LastEditors: viletyy + * @LastEditTime: 2021-06-20 19:06:15 + * @FilePath: /potato/initialize/grpc_server.go + */ +package initialize + +import ( + pb "github.com/viletyy/potato/proto/basic" + "github.com/viletyy/potato/server/basic" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" +) + +func RunGrpcServer() *grpc.Server { + server := grpc.NewServer() + pb.RegisterVendorServiceServer(server, basic.NewVendorServer()) + reflection.Register(server) + + // go func() { + // listen, err := net.Listen("tcp", ":"+convert.ToString(global.GO_CONFIG.Server.RpcPort)) + // if err != nil { + // global.GO_LOG.Sugar().Fatalf("net.Listen err: %v", err) + // } + + // err = server.Serve(listen) + // if err != nil { + // global.GO_LOG.Sugar().Fatalf("server.Serve err: %v", err) + // } + // }() + return server +} diff --git a/initialize/server.go b/initialize/server.go index 2d1986c..a2178d6 100644 --- a/initialize/server.go +++ b/initialize/server.go @@ -1,50 +1,47 @@ /* * @Date: 2021-03-22 17:03:27 * @LastEditors: viletyy - * @LastEditTime: 2021-06-10 22:24:39 + * @LastEditTime: 2021-06-20 19:18:10 * @FilePath: /potato/initialize/server.go */ package initialize import ( - "context" - "fmt" "net/http" - "os" - "os/signal" "time" "github.com/viletyy/potato/global" "github.com/viletyy/potato/internal/routers" ) -func RunServer() { +func RunServer(port string) *http.Server { router := routers.InitRouter() server := &http.Server{ - Addr: fmt.Sprintf(":%d", global.GO_CONFIG.Server.HttpPort), + Addr: port, Handler: router, ReadTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second, WriteTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second, MaxHeaderBytes: 1 << 20, } - go func() { - if err := server.ListenAndServe(); err != nil { - global.GO_LOG.Info(fmt.Sprintf("Listen: %s\n", err)) - } - }() + // go func() { + // if err := server.ListenAndServe(); err != nil { + // global.GO_LOG.Info(fmt.Sprintf("Listen: %s\n", err)) + // } + // }() - quit := make(chan os.Signal) - signal.Notify(quit, os.Interrupt) - <-quit + // quit := make(chan os.Signal) + // signal.Notify(quit, os.Interrupt) + // <-quit - global.GO_LOG.Info("Shutdown Server") + // global.GO_LOG.Info("Shutdown Server") - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - if err := server.Shutdown(ctx); err != nil { - global.GO_LOG.Fatal(fmt.Sprintf("Server Shutdown: %v", err)) - } + // ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + // defer cancel() + // if err := server.Shutdown(ctx); err != nil { + // global.GO_LOG.Fatal(fmt.Sprintf("Server Shutdown: %v", err)) + // } - global.GO_LOG.Info("Server exiting") + // global.GO_LOG.Info("Server exiting") + return server } diff --git a/main.go b/main.go index f464e5a..28e4663 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,19 @@ /* * @Date: 2021-03-21 19:54:57 * @LastEditors: viletyy - * @LastEditTime: 2021-06-17 00:29:41 + * @LastEditTime: 2021-06-20 19:24:16 * @FilePath: /potato/main.go */ package main import ( + "flag" + "net" + + "github.com/soheilhy/cmux" "github.com/viletyy/potato/global" "github.com/viletyy/potato/initialize" + "github.com/viletyy/yolk/convert" ) // @title Potato Api @@ -16,6 +21,12 @@ import ( // @description This is a potato use golang // @BasePath /api +var port string + +func runTcpServer(port string) (net.Listener, error) { + return net.Listen("tcp", ":"+port) +} + func main() { global.GO_VP = initialize.Viper() global.GO_LOG = initialize.Zap() @@ -26,7 +37,30 @@ func main() { defer global.GO_DB.Close() defer global.GO_REDIS.Close() - initialize.RunGrpc() - initialize.RunServer() + flag.StringVar(&port, "port", convert.ToString(global.GO_CONFIG.Server.HttpPort), "启动端口号") + flag.Parse() + + l, err := runTcpServer(port) + if err != nil { + global.GO_LOG.Sugar().Fatalf("Run Tcp Server err: %v", err) + } + m := cmux.New(l) + grpcL := m.MatchWithWriters( + cmux.HTTP2MatchHeaderFieldSendSettings( + "content-type", + "application/grpc", + ), + ) + httpL := m.Match(cmux.HTTP1Fast()) + + grpcS := initialize.RunGrpcServer() + httpS := initialize.RunServer(port) + go grpcS.Serve(grpcL) + go httpS.Serve(httpL) + + err = m.Serve() + if err != nil { + global.GO_LOG.Sugar().Fatalf("Run Server err: %v", err) + } }