Files
janus/server/handlers.go
2022-03-31 16:24:32 +05:30

143 lines
3.5 KiB
Go

package server
import (
lib "com.navi.medici.janus/lib"
producer "com.navi.medici.janus/producer"
"fmt"
"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 eventsHandler(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 {
// fmt.Println("HEADER: ", 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.ProtobufRequestChannel <- &lib.RequestObject{Body: body, Header: r.Header}
io.WriteString(w, "ok")
}
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
}
}
func healthToggleHandler(w http.ResponseWriter, r *http.Request) {
healthyBool = !healthyBool
io.WriteString(w, "toggled")
}
func getSchemaHandler(w http.ResponseWriter, r *http.Request) {
schemaMapJson, _ := json.Marshal(producer.SchemaVersionMap)
w.Header().Set("Content-Type", "application/json")
w.Write(schemaMapJson)
}
func refreshSchemaHandler(w http.ResponseWriter, r *http.Request) {
producer.GetSchemaVersions()
io.WriteString(w, "Updated Schema Map")
}
func addSchemaHandler(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
var bodyJson NewSchemaRequest
error := json.Unmarshal(body, &bodyJson)
fmt.Println(error)
errorSchema := producer.AddSchema(bodyJson.Topic, bodyJson.SchemaType, bodyJson.Schema)
fmt.Println("errorSchema: ")
fmt.Println(error)
if errorSchema != nil {
http.Error(w, fmt.Sprintf("Error creating the schema %s", errorSchema), http.StatusBadRequest)
} else {
io.WriteString(w, "added")
}
}