Skip to content

Commit 1d26977

Browse files
committed
git-bundle-web-server: do not use global 'HandleFunc()'
In 684a295 (web-server: implement graceful shutdown, 2023-01-06), we switched from using the global HTTP server in 'git-bundle-web-server' to an instance of 'http.Server'. In keeping with that change from global to local instances, switch from using the global 'http.HandleFunc()' to process requests to an instance of 'http.ServeMux'. Signed-off-by: Victoria Dye <[email protected]>
1 parent 542ae0e commit 1d26977

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

cmd/git-bundle-web-server/main.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,7 @@ func serve(w http.ResponseWriter, r *http.Request) {
8383
w.Write(data)
8484
}
8585

86-
func createAndStartServer(address string, serverWaitGroup *sync.WaitGroup) *http.Server {
87-
// Create the HTTP server
88-
server := &http.Server{Addr: address}
89-
90-
// API routes
91-
http.HandleFunc("/", serve)
92-
86+
func startServer(server *http.Server, serverWaitGroup *sync.WaitGroup) {
9387
// Add to wait group
9488
serverWaitGroup.Add(1)
9589

@@ -103,16 +97,21 @@ func createAndStartServer(address string, serverWaitGroup *sync.WaitGroup) *http
10397
}
10498
}()
10599

106-
fmt.Println("Server is running at address " + address)
107-
return server
100+
fmt.Println("Server is running at address " + server.Addr)
108101
}
109102

110103
func main() {
104+
// Configure the server
105+
mux := http.NewServeMux()
106+
mux.HandleFunc("/", serve)
107+
server := &http.Server{
108+
Handler: mux,
109+
Addr: ":8080",
110+
}
111111
serverWaitGroup := &sync.WaitGroup{}
112112

113113
// Start the server asynchronously
114-
port := ":8080"
115-
server := createAndStartServer(port, serverWaitGroup)
114+
startServer(server, serverWaitGroup)
116115

117116
// Intercept interrupt signals
118117
c := make(chan os.Signal, 1)

0 commit comments

Comments
 (0)