Skip to content

Commit

Permalink
main: Add /info endpoint
Browse files Browse the repository at this point in the history
This returns information about the managed channels.
  • Loading branch information
matheusd committed Mar 22, 2024
1 parent 506aa9a commit 818a0a9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
Expand All @@ -20,9 +21,27 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "%s\n%s\n", appName, version.String())
}

func newInfoHandler(server *server.Server) func(w http.ResponseWriter, req *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
info, err := server.FetchManagedChannels(req.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "error fetching managed channels: %v\n", err)
log.Errorf("Unable to fetch managed channels: %v", err)
return
}

err = json.NewEncoder(w).Encode(info)
if err != nil {
log.Errorf("Unable to encode info: %v", err)
}
}
}

func handler(s *server.Server) http.Handler {
router := mux.NewRouter().StrictSlash(true)
router.Methods("GET").Path("/").Name("index").HandlerFunc(indexHandler)
router.Methods("GET").Path("/info").Name("info").HandlerFunc(newInfoHandler(s))
server.NewV1Handler(s, router)
logRouterConfig(router)
return router
Expand Down

0 comments on commit 818a0a9

Please sign in to comment.