2021-03-24 00:12:15 +03:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2021-03-26 15:10:10 +03:00
|
|
|
"context"
|
|
|
|
"github.com/getsentry/sentry-go"
|
2021-03-24 00:12:15 +03:00
|
|
|
sentryhttp "github.com/getsentry/sentry-go/http"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2021-03-26 15:10:10 +03:00
|
|
|
type SentryMiddleware struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2021-03-24 00:12:15 +03:00
|
|
|
func NewSentryMiddleware() func(http.Handler) http.Handler {
|
|
|
|
return func(h http.Handler) http.Handler {
|
|
|
|
return sentryhttp.New(sentryhttp.Options{
|
|
|
|
Repanic: true,
|
2021-03-26 15:10:10 +03:00
|
|
|
}).Handle(&SentryMiddleware{handler: h})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SentryMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := context.WithValue(r.Context(), "-", "-")
|
|
|
|
h.handler.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
if hub := sentry.GetHubFromContext(ctx); hub != nil {
|
|
|
|
if user := GetPrincipal(r); user != nil {
|
|
|
|
hub.Scope().SetUser(sentry.User{ID: user.ID})
|
|
|
|
}
|
2021-03-24 00:12:15 +03:00
|
|
|
}
|
|
|
|
}
|