Files
janus/server/handlers.go
2023-01-23 16:24:36 +05:30

69 lines
1.5 KiB
Go

package server
import (
"com.navi.medici.janus/lib"
"io"
// "log"
"compress/gzip"
"encoding/json"
"io/ioutil"
"net/http"
)
var (
healthyBool bool = true
)
type NewSchemaRequest struct {
Topic string `json:"topic"`
Schema string `json:"schema"`
SchemaType string `json:"schema_type"`
}
type CustomResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
func eventsHandlerJson(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.JsonRequestChannel <- &lib.RequestObject{Body: body, Header: r.Header}
//io.WriteString(w, "ok")
var rsp = CustomResponse{Code: 200, Message: "OK"}
json.NewEncoder(w).Encode(rsp)
}
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
}
}