fix: change server init

This commit is contained in:
viletyy 2021-07-09 14:26:02 +08:00 committed by 来自村里的小螃蟹
parent 85ccd47bb6
commit bf7c262d78
4 changed files with 19 additions and 10 deletions

View File

@ -12,7 +12,8 @@ app:
upload_image_max_size: 5 # MB upload_image_max_size: 5 # MB
upload_image_allow_exts: ['.jpg','.jpeg','.png'] upload_image_allow_exts: ['.jpg','.jpeg','.png']
server: server:
port: 8000 http_port: 8001
grpc_port: 8002
read_timeout: 60 read_timeout: 60
write_timeout: 60 write_timeout: 60
tracer_host_port: '127.0.0.1:6831' tracer_host_port: '127.0.0.1:6831'

View File

@ -1,13 +1,14 @@
/* /*
* @Date: 2021-03-22 09:54:07 * @Date: 2021-03-22 09:54:07
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-20 19:45:31 * @LastEditTime: 2021-07-09 14:22:47
* @FilePath: /potato/config/server.go * @FilePath: /potato/config/server.go
*/ */
package config package config
type Server struct { 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"` ReadTimeout int64 `mapstructure:"read_timeout" json:"read_timeout" yaml:"read_timeout"`
WriteTimeout int64 `mapstructure:"write_timeout" json:"write_timeout" yaml:"write_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"` TracerHostPort string `mapstructure:"tracer_host_port" json:"tracer_host_port" yaml:"tracer_host_port"`

View File

@ -1,22 +1,28 @@
/* /*
* @Date: 2021-06-17 00:19:32 * @Date: 2021-06-17 00:19:32
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-20 19:46:17 * @LastEditTime: 2021-07-09 14:24:14
* @FilePath: /potato/initialize/grpc_server.go * @FilePath: /potato/initialize/grpc_server.go
*/ */
package initialize package initialize
import ( import (
"net"
pb "github.com/viletyy/potato/proto/basic" pb "github.com/viletyy/potato/proto/basic"
"github.com/viletyy/potato/server/basic" "github.com/viletyy/potato/server/basic"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/reflection" "google.golang.org/grpc/reflection"
) )
func RunGrpcServer() *grpc.Server { func RunGrpcServer(port string) error {
server := grpc.NewServer() server := grpc.NewServer()
pb.RegisterVendorServiceServer(server, basic.NewVendorServer()) pb.RegisterVendorServiceServer(server, basic.NewVendorServer())
reflection.Register(server) reflection.Register(server)
return server lis, err := net.Listen("tcp", ":"+port)
if err != nil {
return err
}
return server.Serve(lis)
} }

View File

@ -1,12 +1,13 @@
/* /*
* @Date: 2021-03-22 17:03:27 * @Date: 2021-03-22 17:03:27
* @LastEditors: viletyy * @LastEditors: viletyy
* @LastEditTime: 2021-06-20 22:22:10 * @LastEditTime: 2021-07-09 14:24:36
* @FilePath: /potato/initialize/http_server.go * @FilePath: /potato/initialize/http_server.go
*/ */
package initialize package initialize
import ( import (
"fmt"
"net/http" "net/http"
"time" "time"
@ -14,15 +15,15 @@ import (
"github.com/viletyy/potato/internal/routers" "github.com/viletyy/potato/internal/routers"
) )
func RunHttpServer(port string) *http.Server { func RunHttpServer(port string) error {
router := routers.InitRouter() router := routers.InitRouter()
server := &http.Server{ server := &http.Server{
Addr: port, Addr: fmt.Sprintf(":%s", 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,
} }
return server return server.ListenAndServe()
} }