mirror of
https://github.com/prymitive/karma
synced 2026-08-17 11:27:01 +00:00
chore(backend): migrate to chi v5
This commit is contained in:
committed by
Łukasz Mierzwa
parent
f52e7c68fb
commit
88d7d81982
@@ -7,8 +7,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prymitive/karma/internal/config"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
+3
-4
@@ -26,10 +26,9 @@ import (
|
||||
"github.com/prymitive/karma/internal/transform"
|
||||
"github.com/prymitive/karma/internal/uri"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
"github.com/loikg/ravenchi"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
@@ -86,7 +85,7 @@ func setupRouter(router *chi.Mux) {
|
||||
_ = mime.AddExtensionType(".ico", "image/x-icon")
|
||||
|
||||
router.Use(promMiddleware)
|
||||
router.Use(ravenchi.SentryRecovery)
|
||||
router.Use(sentryRecovery)
|
||||
router.Use(middleware.RealIP)
|
||||
|
||||
compressor := middleware.NewCompressor(flate.DefaultCompression)
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/prymitive/karma/internal/alertmanager"
|
||||
"github.com/prymitive/karma/internal/config"
|
||||
"github.com/prymitive/karma/internal/mapper"
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/getsentry/raven-go"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// copied from https://github.com/loikg/ravenchi, adapted for chi v5
|
||||
func sentryRecovery(handler http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if rval := recover(); rval != nil {
|
||||
log.Error().Err(fmt.Errorf("%+v", rval)).Bytes("stack", debug.Stack()).Msg("Panic")
|
||||
rvalStr := fmt.Sprint(rval)
|
||||
var packet *raven.Packet
|
||||
if err, ok := rval.(error); ok {
|
||||
packet = raven.NewPacket(rvalStr, raven.NewException(errors.New(rvalStr), raven.GetOrNewStacktrace(err, 2, 3, nil)), raven.NewHttp(r))
|
||||
} else {
|
||||
packet = raven.NewPacket(rvalStr, raven.NewException(errors.New(rvalStr), raven.NewStacktrace(2, 3, nil)), raven.NewHttp(r))
|
||||
}
|
||||
raven.Capture(packet, nil)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
}
|
||||
}()
|
||||
handler.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
func TestSentryRecovery(t *testing.T) {
|
||||
r := chi.NewRouter()
|
||||
r.Use(sentryRecovery)
|
||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
panic("catched")
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
resp := httptest.NewRecorder()
|
||||
r.ServeHTTP(resp, req)
|
||||
if resp.Code != http.StatusInternalServerError {
|
||||
t.Errorf("GET / returned status %d", resp.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSentryRecoveryWithError(t *testing.T) {
|
||||
r := chi.NewRouter()
|
||||
r.Use(sentryRecovery)
|
||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
panic(errors.New("catched error"))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
resp := httptest.NewRecorder()
|
||||
r.ServeHTTP(resp, req)
|
||||
if resp.Code != http.StatusInternalServerError {
|
||||
t.Errorf("GET / returned status %d", resp.Code)
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/prymitive/karma/internal/alertmanager"
|
||||
"github.com/prymitive/karma/internal/config"
|
||||
"github.com/prymitive/karma/internal/mock"
|
||||
|
||||
@@ -8,9 +8,9 @@ require (
|
||||
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08
|
||||
github.com/elazarl/go-bindata-assetfs v1.0.1
|
||||
github.com/fvbommel/sortorder v1.0.2
|
||||
github.com/getsentry/raven-go v0.2.0 // indirect
|
||||
github.com/getsentry/raven-go v0.2.0
|
||||
github.com/getsentry/sentry-go v0.10.0
|
||||
github.com/go-chi/chi v1.5.4
|
||||
github.com/go-chi/chi/v5 v5.0.0
|
||||
github.com/go-chi/cors v1.1.1
|
||||
github.com/go-openapi/errors v0.20.0
|
||||
github.com/go-openapi/runtime v0.19.26
|
||||
@@ -21,7 +21,6 @@ require (
|
||||
github.com/hansrodtang/randomcolor v0.0.0-20160512071917-d27108b3d7a5
|
||||
github.com/jarcoal/httpmock v1.0.8
|
||||
github.com/knadh/koanf v0.15.0
|
||||
github.com/loikg/ravenchi v2.0.0+incompatible
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/mitchellh/mapstructure v1.4.1
|
||||
github.com/patrickmn/go-cache v2.1.1-0.20180815053127-5633e0862627+incompatible
|
||||
|
||||
@@ -115,8 +115,8 @@ github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/
|
||||
github.com/globalsign/mgo v0.0.0-20180905125535-1ca0a4f7cbcb/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
|
||||
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
|
||||
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
|
||||
github.com/go-chi/chi v1.5.4 h1:QHdzF2szwjqVV4wmByUnTcsbIg7UGaQ0tPF2t5GcAIs=
|
||||
github.com/go-chi/chi v1.5.4/go.mod h1:uaf8YgoFazUOkPBG7fxPftUylNumIev9awIWOENIuEg=
|
||||
github.com/go-chi/chi/v5 v5.0.0 h1:DBPx88FjZJH3FsICfDAfIfnb7XxKIYVGG6lOPlhENAg=
|
||||
github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs=
|
||||
github.com/go-chi/cors v1.1.1 h1:eHuqxsIw89iXcWnWUN8R72JMibABJTN/4IOYI5WERvw=
|
||||
github.com/go-chi/cors v1.1.1/go.mod h1:K2Yje0VW/SJzxiyMYu6iPQYa7hMjQX2i/F491VChg1I=
|
||||
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
|
||||
@@ -396,8 +396,6 @@ github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvf
|
||||
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
|
||||
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
|
||||
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
|
||||
github.com/loikg/ravenchi v2.0.0+incompatible h1:bHcWvqS6JDecE0SnsXyJdpmdN9lTojGLB7wxllclaf0=
|
||||
github.com/loikg/ravenchi v2.0.0+incompatible/go.mod h1:f07lBuSUJjVbBYFKrsRcdqSlBsSl44E4C0AyjKCEsMA=
|
||||
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
|
||||
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
|
||||
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
|
||||
Reference in New Issue
Block a user