Skip to content

Commit c25ee34

Browse files
authored
Merge pull request #7 from schigh/chore/remove-chi-dependency
chore/remove chi dependency
2 parents adbad9f + 5df8f39 commit c25ee34

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ bin/
22
*.env
33
.idea
44
.vscode
5-
cover.out
5+
cover.out
6+
sandbox/

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func main() {
9292
```
9393

9494
## Planned updates
95-
- currently the http reporter used the chi muxxer. I plan to remove it and just use servemux
95+
- ~currently the http reporter used the chi muxxer. I plan to remove it and just use servemux~
9696
- The documentation was hastily assembled, it's definitely WIP
9797
- add more examples
9898
- add more basic checkers

go.mod

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ module github.com/schigh/health
22

33
go 1.21.0
44

5-
require (
6-
github.com/go-chi/chi/v5 v5.0.12
7-
google.golang.org/protobuf v1.32.0
8-
)
5+
require google.golang.org/protobuf v1.32.0
96

107
require (
118
go.uber.org/mock v0.4.0

go.sum

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
2-
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
31
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
2+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
43
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
54
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
6-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
7-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
85
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
96
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
107
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=

reporter/httpserver/reporter.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
"errors"
77
"fmt"
88
"net/http"
9+
"strings"
910
"sync"
1011
"sync/atomic"
1112
"time"
1213

13-
"github.com/go-chi/chi/v5"
14-
chimiddleware "github.com/go-chi/chi/v5/middleware"
1514
"google.golang.org/protobuf/encoding/protojson"
1615

1716
"github.com/schigh/health"
@@ -48,23 +47,34 @@ func NewReporter(cfg Config) *Reporter {
4847
reporter.logger = health.NoOpLogger{}
4948
}
5049

51-
router := chi.NewRouter()
52-
router.Use(chimiddleware.Recoverer)
53-
router.Use(chimiddleware.Timeout(60 * time.Second))
54-
router.Route("/health", func(r chi.Router) {
55-
r.Get(cfg.LivenessRoute, reporter.reportLiveness)
56-
r.Get(cfg.ReadinessRoute, reporter.reportReadiness)
57-
})
50+
mux := http.NewServeMux()
51+
mux.HandleFunc(fmt.Sprintf("/health/%s", strings.TrimPrefix(cfg.LivenessRoute, "/")), reporter.reportLiveness)
52+
mux.HandleFunc(fmt.Sprintf("/health/%s", strings.TrimPrefix(cfg.ReadinessRoute, "/")), reporter.reportReadiness)
53+
handler := reporter.Recover(
54+
http.TimeoutHandler(mux, 60*time.Second, "the request timed out"),
55+
)
5856

5957
reporter.server = &http.Server{
6058
ReadHeaderTimeout: 3 * time.Second,
6159
Addr: fmt.Sprintf("%s:%d", cfg.Addr, cfg.Port),
62-
Handler: router,
60+
Handler: handler,
6361
}
6462

6563
return &reporter
6664
}
6765

66+
func (r *Reporter) Recover(next http.Handler) http.Handler {
67+
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
68+
defer func() {
69+
if recovered := recover(); recovered != nil {
70+
r.logger.Error("health.reporter.httpserver: recovered from panic: %v", recovered)
71+
http.Error(rw, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
72+
}
73+
}()
74+
next.ServeHTTP(rw, req)
75+
})
76+
}
77+
6878
func (r *Reporter) Run(_ context.Context) error {
6979
if !atomic.CompareAndSwapUint32(&r.running, 0, 1) {
7080
return errors.New("health.reporters.httpserver: Run - reporter is already running")

0 commit comments

Comments
 (0)