Skip to content
This repository was archived by the owner on Mar 29, 2025. It is now read-only.

Commit 360543b

Browse files
authored
Merge pull request #18 from randlabs/added_fs
V2
2 parents 0694203 + fda9007 commit 360543b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3152
-1068
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
same "printed page" as the copyright notice for easier
188188
identification within third-party archives.
189189

190-
Copyright (C) 2022-2023 RandLabs
190+
Copyright (C) 2022-2024 RandLabs.IO
191191

192192
Licensed under the Apache License, Version 2.0 (the "License");
193193
you may not use this file except in compliance with the License.

METRICS.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Health and Metrics controller
2+
3+
## Usage with example
4+
5+
```golang
6+
package example
7+
8+
import (
9+
"encoding/json"
10+
"math/rand"
11+
12+
"github.com/randlabs/go-webserver/v2/metrics"
13+
)
14+
15+
func main() {
16+
// Create a new health & metrics controller with a web server
17+
srvOpts := metrics.Options{
18+
Address: "127.0.0.1",
19+
Port: 3000,
20+
HealthCallback: healthCallback, // Setup our health check callback
21+
EnableDebugProfiles: true,
22+
}
23+
mc, err := metrics.CreateController(srvOpts)
24+
if err != nil {
25+
// handle error
26+
}
27+
defer mc.Stop()
28+
29+
// Create a custom prometheus counter
30+
err = mc.NewCounterWithCallback(
31+
"random_counter", "A random counter",
32+
func() float64 {
33+
// Return the counter value.
34+
// The common scenario is to have a shared set of variables you regularly update with the current
35+
// state of your application.
36+
return rand.Float64()
37+
},
38+
)
39+
40+
// Start health & metrics web server
41+
err = mc.Start()
42+
if err != nil {
43+
// handle error
44+
}
45+
46+
// your app code may go here
47+
}
48+
49+
// Health output will be in JSON format.
50+
type exampleHealthOutput struct {
51+
Status string `json:"status"`
52+
}
53+
54+
// Our health callback routine.
55+
func healthCallback() string {
56+
state := exampleHealthOutput{
57+
Status: "ok",
58+
}
59+
60+
j, _ := json.Marshal(state)
61+
return string(j)
62+
}
63+
```

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ import (
1313
"os/signal"
1414
"syscall"
1515

16-
webserver "github.com/randlabs/go-webserver"
17-
"github.com/randlabs/go-webserver/middleware"
18-
"github.com/randlabs/go-webserver/request"
16+
webserver "github.com/randlabs/go-webserver/v2"
17+
"github.com/randlabs/go-webserver/v2/middleware"
1918
)
2019

2120
type testApiOutput struct {
@@ -59,7 +58,7 @@ func main() {
5958
srv.Stop()
6059
}
6160

62-
func getTestApi(req *request.RequestContext) error {
61+
func getTestApi(req *webserver.RequestContext) error {
6362
// Prepare output
6463
output := testApiOutput{
6564
Status: "all systems operational",

debug_profiles.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package go_webserver
2+
3+
import (
4+
httpprof "net/http/pprof"
5+
"runtime/pprof"
6+
"strings"
7+
)
8+
9+
// -----------------------------------------------------------------------------
10+
11+
// ServeDebugProfiles adds the GO runtime profile handlers to a web server
12+
func (srv *Server) ServeDebugProfiles(basePath string, middlewares ...HandlerFunc) {
13+
if !strings.HasPrefix(basePath, "/") {
14+
basePath = "/" + basePath
15+
}
16+
if !strings.HasSuffix(basePath, "/") {
17+
basePath = basePath + "/"
18+
}
19+
20+
srv.GET(basePath, NewHandlerFromHttpHandlerFunc(httpprof.Index), middlewares...)
21+
22+
for _, profile := range pprof.Profiles() {
23+
h := httpprof.Handler(profile.Name())
24+
srv.GET(basePath+profile.Name(), NewHandlerFromHttpHandler(h), middlewares...)
25+
}
26+
srv.GET(basePath+"cmdline", NewHandlerFromHttpHandlerFunc(httpprof.Cmdline), middlewares...)
27+
srv.GET(basePath+"profile", NewHandlerFromHttpHandlerFunc(httpprof.Profile), middlewares...)
28+
srv.GET(basePath+"symbol", NewHandlerFromHttpHandlerFunc(httpprof.Symbol), middlewares...)
29+
srv.GET(basePath+"trace", NewHandlerFromHttpHandlerFunc(httpprof.Trace), middlewares...)
30+
}

go.mod

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
1-
module github.com/randlabs/go-webserver
1+
module github.com/randlabs/go-webserver/v2
22

3-
go 1.19
3+
go 1.21
4+
5+
toolchain go1.22.2
46

57
require (
6-
github.com/fasthttp/router v1.4.22
7-
github.com/valyala/fasthttp v1.51.0
8+
github.com/VictoriaMetrics/fastcache v1.12.2
9+
github.com/fasthttp/router v1.5.0
10+
github.com/valyala/fasthttp v1.52.0
811
)
912

1013
require (
11-
github.com/andybalholm/brotli v1.0.6 // indirect
12-
github.com/klauspost/compress v1.17.4 // indirect
13-
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
14+
github.com/andybalholm/brotli v1.1.0 // indirect
15+
github.com/beorn7/perks v1.0.1 // indirect
16+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
17+
github.com/golang/snappy v0.0.4 // indirect
18+
github.com/klauspost/compress v1.17.8 // indirect
19+
github.com/prometheus/client_golang v1.19.0 // indirect
20+
github.com/prometheus/client_model v0.6.1 // indirect
21+
github.com/prometheus/common v0.53.0 // indirect
22+
github.com/prometheus/procfs v0.14.0 // indirect
23+
github.com/randlabs/rundown-protection v1.1.1 // indirect
24+
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511 // indirect
1425
github.com/valyala/bytebufferpool v1.0.0 // indirect
1526
github.com/valyala/tcplisten v1.0.0 // indirect
16-
golang.org/x/sys v0.13.0 // indirect
27+
golang.org/x/sys v0.20.0 // indirect
28+
google.golang.org/protobuf v1.34.0 // indirect
1729
)

go.sum

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
1-
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
2-
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
3-
github.com/fasthttp/router v1.4.22 h1:qwWcYBbndVDwts4dKaz+A2ehsnbKilmiP6pUhXBfYKo=
4-
github.com/fasthttp/router v1.4.22/go.mod h1:KeMvHLqhlB9vyDWD5TSvTccl9qeWrjSSiTJrJALHKV0=
5-
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
6-
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
7-
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk=
8-
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g=
1+
github.com/VictoriaMetrics/fastcache v1.12.2 h1:N0y9ASrJ0F6h0QaC3o6uJb3NIZ9VKLjCM7NQbSmF7WI=
2+
github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkThDcMsQicp4xDukwJYI=
3+
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah4HI848JfFxHt+iPb26b4zyfspmqY0/8=
4+
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
5+
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
6+
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
7+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
8+
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
9+
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
10+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
11+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
12+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14+
github.com/fasthttp/router v1.5.0 h1:3Qbbo27HAPzwbpRzgiV5V9+2faPkPt3eNuRaDV6LYDA=
15+
github.com/fasthttp/router v1.5.0/go.mod h1:FddcKNXFZg1imHcy+uKB0oo/o6yE9zD3wNguqlhWDak=
16+
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
17+
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
18+
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
19+
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
20+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
21+
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
22+
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
23+
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
24+
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
25+
github.com/prometheus/common v0.53.0 h1:U2pL9w9nmJwJDa4qqLQ3ZaePJ6ZTwt7cMD3AG3+aLCE=
26+
github.com/prometheus/common v0.53.0/go.mod h1:BrxBKv3FWBIGXw89Mg1AeBq7FSyRzXWI3l3e7W3RN5U=
27+
github.com/prometheus/procfs v0.14.0 h1:Lw4VdGGoKEZilJsayHf0B+9YgLGREba2C6xr+Fdfq6s=
28+
github.com/prometheus/procfs v0.14.0/go.mod h1:XL+Iwz8k8ZabyZfMFHPiilCniixqQarAy5Mu67pHlNQ=
29+
github.com/randlabs/rundown-protection v1.1.1 h1:M0ltneeZ0efBPERxWOLC5JbCP0I3TZce41IUQoQNNJA=
30+
github.com/randlabs/rundown-protection v1.1.1/go.mod h1:rCfZhLfCKhedcQ0uQvpZbvyaCsPTsreynI2FqwuesGA=
31+
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511 h1:KanIMPX0QdEdB4R3CiimCAbxFrhB3j7h0/OvpYGVQa8=
32+
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511/go.mod h1:sM7Mt7uEoCeFSCBM+qBrqvEo+/9vdmj19wzp3yzUhmg=
33+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
34+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
935
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
1036
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
11-
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
12-
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
37+
github.com/valyala/fasthttp v1.52.0 h1:wqBQpxH71XW0e2g+Og4dzQM8pk34aFYlA1Ga8db7gU0=
38+
github.com/valyala/fasthttp v1.52.0/go.mod h1:hf5C4QnVMkNXMspnsUlfM3WitlgYflyhHYoKol/szxQ=
1339
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
1440
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
15-
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
16-
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
41+
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
42+
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
43+
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
44+
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
45+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
46+
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
47+
google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

0 commit comments

Comments
 (0)