json support

This commit is contained in:
“nishant-sharma”
2021-04-27 16:40:13 +05:30
parent 256b56e9ad
commit 28a6c1ddff
6 changed files with 122 additions and 22 deletions

View File

@@ -7,26 +7,83 @@ import (
"io/ioutil"
"log"
"net/http"
"compress/gzip"
)
var (
healthyBool bool = true
)
func eventsHandler(w http.ResponseWriter, r *http.Request) {
var reader io.Reader
reader = r.Body
body, err := ioutil.ReadAll(reader)
if err == nil {
log.Printf(string(body))
// 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.RequestChannel <- &lib.RequestObject{Body: body, Header: r.Header}
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")
@@ -37,6 +94,7 @@ func healthHandler(w http.ResponseWriter, r *http.Request) {
}
}
func healthToggleHandler(w http.ResponseWriter, r *http.Request) {
healthyBool = !healthyBool
io.WriteString(w, "toggled")

View File

@@ -31,8 +31,10 @@ func NewServer(port string) (*Server, error) {
router := mux.NewRouter()
router.HandleFunc("/events", eventsHandler).Methods("POST")
router.HandleFunc("/events/json", eventsHandlerJson).Methods("POST")
router.HandleFunc("/health", healthHandler).Methods("GET")
router.HandleFunc("/health/toggle", healthToggleHandler).Methods("GET")
// router.HandleFunc("/test", testHandler).Methods("GET")
// router.HandleFunc("/schema/refresh", schemaRefreshHandler).Methods("POST")
// router.HandleFunc("/stop", stopHandler).Methods("POST")
@@ -41,4 +43,3 @@ func NewServer(port string) (*Server, error) {
return newServer, nil
}