add: more proto insist

This commit is contained in:
viletyy 2021-06-20 19:29:27 +08:00
parent dd3e27e3d6
commit 4df74ff80a
5 changed files with 91 additions and 61 deletions

2
go.mod
View File

@ -21,6 +21,7 @@ require (
github.com/onsi/gomega v1.13.0 // indirect github.com/onsi/gomega v1.13.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 github.com/opentracing/opentracing-go v1.2.0
github.com/smacker/opentracing-gorm v0.0.0-20181207094635-cd4974441042 // indirect 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/spf13/viper v1.7.1
github.com/swaggo/gin-swagger v1.3.0 github.com/swaggo/gin-swagger v1.3.0
github.com/swaggo/swag v1.7.0 github.com/swaggo/swag v1.7.0
@ -29,6 +30,7 @@ require (
github.com/viletyy/yolk v1.0.1 github.com/viletyy/yolk v1.0.1
go.uber.org/atomic v1.8.0 // indirect go.uber.org/atomic v1.8.0 // indirect
go.uber.org/zap v1.17.0 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/grpc v1.38.0
google.golang.org/protobuf v1.26.0 google.golang.org/protobuf v1.26.0
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect

View File

@ -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)
}
}()
}

33
initialize/grpc_server.go Normal file
View File

@ -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
}

View File

@ -1,50 +1,47 @@
/* /*
* @Date: 2021-03-22 17:03:27 * @Date: 2021-03-22 17:03:27
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-10 22:24:39 * @LastEditTime: 2021-06-20 19:18:10
* @FilePath: /potato/initialize/server.go * @FilePath: /potato/initialize/server.go
*/ */
package initialize package initialize
import ( import (
"context"
"fmt"
"net/http" "net/http"
"os"
"os/signal"
"time" "time"
"github.com/viletyy/potato/global" "github.com/viletyy/potato/global"
"github.com/viletyy/potato/internal/routers" "github.com/viletyy/potato/internal/routers"
) )
func RunServer() { func RunServer(port string) *http.Server {
router := routers.InitRouter() router := routers.InitRouter()
server := &http.Server{ server := &http.Server{
Addr: fmt.Sprintf(":%d", global.GO_CONFIG.Server.HttpPort), Addr: port,
Handler: router, Handler: router,
ReadTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second, ReadTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second, WriteTimeout: time.Duration(global.GO_CONFIG.Server.ReadTimeout) * time.Second,
MaxHeaderBytes: 1 << 20, MaxHeaderBytes: 1 << 20,
} }
go func() { // go func() {
if err := server.ListenAndServe(); err != nil { // if err := server.ListenAndServe(); err != nil {
global.GO_LOG.Info(fmt.Sprintf("Listen: %s\n", err)) // global.GO_LOG.Info(fmt.Sprintf("Listen: %s\n", err))
} // }
}() // }()
quit := make(chan os.Signal) // quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt) // signal.Notify(quit, os.Interrupt)
<-quit // <-quit
global.GO_LOG.Info("Shutdown Server") // global.GO_LOG.Info("Shutdown Server")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) // ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() // defer cancel()
if err := server.Shutdown(ctx); err != nil { // if err := server.Shutdown(ctx); err != nil {
global.GO_LOG.Fatal(fmt.Sprintf("Server Shutdown: %v", err)) // global.GO_LOG.Fatal(fmt.Sprintf("Server Shutdown: %v", err))
} // }
global.GO_LOG.Info("Server exiting") // global.GO_LOG.Info("Server exiting")
return server
} }

40
main.go
View File

@ -1,14 +1,19 @@
/* /*
* @Date: 2021-03-21 19:54:57 * @Date: 2021-03-21 19:54:57
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-17 00:29:41 * @LastEditTime: 2021-06-20 19:24:16
* @FilePath: /potato/main.go * @FilePath: /potato/main.go
*/ */
package main package main
import ( import (
"flag"
"net"
"github.com/soheilhy/cmux"
"github.com/viletyy/potato/global" "github.com/viletyy/potato/global"
"github.com/viletyy/potato/initialize" "github.com/viletyy/potato/initialize"
"github.com/viletyy/yolk/convert"
) )
// @title Potato Api // @title Potato Api
@ -16,6 +21,12 @@ import (
// @description This is a potato use golang // @description This is a potato use golang
// @BasePath /api // @BasePath /api
var port string
func runTcpServer(port string) (net.Listener, error) {
return net.Listen("tcp", ":"+port)
}
func main() { func main() {
global.GO_VP = initialize.Viper() global.GO_VP = initialize.Viper()
global.GO_LOG = initialize.Zap() global.GO_LOG = initialize.Zap()
@ -26,7 +37,30 @@ func main() {
defer global.GO_DB.Close() defer global.GO_DB.Close()
defer global.GO_REDIS.Close() defer global.GO_REDIS.Close()
initialize.RunGrpc() flag.StringVar(&port, "port", convert.ToString(global.GO_CONFIG.Server.HttpPort), "启动端口号")
initialize.RunServer() 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)
}
} }