~ogham/doom

fe754de01a5fb66fa59194d37fed0a7af032fea6 — Benjamin Sago 1 year, 10 months ago 9ce268b
Yeet Sentrty
4 files changed, 3 insertions(+), 71 deletions(-)

M go.mod
M go.sum
D mhttp/sentry.go
M mroutes/csp_listener.go
M go.mod => go.mod +1 -6
@@ 2,9 2,4 @@ module git.sr.ht/~ogham/doom

go 1.18

require (
	github.com/getsentry/sentry-go v0.13.0
	github.com/go-chi/chi/v5 v5.0.7
)

require golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 // indirect
require github.com/go-chi/chi/v5 v5.0.7

M go.sum => go.sum +0 -9
@@ 1,11 1,2 @@
github.com/getsentry/sentry-go v0.13.0 h1:20dgTiUSfxRB/EhMPtxcL9ZEbM1ZdR+W/7f7NWD+xWo=
github.com/getsentry/sentry-go v0.13.0/go.mod h1:EOsfu5ZdvKPfeHYV6pTVQnsjfp30+XA7//UooKNumH0=
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2 h1:wM1k/lXfpc5HdkJJyW9GELpd8ERGdnh8sMGL6Gzq3Ho=
golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=

D mhttp/sentry.go => mhttp/sentry.go +0 -36
@@ 1,36 0,0 @@
package mhttp

import (
    "fmt"
    "log"
    "time"

    "github.com/getsentry/sentry-go"
    sentryhttp "github.com/getsentry/sentry-go/http"
)


var SentryMiddleware *sentryhttp.Handler

// ConfigureSentry sets up Sentry integration for the given chi.Router, having it
// send the errors to a Sentry instance based around the given DSN. Returns an
// error if it fails, otherwise returns nil.
func ConfigureSentry(sentryDsn string) error {
    err := sentry.Init(sentry.ClientOptions{
        Dsn:   sentryDsn,
        Debug: true,
    })
    if err != nil {
        return fmt.Errorf("failed to initialise Sentry: %w", err)
    }

    defer sentry.Flush(time.Second)

    SentryMiddleware = sentryhttp.New(sentryhttp.Options{
        Repanic: true,
    })

    log.Println("Sentry reporting enabled")

    return nil
}

M mroutes/csp_listener.go => mroutes/csp_listener.go +2 -20
@@ 1,7 1,6 @@
package mroutes

import (
    "bytes"
    "io"
    "log"
    "net/http"


@@ 11,7 10,7 @@ import (


// AddCspRoutes adds routes that listen on ‘/csp’.
func AddCspRoutes(r chi.Router, reportUri *string) {
func AddCspRoutes(r chi.Router, sender func([]byte)) {
    r.Get("/csp", func(w http.ResponseWriter, req *http.Request) {
        w.WriteHeader(http.StatusNoContent)
    })


@@ 25,24 24,7 @@ func AddCspRoutes(r chi.Router, reportUri *string) {

        log.Println("Received CSP report:")
        log.Println(string(body))

        if reportUri != nil {
            resp, err := http.Post(*reportUri, "application/json", bytes.NewBuffer(body))
            defer func(Body io.ReadCloser) {
                err := Body.Close()
                if err != nil {
                    log.Println("failed to close response body:", err)
                }
            }(resp.Body)

            if err != nil {
                log.Printf("Failed to send CSP report to Sentry: %v", err)
            } else if resp.StatusCode != http.StatusOK {
                log.Printf("Failed to send CSP report to Sentry: status %v", resp.Status)
            } else {
                log.Println("Report sent")
            }
        }
        sender(body)

        w.WriteHeader(http.StatusAccepted)
    })