-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
117 lines (98 loc) · 2.39 KB
/
options.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
/*
* 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/99designs/gqlgen/graphql"
"github.com/gin-gonic/gin"
"github.com/hopeio/utils/net/http/grpc/web"
"github.com/rs/cors"
"google.golang.org/grpc"
"net/http"
"time"
)
type IOption interface {
apply(server *Server)
}
type Option func(server *Server)
func WithContext(ctx context.Context) Option {
return func(server *Server) {
server.BaseContext = ctx
}
}
func WithHttpAddr(addr string) Option {
return func(server *Server) {
server.Http.Addr = addr
}
}
func WithHttp3Addr(addr string) Option {
return func(server *Server) {
server.Http3.Addr = addr
}
}
func WithName(name string) Option {
return func(server *Server) {
server.Name = name
}
}
func WithStopTimeout(stopTimeout time.Duration) Option {
return func(server *Server) {
server.StopTimeout = stopTimeout
}
}
func WithGrpcHandler(grpcHandler func(*grpc.Server)) Option {
return func(server *Server) {
server.GrpcHandler = grpcHandler
}
}
func WithGinHandler(ginHandler func(*gin.Engine)) Option {
return func(server *Server) {
server.GinHandler = ginHandler
}
}
func WithGraphqlHandler(graphqlHandler graphql.ExecutableSchema) Option {
return func(server *Server) {
server.GraphqlHandler = graphqlHandler
}
}
func WithGrpcWeb(options ...web.Option) Option {
return func(server *Server) {
server.EnableGrpcWeb = true
server.GrpcWebOptions = append(server.GrpcWebOptions, options...)
}
}
func WithOnStart(onStart func(context.Context)) Option {
return func(server *Server) {
server.OnStart = onStart
}
}
func WithOnStop(onStop func(context.Context)) Option {
return func(server *Server) {
server.OnStop = onStop
}
}
func WithGrpcOptions(options ...grpc.ServerOption) Option {
return func(server *Server) {
server.GrpcOptions = append(server.GrpcOptions, options...)
}
}
func WithCors(cors *cors.Options) Option {
return func(server *Server) {
server.EnableCors = true
server.Cors = cors
}
}
func WithMiddlewares(middlewares ...http.HandlerFunc) Option {
return func(server *Server) {
server.Middlewares = append(server.Middlewares, middlewares...)
}
}
func WithTelemetry(telemetry TelemetryConfig) Option {
return func(server *Server) {
server.EnableTelemetry = true
server.TelemetryConfig = telemetry
}
}