internal/gaby: use a new ServeMux

Using the global ServeMux meant that other parts of the code
could register routes that conflicted with ours. Now we
control everything that's registered.

Also, restore the "GET" method to the "/" route.

Change-Id: Iaa25a0430d3b3aad929946114b0c43506e36dde9
Reviewed-on: https://go-review.googlesource.com/c/oscar/+/612076
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Tatiana Bradley <tatianabradley@google.com>
diff --git a/internal/gaby/main.go b/internal/gaby/main.go
index 0327334..ddb4108 100644
--- a/internal/gaby/main.go
+++ b/internal/gaby/main.go
@@ -279,7 +279,8 @@
 	cronEndpointCounter := g.newEndpointCounter(cronEndpoint)
 	githubEventEndpointCounter := g.newEndpointCounter(githubEventEndpoint)
 
-	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+	mux := http.NewServeMux()
+	mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
 		fmt.Fprintf(w, "Gaby\n")
 		fmt.Fprintf(w, "meta: %+v\n", g.meta)
 		fmt.Fprintf(w, "flags: %+v\n", flags)
@@ -288,7 +289,7 @@
 
 	// setlevel changes the log level dynamically.
 	// Usage: /setlevel?l=LEVEL
-	http.HandleFunc("GET /"+setLevelEndpoint, func(w http.ResponseWriter, r *http.Request) {
+	mux.HandleFunc("GET /"+setLevelEndpoint, func(w http.ResponseWriter, r *http.Request) {
 		if err := g.slogLevel.UnmarshalText([]byte(r.FormValue("l"))); err != nil {
 			http.Error(w, err.Error(), http.StatusBadRequest)
 			return
@@ -298,7 +299,7 @@
 	})
 
 	// cronEndpoint is called periodically by a Cloud Scheduler job.
-	http.HandleFunc("GET /"+cronEndpoint, func(w http.ResponseWriter, r *http.Request) {
+	mux.HandleFunc("GET /"+cronEndpoint, func(w http.ResponseWriter, r *http.Request) {
 		g.slog.Info(cronEndpoint + " start")
 		defer g.slog.Info(cronEndpoint + " end")
 
@@ -314,7 +315,7 @@
 
 	// githubEventEndpoint is called by a GitHub webhook when a new
 	// event occurs on the githubProject repo.
-	http.HandleFunc("POST /"+githubEventEndpoint, func(w http.ResponseWriter, r *http.Request) {
+	mux.HandleFunc("POST /"+githubEventEndpoint, func(w http.ResponseWriter, r *http.Request) {
 		g.slog.Info(githubEventEndpoint + " start")
 		defer g.slog.Info(githubEventEndpoint + " end")
 
@@ -331,7 +332,7 @@
 
 	// /search: display a form for vector similarity search.
 	// /search?q=...: perform a search using the value of q as input.
-	http.HandleFunc("GET /search", g.handleSearch)
+	mux.HandleFunc("GET /search", g.handleSearch)
 	// Listen in this goroutine so that we can return a synchronous error
 	// if the port is already in use or the address is otherwise invalid.
 	// Run the actual server in a background goroutine.
@@ -340,7 +341,7 @@
 		log.Fatal(err)
 	}
 	go func() {
-		log.Fatal(http.Serve(l, nil))
+		log.Fatal(http.Serve(l, mux))
 	}()
 }