-
Notifications
You must be signed in to change notification settings - Fork 5
/
options.go
70 lines (59 loc) · 1.89 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
package jrpc
import (
"net/http"
)
// Option func type
type Option func(s *Server)
// Auth sets basic auth credentials, required
func Auth(user, password string) Option {
return func(s *Server) {
s.authUser = user
s.authPasswd = password
}
}
// WithTimeouts sets server timeout values such as ReadHeader, Write and Idle timeout, optional.
// If this option not defined server use default timeout values
func WithTimeouts(timeouts Timeouts) Option {
// this option sets only server limits values and exclude middlewares limits fields of Limits struct.
return func(s *Server) {
s.timeouts.ReadHeaderTimeout = timeouts.ReadHeaderTimeout
s.timeouts.WriteTimeout = timeouts.WriteTimeout
s.timeouts.IdleTimeout = timeouts.IdleTimeout
s.timeouts.CallTimeout = timeouts.CallTimeout // it need for middleware with custom timeout value
}
}
// WithLimits sets value for client limit call/sec per client middleware
func WithLimits(limit float64) Option {
// this option sets only server limits values and exclude middlewares limits fields of Limits struct.
return func(s *Server) {
s.limits.clientLimit = limit
}
}
// WithThrottler sets throttler middleware with specify limit value, optional
func WithThrottler(limit int) Option {
return func(s *Server) {
s.limits.serverThrottle = limit
}
}
// WithMiddlewares sets custom middlewares list, optional
func WithMiddlewares(middlewares ...func(http.Handler) http.Handler) Option {
return func(s *Server) {
s.customMiddlewares = append(s.customMiddlewares, middlewares...)
}
}
// WithSignature sets signature data for server response headers
func WithSignature(appName, author, version string) Option {
return func(s *Server) {
s.signature = signaturePayload{
appName: appName,
author: author,
version: version,
}
}
}
// WithLogger sets custom logger, optional
func WithLogger(logger L) Option {
return func(s *Server) {
s.logger = logger
}
}