From bf7c262d78d7487134232bff09548257f87270f9 Mon Sep 17 00:00:00 2001 From: viletyy Date: Fri, 9 Jul 2021 14:26:02 +0800 Subject: [PATCH] fix: change server init --- config.yaml.example | 3 ++- config/server.go | 5 +++-- initialize/grpc_server.go | 12 +++++++++--- initialize/http_server.go | 9 +++++---- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/config.yaml.example b/config.yaml.example index 3071086..79cf06f 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -12,7 +12,8 @@ app: upload_image_max_size: 5 # MB upload_image_allow_exts: ['.jpg','.jpeg','.png'] server: - port: 8000 + http_port: 8001 + grpc_port: 8002 read_timeout: 60 write_timeout: 60 tracer_host_port: '127.0.0.1:6831' diff --git a/config/server.go b/config/server.go index e9b1110..c97ee8d 100644 --- a/config/server.go +++ b/config/server.go @@ -1,13 +1,14 @@ /* * @Date: 2021-03-22 09:54:07 * @LastEditors: viletyy - * @LastEditTime: 2021-06-20 19:45:31 + * @LastEditTime: 2021-07-09 14:22:47 * @FilePath: /potato/config/server.go */ package config type Server struct { - Port int64 `mapstructure:"port" json:"port" yaml:"port"` + HttpPort int64 `mapstructure:"http_port" json:"http_port" yaml:"http_port"` + GrpcPort int64 `mapstructure:"grpc_port" json:"grpc_port" yaml:"grpc_port"` ReadTimeout int64 `mapstructure:"read_timeout" json:"read_timeout" yaml:"read_timeout"` WriteTimeout int64 `mapstructure:"write_timeout" json:"write_timeout" yaml:"write_timeout"` TracerHostPort string `mapstructure:"tracer_host_port" json:"tracer_host_port" yaml:"tracer_host_port"` diff --git a/initialize/grpc_server.go b/initialize/grpc_server.go index a936199..0d23739 100644 --- a/initialize/grpc_server.go +++ b/initialize/grpc_server.go @@ -1,22 +1,28 @@ /* * @Date: 2021-06-17 00:19:32 * @LastEditors: viletyy - * @LastEditTime: 2021-06-20 19:46:17 + * @LastEditTime: 2021-07-09 14:24:14 * @FilePath: /potato/initialize/grpc_server.go */ package initialize import ( + "net" + 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 { +func RunGrpcServer(port string) error { server := grpc.NewServer() pb.RegisterVendorServiceServer(server, basic.NewVendorServer()) reflection.Register(server) - return server + lis, err := net.Listen("tcp", ":"+port) + if err != nil { + return err + } + return server.Serve(lis) } diff --git a/initialize/http_server.go b/initialize/http_server.go index d5f3271..dd0dbd9 100644 --- a/initialize/http_server.go +++ b/initialize/http_server.go @@ -1,12 +1,13 @@ /* * @Date: 2021-03-22 17:03:27 * @LastEditors: viletyy - * @LastEditTime: 2021-06-20 22:22:10 + * @LastEditTime: 2021-07-09 14:24:36 * @FilePath: /potato/initialize/http_server.go */ package initialize import ( + "fmt" "net/http" "time" @@ -14,15 +15,15 @@ import ( "github.com/viletyy/potato/internal/routers" ) -func RunHttpServer(port string) *http.Server { +func RunHttpServer(port string) error { router := routers.InitRouter() server := &http.Server{ - Addr: port, + Addr: fmt.Sprintf(":%s", 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, } - return server + return server.ListenAndServe() }