Skip to content

Commit

Permalink
Merge pull request #7 from schigh/chore/remove-chi-dependency
Browse files Browse the repository at this point in the history
chore/remove chi dependency
  • Loading branch information
schigh authored Mar 1, 2024
2 parents adbad9f + 5df8f39 commit c25ee34
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ bin/
*.env
.idea
.vscode
cover.out
cover.out
sandbox/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func main() {
```

## Planned updates
- currently the http reporter used the chi muxxer. I plan to remove it and just use servemux
- ~currently the http reporter used the chi muxxer. I plan to remove it and just use servemux~
- The documentation was hastily assembled, it's definitely WIP
- add more examples
- add more basic checkers
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ module github.com/schigh/health

go 1.21.0

require (
github.com/go-chi/chi/v5 v5.0.12
google.golang.org/protobuf v1.32.0
)
require google.golang.org/protobuf v1.32.0

require (
go.uber.org/mock v0.4.0
Expand Down
5 changes: 1 addition & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
Expand Down
30 changes: 20 additions & 10 deletions reporter/httpserver/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"sync"
"sync/atomic"
"time"

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

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

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

reporter.server = &http.Server{
ReadHeaderTimeout: 3 * time.Second,
Addr: fmt.Sprintf("%s:%d", cfg.Addr, cfg.Port),
Handler: router,
Handler: handler,
}

return &reporter
}

func (r *Reporter) Recover(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
defer func() {
if recovered := recover(); recovered != nil {
r.logger.Error("health.reporter.httpserver: recovered from panic: %v", recovered)
http.Error(rw, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
}
}()
next.ServeHTTP(rw, req)
})
}

func (r *Reporter) Run(_ context.Context) error {
if !atomic.CompareAndSwapUint32(&r.running, 0, 1) {
return errors.New("health.reporters.httpserver: Run - reporter is already running")
Expand Down

0 comments on commit c25ee34

Please sign in to comment.