Skip to content

Commit a341893

Browse files
authored
Consolidate mux initialization (efixler#38)
1 parent c96777d commit a341893

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

cmd/scrape-server/main.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@ func main() {
7878
slog.Info("scrape-server authorization is disabled, running in open access mode")
7979
}
8080

81-
mux, err := server.InitMux(ss, dbh, publicHome.Get())
81+
mux, err := server.InitMux(
82+
ss,
83+
dbh,
84+
publicHome.Get(),
85+
profile.Get(),
86+
)
8287
if err != nil {
8388
slog.Error("scrape-server error initializing the server's mux", "error", err)
8489
os.Exit(1)
8590
}
86-
if profile.Get() {
87-
server.EnableProfiling(mux)
88-
}
8991
s := &http.Server{
9092
Addr: fmt.Sprintf(":%d", port.Get()),
9193
Handler: mux,

internal/server/routes.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@ import (
88
"github.com/efixler/scrape/internal/server/healthchecks"
99
)
1010

11-
func InitMux(ss *scrapeServer, db *database.DBHandle, openHome bool) (*http.ServeMux, error) {
11+
// Mux Initialization Arguments:
12+
// - ss: scrapeServer: used for setting up API routes
13+
// - db: database.DBHandle: used for healthchecks
14+
// - openHome: bool: if true, the the page will always be open, even
15+
// if auth is enabled
16+
// - enableProfiling: bool: if true, pprof routes will be added to the mux
17+
func InitMux(
18+
ss *scrapeServer,
19+
db *database.DBHandle,
20+
openHome bool,
21+
enableProfiling bool,
22+
) (*http.ServeMux, error) {
1223
mux := http.NewServeMux()
1324
mux.HandleFunc("GET /{$}", homeHandler(ss, openHome))
1425
mux.Handle("/assets/", assetsHandler())
@@ -24,13 +35,12 @@ func InitMux(ss *scrapeServer, db *database.DBHandle, openHome bool) (*http.Serv
2435
mux.HandleFunc("GET /feed", h)
2536
mux.HandleFunc("POST /feed", h)
2637
mux.Handle("GET /.well-known/", healthchecks.Handler("/.well-known", db))
38+
if enableProfiling {
39+
initPProf(mux)
40+
}
2741
return mux, nil
2842
}
2943

30-
func EnableProfiling(mux *http.ServeMux) {
31-
initPProf(mux)
32-
}
33-
3444
func initPProf(mux *http.ServeMux) {
3545
// pprof
3646
mux.HandleFunc("GET /debug/pprof/", http.HandlerFunc(pprof.Index))

internal/server/routes_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestWellknown(t *testing.T) {
1515
//ctx, cancel := context.WithCancel(context.Background())
1616
//defer cancel()
1717

18-
mux, err := InitMux(&scrapeServer{}, nil, false)
18+
mux, err := InitMux(&scrapeServer{}, nil, false, false)
1919
if err != nil {
2020
t.Fatal(err)
2121
}
@@ -61,7 +61,7 @@ func TestExtractErrors(t *testing.T) {
6161
WithURLFetcher(trafilatura.MustNew(nil)),
6262
)
6363

64-
mux, err := InitMux(ss, nil, false)
64+
mux, err := InitMux(ss, nil, false, false)
6565
if err != nil {
6666
t.Fatal(err)
6767
}

internal/server/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestBatchReponseIsValid(t *testing.T) {
106106
WithURLFetcher(fetcher),
107107
)
108108

109-
mux, err := InitMux(ss, nil, false)
109+
mux, err := InitMux(ss, nil, false, false)
110110
if err != nil {
111111
t.Fatal(err)
112112
}

0 commit comments

Comments
 (0)