Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit fa7ac4f

Browse files
authored
Merge pull request #9 from randlabs/more_options
Updated deps, added new features and improved health handler.
2 parents 395c1cd + 3b8cde3 commit fa7ac4f

File tree

5 files changed

+116
-47
lines changed

5 files changed

+116
-47
lines changed

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module github.com/randlabs/go-metrics
22

3-
go 1.17
3+
go 1.19
44

55
require (
6-
github.com/prometheus/client_golang v1.13.0
7-
github.com/prometheus/client_model v0.2.0
8-
github.com/randlabs/go-webserver v1.1.6
6+
github.com/prometheus/client_golang v1.14.0
7+
github.com/prometheus/client_model v0.3.0
8+
github.com/randlabs/go-webserver v1.2.0
99
github.com/randlabs/rundown-protection v1.0.5
1010
google.golang.org/protobuf v1.28.1
1111
)
@@ -14,15 +14,15 @@ require (
1414
github.com/andybalholm/brotli v1.0.4 // indirect
1515
github.com/beorn7/perks v1.0.1 // indirect
1616
github.com/cespare/xxhash/v2 v2.1.2 // indirect
17-
github.com/fasthttp/router v1.4.11 // indirect
17+
github.com/fasthttp/router v1.4.13 // indirect
1818
github.com/golang/protobuf v1.5.2 // indirect
19-
github.com/klauspost/compress v1.15.9 // indirect
19+
github.com/klauspost/compress v1.15.12 // indirect
2020
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
2121
github.com/prometheus/common v0.37.0 // indirect
2222
github.com/prometheus/procfs v0.8.0 // indirect
2323
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
2424
github.com/valyala/bytebufferpool v1.0.0 // indirect
25-
github.com/valyala/fasthttp v1.39.0 // indirect
25+
github.com/valyala/fasthttp v1.41.0 // indirect
2626
github.com/valyala/tcplisten v1.0.0 // indirect
27-
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2 // indirect
27+
golang.org/x/sys v0.2.0 // indirect
2828
)

go.sum

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
5454
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
5555
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
5656
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
57+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5758
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5859
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
5960
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
6061
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
6162
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
62-
github.com/fasthttp/router v1.4.11 h1:99BvgVxeS2oOZBHnKr/okpdPq1jkn8WvYA2trh/71LY=
63-
github.com/fasthttp/router v1.4.11/go.mod h1:luEEYkGBSAmYyPaMeIUGNgqY+FdHHYDOK9Kivaw7aNo=
63+
github.com/fasthttp/router v1.4.13 h1:42M7+7tNO6clb5seb4HhXlBIX1lnNv8DLhiT6jUv75A=
64+
github.com/fasthttp/router v1.4.13/go.mod h1:mVhHMaSQA2Hi1HeuL/ZMuZpsZWk5bya75EpaDr3fO7E=
6465
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
6566
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
6667
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -142,9 +143,9 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
142143
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
143144
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
144145
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
145-
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
146-
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
147146
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
147+
github.com/klauspost/compress v1.15.12 h1:YClS/PImqYbn+UILDnqxQCZ3RehC9N318SU3kElDUEM=
148+
github.com/klauspost/compress v1.15.12/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
148149
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
149150
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
150151
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
@@ -169,13 +170,14 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
169170
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
170171
github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
171172
github.com/prometheus/client_golang v1.12.1/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY=
172-
github.com/prometheus/client_golang v1.13.0 h1:b71QUfeo5M8gq2+evJdTPfZhYMAU0uKPkyPJ7TPsloU=
173-
github.com/prometheus/client_golang v1.13.0/go.mod h1:vTeo+zgvILHsnnj/39Ou/1fPN5nJFOEMgftOUOmlvYQ=
173+
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
174+
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
174175
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
175176
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
176177
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
177-
github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M=
178178
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
179+
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
180+
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
179181
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
180182
github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo=
181183
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
@@ -189,8 +191,8 @@ github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
189191
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
190192
github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo=
191193
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
192-
github.com/randlabs/go-webserver v1.1.6 h1:cTh6ihNgvV14wQAY3xX6JMsmHEMR9ZHkKG+BODnVnpE=
193-
github.com/randlabs/go-webserver v1.1.6/go.mod h1:EJnQ2yTCDGEG0aKu+Xitp2BiXJRFeBavk1Lfgyb3wow=
194+
github.com/randlabs/go-webserver v1.2.0 h1:pX1DQku+I2vk/3ib/TSjFW/g+Ng1noZKcH1Rrb5JNM4=
195+
github.com/randlabs/go-webserver v1.2.0/go.mod h1:Fd29ict+bpeUIJ7q/nw20haKn5CNEe2eJktpkwBJSjA=
194196
github.com/randlabs/rundown-protection v1.0.5 h1:Bq9GMTuWvZI3915VkVLVysayQfSfHmWwuhD6feawhfU=
195197
github.com/randlabs/rundown-protection v1.0.5/go.mod h1:rccYmsidTaGDOvZSAF9eYsLzMVvh9tj043JyTicfQ5o=
196198
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
@@ -206,9 +208,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
206208
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
207209
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
208210
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
209-
github.com/valyala/fasthttp v1.38.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
210-
github.com/valyala/fasthttp v1.39.0 h1:lW8mGeM7yydOqZKmwyMTaz/PH/A+CLgtmmcjv+OORfU=
211-
github.com/valyala/fasthttp v1.39.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
211+
github.com/valyala/fasthttp v1.41.0 h1:zeR0Z1my1wDHTRiamBCXVglQdbUwgb9uWG3k1HQz6jY=
212+
github.com/valyala/fasthttp v1.41.0/go.mod h1:f6VbjjoI3z1NDOZOv17o6RvtRSWxC77seBFc2uWtgiY=
212213
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
213214
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
214215
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -288,6 +289,7 @@ golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qx
288289
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
289290
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
290291
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
292+
golang.org/x/net v0.0.0-20220906165146-f3363e06e74c/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
291293
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
292294
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
293295
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -343,10 +345,10 @@ golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBc
343345
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
344346
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
345347
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
346-
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
347348
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
348-
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2 h1:fqTvyMIIj+HRzMmnzr9NtpHP6uVpvB5fkHcgPDC4nu8=
349-
golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
349+
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
350+
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
351+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
350352
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
351353
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
352354
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

handlers.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
11
package metrics
22

33
import (
4+
"strconv"
5+
46
"github.com/prometheus/client_golang/prometheus/promhttp"
57
webserver "github.com/randlabs/go-webserver"
68
"github.com/randlabs/go-webserver/request"
79
)
810

911
// -----------------------------------------------------------------------------
1012

13+
const (
14+
strContentTypeTextPlain = "text/plain"
15+
strContentTypeApplicationJSON = "application/json"
16+
)
17+
1118
func (mws *Controller) getHealthHandler() webserver.HandlerFunc {
1219
return func(req *request.RequestContext) error {
1320
// Get current health status from callback
1421
status := mws.healthCallback()
1522

1623
// Send output
17-
if len(status) > 0 {
18-
req.WriteString(status)
24+
if isJSON(status) {
25+
req.SetResponseHeader("Content-Type", strContentTypeApplicationJSON)
26+
} else {
27+
req.SetResponseHeader("Content-Type", strContentTypeTextPlain)
28+
}
29+
30+
if !req.IsHead() {
31+
_, _ = req.WriteString(status)
32+
} else {
33+
req.SetResponseHeader("Content-Length", strconv.FormatUint(uint64(int64(len(status))), 10))
1934
}
35+
2036
req.Success()
2137

2238
// Done
@@ -32,3 +48,31 @@ func (mws *Controller) getMetricsHandler() webserver.HandlerFunc {
3248
},
3349
))
3450
}
51+
52+
// -----------------------------------------------------------------------------
53+
54+
func isJSON(s string) bool {
55+
// An official (?) method but a plain text is also considered a valid JSON
56+
// var js interface{}
57+
// return json.Unmarshal([]byte(s), &js) == nil
58+
59+
// Our quick approach
60+
startIdx := 0
61+
endIdx := len(s)
62+
63+
// Skip blanks at the beginning and the end
64+
for startIdx < endIdx && isBlank(s[startIdx]) {
65+
startIdx += 1
66+
}
67+
for endIdx > startIdx && isBlank(s[endIdx-1]) {
68+
endIdx -= 1
69+
}
70+
71+
return startIdx < endIdx &&
72+
((s[startIdx] == '{' && s[endIdx-1] == '}') ||
73+
(s[startIdx] == '[' && s[endIdx-1] == ']'))
74+
}
75+
76+
func isBlank(ch byte) bool {
77+
return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'
78+
}

metrics.go

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/tls"
77
"errors"
88
"fmt"
9+
"time"
910

1011
"github.com/prometheus/client_golang/prometheus"
1112
webserver "github.com/randlabs/go-webserver"
@@ -29,6 +30,9 @@ type Options struct {
2930
// If Server is provided, use this server instead of creating a new one.
3031
Server *webserver.Server
3132

33+
// Server name to use when sending response headers. Defaults to 'metrics-server'.
34+
Name string
35+
3236
// Address is the bind address to attach the internal web server.
3337
Address string
3438

@@ -38,6 +42,9 @@ type Options struct {
3842
// TLSConfig optionally provides a TLS configuration for use.
3943
TLSConfig *tls.Config
4044

45+
// A callback to call if an error is encountered.
46+
ListenErrorHandler webserver.ListenErrorHandler
47+
4148
// AccessToken is an optional access token required to access the status endpoints.
4249
AccessToken string
4350

@@ -65,31 +72,47 @@ type HealthCallback func() string
6572

6673
// -----------------------------------------------------------------------------
6774

75+
const (
76+
defaultServerName = "metrics-server"
77+
)
78+
79+
// -----------------------------------------------------------------------------
80+
6881
// CreateController initializes and creates a new controller
69-
func CreateController(opts Options) (*Controller, error) {
82+
func CreateController(options Options) (*Controller, error) {
7083
var err error
7184

72-
if opts.HealthCallback == nil {
85+
if options.HealthCallback == nil {
7386
return nil, errors.New("invalid health callback")
7487
}
7588

7689
// Create metrics object
7790
mws := Controller{
7891
rundownProt: rp.Create(),
79-
healthCallback: opts.HealthCallback,
92+
healthCallback: options.HealthCallback,
8093
}
8194

8295
// Create webserver
83-
if opts.Server != nil {
84-
mws.server = opts.Server
96+
if options.Server != nil {
97+
mws.server = options.Server
8598
} else {
99+
serverName := options.Name
100+
if len(serverName) == 0 {
101+
serverName = defaultServerName
102+
}
103+
86104
mws.usingInternalServer = true
87105
mws.server, err = webserver.Create(webserver.Options{
88-
Name: "metrics-server",
89-
Address: opts.Address,
90-
Port: opts.Port,
91-
EnableCompression: false,
92-
TLSConfig: opts.TLSConfig,
106+
Name: serverName,
107+
Address: options.Address,
108+
Port: options.Port,
109+
ReadTimeout: 10 * time.Second, // 10 seconds for reading and writing a metrics request
110+
WriteTimeout: 10 * time.Second, // should be enough.
111+
MaxRequestBodySize: 512, // Currently, no POST endpoints but leave a small buffer for future requests.
112+
EnableCompression: false,
113+
ListenErrorHandler: options.ListenErrorHandler,
114+
TLSConfig: options.TLSConfig,
115+
MinReqFileDescs: 16,
93116
})
94117
if err != nil {
95118
mws.Destroy()
@@ -106,36 +129,37 @@ func CreateController(opts Options) (*Controller, error) {
106129

107130
// Add middlewares
108131
middlewares := make([]webserver.MiddlewareFunc, 0)
109-
if len(opts.Middlewares) > 0 {
110-
middlewares = append(middlewares, opts.Middlewares...)
132+
if len(options.Middlewares) > 0 {
133+
middlewares = append(middlewares, options.Middlewares...)
111134
}
112135
middlewares = append(middlewares, mws.createAliveMiddleware())
113-
if opts.DisableClientCache {
136+
if options.DisableClientCache {
114137
middlewares = append(middlewares, middleware.DisableClientCache())
115138
}
116-
if opts.IncludeCORS {
139+
if options.IncludeCORS {
117140
middlewares = append(middlewares, middleware.DefaultCORS())
118141
}
119142

120143
// Create middlewares with authorization
121144
middlewaresWithAuth := make([]webserver.MiddlewareFunc, len(middlewares))
122145
copy(middlewaresWithAuth, middlewares)
123-
if len(opts.AccessToken) > 0 {
124-
middlewaresWithAuth = append(middlewaresWithAuth, middleware.ProtectedWithToken(opts.AccessToken))
146+
if len(options.AccessToken) > 0 {
147+
middlewaresWithAuth = append(middlewaresWithAuth, middleware.ProtectedWithToken(options.AccessToken))
125148
}
126149

127150
// Add health handler to web server
128-
if opts.RequestAccessTokenInHealth {
129-
mws.server.GET("/health", mws.getHealthHandler(), middlewaresWithAuth...)
130-
} else {
131-
mws.server.GET("/health", mws.getHealthHandler(), middlewares...)
151+
m := middlewares
152+
if options.RequestAccessTokenInHealth {
153+
m = middlewaresWithAuth
132154
}
155+
mws.server.GET("/health", mws.getHealthHandler(), m...)
156+
mws.server.HEAD("/health", mws.getHealthHandler(), m...)
133157

134158
// Add metrics handler to web server
135159
mws.server.GET("/metrics", mws.getMetricsHandler(), middlewaresWithAuth...)
136160

137161
// Add debug profiles handler to web server
138-
if opts.EnableDebugProfiles {
162+
if options.EnableDebugProfiles {
139163
mws.server.ServeDebugProfiles("/debug/pprof", middlewaresWithAuth...)
140164
}
141165

middlewares.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func (mws *Controller) createAliveMiddleware() webserver.MiddlewareFunc {
2020
req.Error("", http.StatusServiceUnavailable)
2121
return nil
2222
}
23-
2423
}
2524
}
2625
}

0 commit comments

Comments
 (0)