102 lines
2.3 KiB
Go
102 lines
2.3 KiB
Go
package server
|
|
|
|
import (
|
|
lib "com.navi.medici.janus/lib"
|
|
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"compress/gzip"
|
|
)
|
|
|
|
var (
|
|
healthyBool bool = true
|
|
)
|
|
|
|
|
|
func eventsHandler(w http.ResponseWriter, r *http.Request) {
|
|
var reader io.Reader
|
|
|
|
// check if body is gzip compressed
|
|
if r.Header.Get("Content-Encoding") == "gzip" {
|
|
var err error
|
|
reader, err = gzip.NewReader(r.Body)
|
|
if err != nil {
|
|
// log.Printf(err)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
http.Error(w, "Error while decompressing GZIP payload", http.StatusBadRequest)
|
|
return
|
|
}
|
|
} else {
|
|
reader = r.Body
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(reader)
|
|
if err != nil {
|
|
// log.Printf(err)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
http.Error(w, "Request body invalid", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
lib.ProtobufRequestChannel <- &lib.RequestObject{Body: body, Header: r.Header}
|
|
io.WriteString(w, "ok")
|
|
}
|
|
|
|
|
|
func eventsHandlerJson(w http.ResponseWriter, r *http.Request) {
|
|
var reader io.Reader
|
|
|
|
for name, values := range r.Header {
|
|
// Loop over all values for the name.
|
|
for _, value := range values {
|
|
log.Println(name, value)
|
|
}
|
|
}
|
|
|
|
// check if body is gzip compressed
|
|
if r.Header.Get("Content-Encoding") == "gzip" {
|
|
var err error
|
|
reader, err = gzip.NewReader(r.Body)
|
|
if err != nil {
|
|
// log.Printf(err)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
http.Error(w, "Error while decompressing GZIP payload", http.StatusBadRequest)
|
|
return
|
|
}
|
|
} else {
|
|
reader = r.Body
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(reader)
|
|
if err != nil {
|
|
// log.Printf(err)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
http.Error(w, "Request body invalid", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
lib.JsonRequestChannel <- &lib.RequestObject{Body: body, Header: r.Header}
|
|
io.WriteString(w, "ok")
|
|
}
|
|
|
|
|
|
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
|
if healthyBool {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
io.WriteString(w, "true")
|
|
} else {
|
|
http.Error(w, "server unhealthy", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
func healthToggleHandler(w http.ResponseWriter, r *http.Request) {
|
|
healthyBool = !healthyBool
|
|
io.WriteString(w, "toggled")
|
|
}
|