-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
55 lines (52 loc) · 1.45 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package kratosx
import (
"github.com/HC74/kratosx/config"
"github.com/HC74/kratosx/server"
"github.com/go-kratos/kratos/v2/encoding/json"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
"google.golang.org/protobuf/encoding/protojson"
)
func grpcServer(c *config.GrpcService, md []middleware.Middleware) *grpc.Server {
var opts = []grpc.ServerOption{
grpc.Middleware(md...),
}
if c.Network != "" {
opts = append(opts, grpc.Network(c.Network))
}
if c.Addr != "" {
opts = append(opts, grpc.Address(c.Addr))
}
if c.Timeout != 0 {
opts = append(opts, grpc.Timeout(c.Timeout))
}
return grpc.NewServer(opts...)
}
func httpServer(c *config.HttpService, md []middleware.Middleware) *http.Server {
var opts = []http.ServerOption{
http.Middleware(md...),
}
if c.Network != "" {
opts = append(opts, http.Network(c.Network))
}
if c.Addr != "" {
opts = append(opts, http.Address(c.Addr))
}
if c.Timeout != 0 {
opts = append(opts, http.Timeout(c.Timeout))
}
if c.FormatResponse {
opts = append(opts, server.HttpEncoder()...)
}
if c.Cors != nil {
opts = append(opts, server.Cors(c.Cors))
}
if c.Marshal != nil {
json.MarshalOptions = protojson.MarshalOptions{
EmitUnpopulated: c.Marshal.EmitUnpopulated, //默认值不忽略
UseProtoNames: c.Marshal.UseProtoNames, //使用proto name返回http字段
}
}
return http.NewServer(opts...)
}