• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dunglas / mercure / 20127563161

11 Dec 2025 09:01AM UTC coverage: 84.119% (-0.2%) from 84.275%
20127563161

Pull #1139

github

web-flow
Update bolt.go

Co-authored-by: Stanislau Kviatkouski <7-zete-7@users.noreply.github.com>
Signed-off-by: Kévin Dunglas <kevin@dunglas.fr>
Pull Request #1139: fix: create bolt.db in Caddy's data dir by default

1732 of 2059 relevant lines covered (84.12%)

69.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.31
/handler.go
1
package mercure
2

3
import (
4
        "context"
5
        "io/fs"
6
        "log/slog"
7
        "net/http"
8

9
        "github.com/gorilla/mux"
10
        "github.com/rs/cors"
11
        "github.com/unrolled/secure"
12
)
13

14
const (
15
        defaultHubURL  = "/.well-known/mercure"
16
        defaultUIURL   = defaultHubURL + "/ui/"
17
        defaultDemoURL = defaultUIURL + "demo/"
18
)
19

168✔
20
func (h *Hub) initHandler(ctx context.Context) {
168✔
21
        router := mux.NewRouter()
168✔
22
        router.UseEncodedPath()
168✔
23
        router.SkipClean(true)
168✔
24

168✔
25
        csp := "default-src 'self'"
168✔
26

170✔
27
        if h.demo {
2✔
28
                router.PathPrefix(defaultDemoURL).HandlerFunc(h.Demo).Methods(http.MethodGet, http.MethodHead)
2✔
29
        }
30

170✔
31
        if h.ui {
2✔
32
                public, err := fs.Sub(uiContent, "public")
2✔
33
                if err != nil {
×
34
                        panic(err)
35
                }
36

2✔
37
                router.PathPrefix(defaultUIURL).Handler(http.StripPrefix(defaultUIURL, http.FileServer(http.FS(public))))
2✔
38

2✔
39
                csp += " mercure.rocks cdn.jsdelivr.net"
40
        }
41

168✔
42
        h.registerSubscriptionHandlers(ctx, router)
168✔
43

333✔
44
        if h.subscriberJWTKeyFunc != nil || h.anonymous {
165✔
45
                router.HandleFunc(defaultHubURL, h.SubscribeHandler).Methods(http.MethodGet, http.MethodHead)
165✔
46
        }
47

331✔
48
        if h.publisherJWTKeyFunc != nil {
163✔
49
                router.HandleFunc(defaultHubURL, h.PublishHandler).Methods(http.MethodPost)
163✔
50
        }
51

168✔
52
        secureMiddleware := secure.New(secure.Options{
168✔
53
                IsDevelopment:         h.debug,
168✔
54
                AllowedHosts:          h.allowedHosts,
168✔
55
                FrameDeny:             true,
168✔
56
                ContentTypeNosniff:    true,
168✔
57
                BrowserXssFilter:      true,
168✔
58
                ContentSecurityPolicy: csp,
168✔
59
        })
168✔
60

333✔
61
        if len(h.corsOrigins) == 0 {
165✔
62
                h.handler = secureMiddleware.Handler(router)
165✔
63

165✔
64
                return
165✔
65
        }
66

3✔
67
        h.handler = secureMiddleware.Handler(
3✔
68
                cors.New(cors.Options{
3✔
69
                        AllowedOrigins:   h.corsOrigins,
3✔
70
                        AllowCredentials: true,
3✔
71
                        AllowedHeaders:   []string{"authorization", "cache-control", "last-event-id"},
3✔
72
                        Debug:            h.debug,
3✔
73
                }).Handler(router),
3✔
74
        )
75
}
76

15✔
77
func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
15✔
78
        h.handler.ServeHTTP(w, r)
15✔
79
}
80

176✔
81
func (h *Hub) registerSubscriptionHandlers(ctx context.Context, r *mux.Router) {
345✔
82
        if !h.subscriptions {
169✔
83
                return
169✔
84
        }
85

7✔
86
        if _, ok := h.transport.(TransportSubscribers); !ok {
×
87
                if h.logger.Enabled(ctx, slog.LevelError) {
×
88
                        h.logger.LogAttrs(ctx, slog.LevelError, "The current transport doesn't support subscriptions. Subscription API disabled.")
×
89
                }
90

×
91
                return
92
        }
93

7✔
94
        r.UseEncodedPath()
7✔
95
        r.SkipClean(true)
7✔
96

7✔
97
        r.HandleFunc(subscriptionURL, h.SubscriptionHandler).Methods(http.MethodGet)
7✔
98
        r.HandleFunc(subscriptionsForTopicURL, h.SubscriptionsHandler).Methods(http.MethodGet)
7✔
99
        r.HandleFunc(subscriptionsURL, h.SubscriptionsHandler).Methods(http.MethodGet)
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc