-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_options.go
144 lines (117 loc) · 3.52 KB
/
server_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
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
package rony
import (
"time"
"github.com/clubpay/ronykit/kit"
"github.com/clubpay/ronykit/kit/desc"
"github.com/clubpay/ronykit/std/gateways/fasthttp"
)
type serverConfig struct {
services map[string]*desc.Service
edgeOpts []kit.Option
gatewayOpts []fasthttp.Option
}
func defaultServerConfig() serverConfig {
return serverConfig{
gatewayOpts: []fasthttp.Option{
fasthttp.WithPredicateKey("cmd"),
},
}
}
func (cfg *serverConfig) getService(name string) *desc.Service {
if cfg.services == nil {
cfg.services = make(map[string]*desc.Service)
}
svc, ok := cfg.services[name]
if !ok {
svc = desc.NewService(name)
cfg.services[name] = svc
}
return svc
}
func (cfg *serverConfig) allServices() []kit.ServiceDescriptor {
svcs := make([]kit.ServiceDescriptor, 0, len(cfg.services))
for _, svc := range cfg.services {
svcs = append(svcs, svc)
}
return svcs
}
func (cfg serverConfig) Gateway() kit.Gateway {
return fasthttp.MustNew(cfg.gatewayOpts...)
}
type ServerOption func(cfg *serverConfig)
type (
CORSConfig = fasthttp.CORSConfig
)
func WithCORS(cors CORSConfig) ServerOption {
return func(cfg *serverConfig) {
cfg.gatewayOpts = append(cfg.gatewayOpts, fasthttp.WithCORS(cors))
}
}
func Listen(addr string) ServerOption {
return func(cfg *serverConfig) {
cfg.gatewayOpts = append(cfg.gatewayOpts, fasthttp.Listen(addr))
}
}
func WithServerName(name string) ServerOption {
return func(cfg *serverConfig) {
cfg.gatewayOpts = append(cfg.gatewayOpts, fasthttp.WithServerName(name))
}
}
type CompressionLevel = fasthttp.CompressionLevel
// Represents compression level that will be used in the middleware
const (
CompressionLevelDisabled CompressionLevel = -1
CompressionLevelDefault CompressionLevel = 0
CompressionLevelBestSpeed CompressionLevel = 1
CompressionLevelBestCompression CompressionLevel = 2
)
func WithCompression(lvl CompressionLevel) ServerOption {
return func(cfg *serverConfig) {
cfg.gatewayOpts = append(cfg.gatewayOpts, fasthttp.WithCompressionLevel(lvl))
}
}
func WithPredicateKey(key string) ServerOption {
return func(cfg *serverConfig) {
cfg.gatewayOpts = append(cfg.gatewayOpts, fasthttp.WithPredicateKey(key))
}
}
func WithWebsocketEndpoint(endpoint string) ServerOption {
return func(cfg *serverConfig) {
cfg.gatewayOpts = append(cfg.gatewayOpts, fasthttp.WithWebsocketEndpoint(endpoint))
}
}
func WithCustomRPC(in kit.IncomingRPCFactory, out kit.OutgoingRPCFactory) ServerOption {
return func(cfg *serverConfig) {
cfg.gatewayOpts = append(cfg.gatewayOpts, fasthttp.WithCustomRPC(in, out))
}
}
func WithTracer(tracer kit.Tracer) ServerOption {
return func(cfg *serverConfig) {
cfg.edgeOpts = append(cfg.edgeOpts, kit.WithTrace(tracer))
}
}
func WithLogger(logger kit.Logger) ServerOption {
return func(cfg *serverConfig) {
cfg.edgeOpts = append(cfg.edgeOpts, kit.WithLogger(logger))
}
}
func WithPrefork() ServerOption {
return func(cfg *serverConfig) {
cfg.edgeOpts = append(cfg.edgeOpts, kit.WithPrefork())
}
}
func WithShutdownTimeout(timeout time.Duration) ServerOption {
return func(cfg *serverConfig) {
cfg.edgeOpts = append(cfg.edgeOpts, kit.WithShutdownTimeout(timeout))
}
}
func WithGlobalHandlers(handlers ...kit.HandlerFunc) ServerOption {
return func(cfg *serverConfig) {
cfg.edgeOpts = append(cfg.edgeOpts, kit.WithGlobalHandlers(handlers...))
}
}
func WithDisableHeaderNamesNormalizing() ServerOption {
return func(cfg *serverConfig) {
cfg.gatewayOpts = append(cfg.gatewayOpts, fasthttp.WithDisableHeaderNamesNormalizing())
}
}