package web

import (
	"github.com/intel-go/fastjson"
	"github.com/valyala/fasthttp"
	"go.uber.org/zap"

	"tiup-log-collector/log"
)

func otherHandler(ctx *fasthttp.RequestCtx) {
	defer recoverError("otherHandler", ctx)
	renderMsg(ctx, fasthttp.StatusNotFound, "404 not found")
}

func pingHandler(ctx *fasthttp.RequestCtx) {
	defer recoverError("pingHandler", ctx)
	renderMsg(ctx, fasthttp.StatusOK, "pong")
}

func eventHandler(ctx *fasthttp.RequestCtx, app string) {
	defer recoverError("eventRequest", ctx)

	var event interface{}
	err := fastjson.Unmarshal(ctx.PostBody(), &event)
	if err != nil {
		renderMsg(ctx, fasthttp.StatusBadRequest, "Invalid content, required json.")
		return
	}

	log.E().Info(
		"event",
		zap.String("ip", ClientIP(ctx)),
		zap.String("app", app),
		zap.Any("content", event),
	)

	renderMsg(ctx, fasthttp.StatusOK, "success")
}
