package server import ( "net/http" "runtime" "github.com/prometheus/client_golang/prometheus/promhttp" "gopkg.in/yaml.v2" ) type Server struct { mux *http.ServeMux } func New(options ...func(*Server)) *Server { s := &Server{mux: http.NewServeMux()} for _, f := range options { f(s) } s.mux.HandleFunc("/", s.index) s.mux.HandleFunc("/healthz/", s.healthz) s.mux.Handle("/metrics", promhttp.Handler()) return s } func (s *Server) index(w http.ResponseWriter, r *http.Request) { resp, err := makeResponse() if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) } d, err := yaml.Marshal(resp) if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) } w.Header().Set("Content-Type", "text/x-yaml") w.Write(d) } func (s *Server) healthz(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte("OK")) } func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Server", runtime.Version()) s.mux.ServeHTTP(w, r) }