Files
janus/server/handlers.go

104 lines
2.9 KiB
Go
Raw Permalink Normal View History

2021-03-25 13:24:08 +05:30
package server
import (
2021-04-27 16:40:13 +05:30
"compress/gzip"
2021-05-06 16:18:54 +05:30
"encoding/json"
"io"
2021-05-26 19:23:56 +05:30
"io/ioutil"
"net/http"
2024-03-06 18:45:34 +05:30
"strings"
"time"
metrics "com.navi.medici.janus/instrumentation"
"com.navi.medici.janus/lib"
"com.navi.medici.janus/utils"
"go.uber.org/zap"
2021-03-25 13:24:08 +05:30
)
2021-03-31 10:14:54 +05:30
var (
logger *zap.Logger
2021-03-31 10:14:54 +05:30
healthyBool bool = true
)
const (
JSON = "json"
SUCCESS = "success"
ERROR = "error"
)
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
func init() {
logger = utils.GetLogger()
}
func eventsHandlerJson(workerPool *lib.WorkerPool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
eventHandlerStartTime := utils.NanosToMillis(time.Now().UnixNano())
headerName := "X-Correlation-Id"
substring := "abcdabcd-1234"
headerValue := r.Header.Get(headerName)
if strings.HasPrefix(headerValue, substring) {
w.Header().Set("Content-Type", "application/json")
rsp := CustomResponse{Code: 200, Message: "OK"}
json.NewEncoder(w).Encode(rsp)
logger.Info("Dropped blacklisted request", zap.String("correlationID", headerValue))
return
}
var reader io.Reader
switch r.Header.Get("Content-Encoding") {
case "gzip":
var err error
reader, err = gzip.NewReader(r.Body)
if err != nil {
logger.Error("Error decompressing GZIP payload", zap.Error(err))
w.Header().Set("Content-Type", "application/json")
http.Error(w, "Error decompressing GZIP payload", http.StatusBadRequest)
metrics.EventHandlerTimeHist.WithLabelValues(JSON, ERROR).Observe(float64(utils.NanosToMillis(time.Now().UnixNano()) - eventHandlerStartTime))
return
}
defer reader.(*gzip.Reader).Close()
default:
reader = r.Body
}
body, err := ioutil.ReadAll(reader) // todo Limit to 1MB
2021-04-27 16:40:13 +05:30
if err != nil {
logger.Error("Error reading request body",
zap.Error(err),
zap.String("client_addr", utils.GetClientIP(r.Header.Get("X-Forwarded-For"), r.RemoteAddr)),
zap.String("user_agent", r.UserAgent()),
zap.Int64("content_length", r.ContentLength),
)
http.Error(w, "Request body invalid", http.StatusBadRequest)
metrics.EventHandlerTimeHist.WithLabelValues(JSON, ERROR).Observe(float64(utils.NanosToMillis(time.Now().UnixNano()) - eventHandlerStartTime))
2021-04-27 16:40:13 +05:30
return
}
w.Header().Set("Content-Type", "application/json")
workerPool.AddJob(lib.RequestObject{Body: body, Header: r.Header})
rsp := CustomResponse{Code: 200, Message: "OK"}
if err := json.NewEncoder(w).Encode(rsp); err != nil {
logger.Error("Error encoding response", zap.Error(err))
}
metrics.EventHandlerTimeHist.WithLabelValues(JSON, SUCCESS).Observe(float64(utils.NanosToMillis(time.Now().UnixNano()) - eventHandlerStartTime))
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, `{"status":"healthy"}`)
2021-03-31 10:14:54 +05:30
} else {
http.Error(w, `{"status":"unhealthy"}`, http.StatusServiceUnavailable)
2021-03-31 10:14:54 +05:30
}
}