Files
janus/server/handlers.go

141 lines
3.5 KiB
Go
Raw Normal View History

2021-03-25 13:24:08 +05:30
package server
import (
"com.navi.medici.janus/lib"
"com.navi.medici.janus/schema"
2021-04-27 16:40:13 +05:30
"compress/gzip"
2021-05-06 16:18:54 +05:30
"encoding/json"
"fmt"
"io"
2021-05-26 19:23:56 +05:30
"io/ioutil"
"net/http"
2021-03-25 13:24:08 +05:30
)
2021-03-31 10:14:54 +05:30
var (
healthyBool bool = true
)
2021-05-12 15:26:13 +05:30
type NewSchemaRequest struct {
2021-05-26 19:23:56 +05:30
Topic string `json:"topic"`
Schema string `json:"schema"`
SchemaType string `json:"schema_type"`
2021-05-12 15:26:13 +05:30
}
2021-05-26 19:23:56 +05:30
type CustomResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
2021-05-12 15:26:13 +05:30
2021-03-25 13:24:08 +05:30
func eventsHandler(w http.ResponseWriter, r *http.Request) {
var reader io.Reader
2021-04-27 16:40:13 +05:30
2021-05-06 16:07:43 +05:30
// for name, values := range r.Header {
// // Loop over all values for the name.
// for _, value := range values {
// fmt.Println("HEADER: ", name, value)
// }
// }
2021-04-27 16:40:13 +05:30
// 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
}
2021-03-25 13:24:08 +05:30
body, err := ioutil.ReadAll(reader)
2021-04-27 16:40:13 +05:30
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
2021-03-25 13:24:08 +05:30
}
w.Header().Set("Content-Type", "application/json")
2021-04-27 16:40:13 +05:30
lib.JsonRequestChannel <- &lib.RequestObject{Body: body, Header: r.Header}
2021-05-26 19:23:56 +05:30
//io.WriteString(w, "ok")
var rsp = CustomResponse{Code: 200, Message: "OK"}
json.NewEncoder(w).Encode(rsp)
2021-03-25 13:24:08 +05:30
}
2021-03-31 10:14:54 +05:30
2021-05-26 19:23:56 +05:30
func healthHandler(w http.ResponseWriter, r *http.Request) {
2021-03-31 10:14:54 +05:30
if healthyBool {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, "true")
} else {
http.Error(w, "server unhealthy", http.StatusServiceUnavailable)
return
}
}
2021-05-26 19:23:56 +05:30
func healthToggleHandler(w http.ResponseWriter, r *http.Request) {
2021-03-31 10:14:54 +05:30
healthyBool = !healthyBool
io.WriteString(w, "toggled")
}
2021-05-06 16:18:54 +05:30
2021-05-26 19:23:56 +05:30
func getSchemaHandler(w http.ResponseWriter, r *http.Request) {
schemaMapJson, _ := json.Marshal(schema.SchemaVersionMap)
2021-05-06 16:18:54 +05:30
w.Header().Set("Content-Type", "application/json")
w.Write(schemaMapJson)
2021-05-07 20:35:35 +05:30
}
2021-05-26 19:23:56 +05:30
func refreshSchemaHandler(w http.ResponseWriter, r *http.Request) {
schema.GetSchemaVersions()
2021-05-07 20:35:35 +05:30
io.WriteString(w, "Updated Schema Map")
}
2021-05-12 15:26:13 +05:30
2021-05-26 19:23:56 +05:30
func addSchemaHandler(w http.ResponseWriter, r *http.Request) {
2021-05-12 15:26:13 +05:30
body, _ := ioutil.ReadAll(r.Body)
var bodyJson NewSchemaRequest
error := json.Unmarshal(body, &bodyJson)
fmt.Println(error)
errorSchema := schema.AddSchema(bodyJson.Topic, bodyJson.SchemaType, bodyJson.Schema)
2021-05-12 15:26:13 +05:30
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")
}
}