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

dunglas / mercure / 19304629705

12 Nov 2025 04:33PM UTC coverage: 84.275% (-2.1%) from 86.414%
19304629705

push

github

web-flow
perf: allocate header values only one time (#1120)

* perf: allocate header values only one time

* fix canocalization

9 of 9 new or added lines in 1 file covered. (100.0%)

121 existing lines in 10 files now uncovered.

1731 of 2054 relevant lines covered (84.27%)

68.69 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

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

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

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

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

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

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

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

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

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

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

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

164✔
64
                return
164✔
65
        }
164✔
66

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
        )
3✔
75
}
76

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

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

86
        if _, ok := h.transport.(TransportSubscribers); !ok {
7✔
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

UNCOV
91
                return
×
92
        }
93

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)
7✔
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