mirror of
https://github.com/stefanprodan/podinfo.git
synced 2026-03-04 02:50:20 +00:00
54 lines
978 B
Go
54 lines
978 B
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"runtime"
|
|
|
|
"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)
|
|
|
|
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)
|
|
}
|