/
option.go
147 lines (126 loc) · 3.35 KB
/
option.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
package taskqueueworker
import (
"crypto/tls"
"encoding/base64"
"net/http"
"strings"
"time"
"github.com/golangid/candi/codebase/interfaces"
)
type (
option struct {
queue QueueStorage
persistent Persistent
secondaryPersistent Persistent
maxClientSubscriber int
autoRemoveClientInterval time.Duration
dashboardBanner string
dashboardPort uint16
dashboardAuthKey string
debugMode bool
locker interfaces.Locker
tlsConfig *tls.Config
}
// OptionFunc type
OptionFunc func(*option)
)
// SetQueue option func
func SetQueue(q QueueStorage) OptionFunc {
return func(o *option) {
o.queue = q
}
}
// SetPersistent option func
func SetPersistent(p Persistent) OptionFunc {
return func(o *option) {
o.persistent = p
}
}
// SetSecondaryPersistent option func
func SetSecondaryPersistent(p Persistent) OptionFunc {
return func(o *option) {
o.secondaryPersistent = p
}
}
// SetMaxClientSubscriber option func
func SetMaxClientSubscriber(max int) OptionFunc {
return func(o *option) {
o.maxClientSubscriber = max
}
}
// SetAutoRemoveClientInterval option func
func SetAutoRemoveClientInterval(d time.Duration) OptionFunc {
return func(o *option) {
o.autoRemoveClientInterval = d
}
}
// SetDashboardBanner option func
func SetDashboardBanner(banner string) OptionFunc {
return func(o *option) {
o.dashboardBanner = banner
}
}
// SetDashboardHTTPPort option func
func SetDashboardHTTPPort(port uint16) OptionFunc {
return func(o *option) {
o.dashboardPort = port
}
}
// SetDebugMode option func
func SetDebugMode(debugMode bool) OptionFunc {
return func(o *option) {
o.debugMode = debugMode
}
}
// SetLocker option func
func SetLocker(locker interfaces.Locker) OptionFunc {
return func(o *option) {
o.locker = locker
}
}
// SetExternalWorkerHost option func, setting worker host for add job, if not empty default using http request when add job
func SetExternalWorkerHost(host string) OptionFunc {
externalWorkerHost = host
return func(o *option) {
externalWorkerHost = host
}
}
// SetDashboardBasicAuth option func
func SetDashboardBasicAuth(username, password string) OptionFunc {
return func(o *option) {
o.dashboardAuthKey = base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
}
}
func (o *option) basicAuth(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "/_next") {
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/task")
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/job")
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/expired")
}
if o.dashboardAuthKey == "" {
next.ServeHTTP(w, r)
return
}
w.Header().Set("WWW-Authenticate", `Basic realm=""`)
auth := r.Header.Get("Authorization")
const prefix = "Basic "
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Invalid authorization"))
return
}
if auth[len(prefix):] != o.dashboardAuthKey {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Invalid authorization"))
return
}
next.ServeHTTP(w, r)
}
}
// SetTLSConfig option func
func SetTLSConfig(tlsConfig *tls.Config) OptionFunc {
return func(o *option) {
o.tlsConfig = tlsConfig
}
}