-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_river.go
More file actions
68 lines (58 loc) · 1.83 KB
/
debug_river.go
File metadata and controls
68 lines (58 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package peerdb
import (
"context"
"log/slog"
"net/http"
"github.com/rs/zerolog"
slogzerolog "github.com/samber/slog-zerolog/v2"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/waf"
"riverqueue.com/riverui"
)
// debugRiverPrefix is the URL path at which the River UI is mounted.
const debugRiverPrefix = "/debug/river"
// initDebugRiverHandler creates the site's River UI handler, mounted at
// debugRiverPrefix, and starts its background services. The services stop
// when ctx is cancelled.
func (s *Site) initDebugRiverHandler(ctx context.Context, logger zerolog.Logger) errors.E {
l := slog.New(slogzerolog.Option{
Level: slogzerolog.ZeroLogLeveler{Logger: &logger},
Logger: &logger,
NoTimestamp: true,
Converter: nil,
AttrFromContext: nil,
AddSource: false,
ReplaceAttr: nil,
}.NewZerologHandler())
handler, err := riverui.NewHandler(&riverui.HandlerOpts{
Endpoints: riverui.NewEndpoints(s.RiverClient, nil),
Logger: l,
Prefix: debugRiverPrefix,
DevMode: false,
JobListHideArgsByDefault: false,
LiveFS: false,
})
if err != nil {
return errors.WithStack(err)
}
err = handler.Start(ctx)
if err != nil {
return errors.WithStack(err)
}
s.debugRiverHandler = handler
return nil
}
// DebugRiver handles requests under /debug/river by forwarding them to the
// site's River UI handler.
func (s *Service) DebugRiver(w http.ResponseWriter, req *http.Request, _ waf.Params) {
if !s.Development {
s.NotFoundWithError(w, req, errors.New("not in development mode"))
return
}
site := waf.MustGetSite[*Site](req.Context())
if site.debugRiverHandler == nil {
s.InternalServerErrorWithError(w, req, errors.New("no River UI handler for site"))
return
}
site.debugRiverHandler.ServeHTTP(w, req)
}