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

dunglas / mercure / 20101920966

10 Dec 2025 02:24PM UTC coverage: 84.216% (-0.06%) from 84.275%
20101920966

push

github

web-flow
fix(caddy): prevent Caddy to never stop because of the eternal grace period (#1138)

* fix(caddy): prevent Caddy to never stop because of the eternal grace period

* fix tests

* tests: add test for global context cancellation

* lint

14 of 18 new or added lines in 4 files covered. (77.78%)

1 existing line in 1 file now uncovered.

1734 of 2059 relevant lines covered (84.22%)

71.05 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
        "io/fs"
5
        "log/slog"
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() {
168✔
20
        router := mux.NewRouter()
168✔
21
        router.UseEncodedPath()
168✔
22
        router.SkipClean(true)
168✔
23

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

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

30
        if h.ui {
170✔
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(router)
168✔
42

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

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

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

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

165✔
63
                return
165✔
64
        }
165✔
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(r *mux.Router) {
176✔
81
        if !h.subscriptions {
345✔
82
                return
169✔
83
        }
169✔
84

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

90
                return
×
91
        }
92

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

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