Files
2018-03-15 09:22:43 +11:00

51 lines
1.1 KiB
Go

package controllers
import (
"net/http"
"github.com/wahyd4/next-dashboard/models"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/sirupsen/logrus"
"github.com/wahyd4/next-dashboard/requests"
)
type WebHookController struct {
DB *gorm.DB
}
func (controller *WebHookController) PostData(c *gin.Context) {
eventConfig := models.GlobalConfig.Event
if !checkParam(eventConfig, c.Param("name")) {
logrus.Warn("Request body is malformed")
c.JSON(http.StatusNotFound, gin.H{"msg": "webhook is invalid"})
return
}
var requestBody requests.WebhookRequest
if err := c.BindJSON(&requestBody); err != nil {
logrus.Warn("Request body is malformed")
c.JSON(http.StatusBadRequest, gin.H{"msg": "bad request"})
return
}
controller.DB.Create(&models.WebHookData{
Name: c.Param("name"),
Type: requestBody.Type,
Body: requestBody.Body,
Title: requestBody.Title,
})
c.Status(http.StatusOK)
}
func checkParam(eventConfig *models.EventConfig, target string) bool {
for _, check := range eventConfig.Checks {
if check.Name == target {
return true
}
}
return false
}