extract handlers

This commit is contained in:
Stefan Prodan
2018-01-08 19:43:00 +02:00
parent 20c45d997f
commit fc479d6073
2 changed files with 64 additions and 54 deletions
+61
View File
@@ -0,0 +1,61 @@
package server
import (
"io/ioutil"
"net/http"
"sync/atomic"
"github.com/golang/glog"
"gopkg.in/yaml.v2"
)
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()))
return
}
w.Header().Set("Content-Type", "text/x-yaml; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
w.Write(d)
}
func (s *Server) echo(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
body, err := ioutil.ReadAll(r.Body)
if err != nil {
glog.Errorf("Reading the request body failed: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
glog.Infof("Payload received from %s: %s", r.RemoteAddr, string(body))
w.Write(body)
w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusNotAcceptable)
}
}
func (s *Server) healthz(w http.ResponseWriter, r *http.Request) {
if atomic.LoadInt32(&status) == 1 {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
return
}
w.WriteHeader(http.StatusServiceUnavailable)
}
func (s *Server) panic(w http.ResponseWriter, r *http.Request) {
glog.Fatal("Kill switch triggered")
}
+3 -54
View File
@@ -2,7 +2,6 @@ package server
import (
"context"
"io/ioutil"
"net/http"
"runtime"
"sync/atomic"
@@ -10,7 +9,6 @@ import (
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/yaml.v2"
)
var status int32
@@ -35,57 +33,6 @@ func New(options ...func(*Server)) *Server {
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()))
return
}
w.Header().Set("Content-Type", "text/x-yaml; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusOK)
w.Write(d)
}
func (s *Server) echo(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
body, err := ioutil.ReadAll(r.Body)
if err != nil {
glog.Errorf("Reading the request body failed: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
glog.Infof("Payload received from %s: %s", r.RemoteAddr, string(body))
w.Write(body)
w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusNotAcceptable)
}
}
func (s *Server) healthz(w http.ResponseWriter, r *http.Request) {
if atomic.LoadInt32(&status) == 1 {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
return
}
w.WriteHeader(http.StatusServiceUnavailable)
}
func (s *Server) panic(w http.ResponseWriter, r *http.Request) {
glog.Fatal("Kill switch triggered")
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", runtime.Version())
@@ -112,10 +59,12 @@ func ListenAndServe(port string, timeout time.Duration, stopCh <-chan struct{})
// wait for SIGTERM or SIGINT
<-stopCh
atomic.StoreInt32(&status, 0)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
// all calls to /healthz will fail from now on
atomic.StoreInt32(&status, 0)
glog.Infof("Shutting down HTTP server with timeout: %v", timeout)
if err := srv.Shutdown(ctx); err != nil {