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

dunglas / mercure / 19233086250

10 Nov 2025 01:22PM UTC coverage: 86.414% (+1.6%) from 84.853%
19233086250

push

github

web-flow
feat!: switch from Zap to log/slog (#1115)

* feat!: switch from Zap to log/slog

* add context to the API

* clean using a custom handler

* fix

* lint/fix

124 of 168 new or added lines in 15 files covered. (73.81%)

9 existing lines in 6 files now uncovered.

1730 of 2002 relevant lines covered (86.41%)

70.89 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
        "net/http"
7

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

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

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

167✔
24
        csp := "default-src 'self'"
167✔
25

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

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

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

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

41
        h.registerSubscriptionHandlers(ctx, router)
167✔
42

167✔
43
        if h.subscriberJWTKeyFunc != nil || h.anonymous {
331✔
44
                router.HandleFunc(defaultHubURL, h.SubscribeHandler).Methods(http.MethodGet, http.MethodHead)
164✔
45
        }
164✔
46

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

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

167✔
60
        if len(h.corsOrigins) == 0 {
331✔
61
                h.handler = secureMiddleware.Handler(router)
164✔
62

164✔
63
                return
164✔
64
        }
164✔
65

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

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

80
func (h *Hub) registerSubscriptionHandlers(ctx context.Context, r *mux.Router) {
175✔
81
        if !h.subscriptions {
343✔
82
                return
168✔
83
        }
168✔
84

85
        if _, ok := h.transport.(TransportSubscribers); !ok {
7✔
NEW
86
                h.logger.ErrorContext(ctx, "The current transport doesn't support subscriptions. Subscription API disabled.")
×
87

×
88
                return
×
UNCOV
89
        }
×
90

91
        r.UseEncodedPath()
7✔
92
        r.SkipClean(true)
7✔
93

7✔
94
        r.HandleFunc(subscriptionURL, h.SubscriptionHandler).Methods(http.MethodGet)
7✔
95
        r.HandleFunc(subscriptionsForTopicURL, h.SubscriptionsHandler).Methods(http.MethodGet)
7✔
96
        r.HandleFunc(subscriptionsURL, h.SubscriptionsHandler).Methods(http.MethodGet)
7✔
97
}
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