-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
169 lines (154 loc) · 4.18 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright 2024 hopeio. All rights reserved.
* Licensed under the MIT License that can be found in the LICENSE file.
* @Created by jyb
*/
package cherry
import (
"context"
"github.com/gin-gonic/gin"
"github.com/hopeio/utils/crypto/tls"
"github.com/hopeio/utils/log"
gini "github.com/hopeio/utils/net/http/gin"
"github.com/hopeio/utils/net/http/grpc/gateway/grpc-gateway"
"github.com/hopeio/utils/net/http/grpc/web"
"github.com/hopeio/utils/validation/validator"
"github.com/quic-go/quic-go/http3"
"github.com/rs/cors"
"go.opentelemetry.io/otel/propagation"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"golang.org/x/net/http2"
"google.golang.org/grpc"
"net/http"
"time"
)
func NewServer(options ...Option) *Server {
c := &Server{}
c.Http.Addr = ":8080"
c.Http.ReadTimeout = 5 * time.Second
c.Http.WriteTimeout = 5 * time.Second
c.StopTimeout = 5 * time.Second
gin.SetMode(gin.ReleaseMode)
gin.DisableBindValidation()
validator.DefaultValidator = nil // 自己做校验
c.EnableCors = true
c.EnableTelemetry = true
c.MetricsInterval = time.Minute
c.EnableDebugApi = true
for _, option := range options {
option(c)
}
return c
}
type Http struct {
http.Server
CertFile string
KeyFile string
}
type Http3 struct {
http3.Server
CertFile string
KeyFile string
}
type HttpOption struct {
AccessLog AccessLog
ExcludeLogPrefixes []string
IncludeLogPrefixes []string
StaticFs []StaticFsConfig
Middlewares []http.HandlerFunc
}
type StaticFsConfig struct {
Prefix string
Root string
}
type Server struct {
Name string
Http Http
Http2 http2.Server
Http3 *Http3
StopTimeout time.Duration
Gin gini.Config
EnableCors bool
Cors *cors.Options
Middlewares []http.HandlerFunc
HttpOption HttpOption
// Grpc options
GrpcOptions []grpc.ServerOption
EnableGrpcWeb bool
GrpcWebOptions []web.Option
EnableTelemetry, EnableDebugApi, EnableApiDoc bool
ApiDocUriPrefix, ApiDocDir string
TelemetryConfig
BaseContext context.Context
// 注册 grpc 服务
GrpcHandler func(*grpc.Server)
// 注册 grpc-gateway 服务
GatewayHandler grpc_gateway.GatewayHandler
// 注册 gin 服务
GinHandler func(*gin.Engine)
// 各种钩子函数
OnStart func(context.Context)
OnStop func(context.Context)
}
type TelemetryConfig struct {
EnablePrometheus bool
MetricsInterval time.Duration
propagator propagation.TextMapPropagator
tracerProvider *sdktrace.TracerProvider
meterProvider *sdkmetric.MeterProvider
}
func (c *TelemetryConfig) SetTextMapPropagator(propagator propagation.TextMapPropagator) {
c.propagator = propagator
}
func (c *TelemetryConfig) SetTracerProvider(tracerProvider *sdktrace.TracerProvider) {
c.tracerProvider = tracerProvider
}
func (c *TelemetryConfig) SetMeterProvider(meterProvider *sdkmetric.MeterProvider) {
c.meterProvider = meterProvider
}
func (s *Server) Init() {
if s.Http.Addr == "" {
s.Http.Addr = ":8080"
}
if s.Http3 != nil && s.Http3.Addr == "" {
s.Http3.Addr = ":8080"
}
log.DurationNotify("ReadTimeout", s.Http.ReadTimeout, time.Second)
log.DurationNotify("WriteTimeout", s.Http.WriteTimeout, time.Second)
if s.StopTimeout == 0 {
s.StopTimeout = 5 * time.Second
}
log.DurationNotify("StopTimeout", s.StopTimeout, time.Second)
if s.Http.CertFile != "" && s.Http.KeyFile != "" {
tlsConfig, err := tls.NewServerTLSConfig(s.Http.CertFile, s.Http.KeyFile)
if err != nil {
log.Fatal(err)
}
s.Http.TLSConfig = tlsConfig
}
if s.Http3 != nil && s.Http3.CertFile != "" && s.Http3.KeyFile != "" {
tlsConfig, err := tls.NewServerTLSConfig(s.Http3.CertFile, s.Http3.KeyFile)
if err != nil {
log.Fatal(err)
}
s.Http3.TLSConfig = tlsConfig
}
if s.BaseContext == nil {
s.BaseContext = context.Background()
}
s.HttpOption.AccessLog = defaultAccessLog
}
// implement initialize
func (s *Server) BeforeInject() {
*s = *NewServer()
}
func (s *Server) AfterInject() {
s.Init()
}
func (s *Server) WithOptions(options ...Option) *Server {
for _, option := range options {
option(s)
}
return s
}