Files
podinfo/pkg/server/server.go
2018-01-05 17:58:39 +02:00

65 lines
1.3 KiB
Go

package server
import (
"net/http"
"os"
"runtime"
"strconv"
"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) {
runtime := map[string]string{
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"version": runtime.Version(),
"max_procs": strconv.FormatInt(int64(runtime.GOMAXPROCS(0)), 10),
"num_goroutine": strconv.FormatInt(int64(runtime.NumGoroutine()), 10),
"num_cpu": strconv.FormatInt(int64(runtime.NumCPU()), 10),
}
runtime["hostname"], _ = os.Hostname()
resp := &Response{
Environment: os.Environ(),
Runtime: runtime,
}
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)
}