fix
This commit is contained in:
297
internal/logic/controllers/crud.go
Normal file
297
internal/logic/controllers/crud.go
Normal file
@@ -0,0 +1,297 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"git.apinb.com/ops/logs/internal/impl"
|
||||
"git.apinb.com/ops/logs/internal/ingest"
|
||||
"git.apinb.com/ops/logs/internal/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func parseID(ctx *gin.Context) (uint, error) {
|
||||
id64, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint(id64), nil
|
||||
}
|
||||
|
||||
func ListSyslogRules(ctx *gin.Context) {
|
||||
var rows []models.SyslogRule
|
||||
if err := impl.DBService.Order("priority desc, id asc").Find(&rows).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, gin.H{"items": rows})
|
||||
}
|
||||
|
||||
func CreateSyslogRule(ctx *gin.Context) {
|
||||
var row models.SyslogRule
|
||||
if err := ctx.ShouldBindJSON(&row); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
row.ID = 0
|
||||
if err := impl.DBService.Create(&row).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, row)
|
||||
}
|
||||
|
||||
func UpdateSyslogRule(ctx *gin.Context) {
|
||||
id, err := parseID(ctx)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, errors.New("invalid id"))
|
||||
return
|
||||
}
|
||||
var row models.SyslogRule
|
||||
if err := impl.DBService.First(&row, id).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&row); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
row.ID = id
|
||||
if err := impl.DBService.Save(&row).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, row)
|
||||
}
|
||||
|
||||
func DeleteSyslogRule(ctx *gin.Context) {
|
||||
id, err := parseID(ctx)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, errors.New("invalid id"))
|
||||
return
|
||||
}
|
||||
if err := impl.DBService.Delete(&models.SyslogRule{}, id).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, gin.H{"deleted": id})
|
||||
}
|
||||
|
||||
func ListTrapRules(ctx *gin.Context) {
|
||||
var rows []models.TrapRule
|
||||
if err := impl.DBService.Order("priority desc, id asc").Find(&rows).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, gin.H{"items": rows})
|
||||
}
|
||||
|
||||
func CreateTrapRule(ctx *gin.Context) {
|
||||
var row models.TrapRule
|
||||
if err := ctx.ShouldBindJSON(&row); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
row.ID = 0
|
||||
if err := impl.DBService.Create(&row).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, row)
|
||||
}
|
||||
|
||||
func UpdateTrapRule(ctx *gin.Context) {
|
||||
id, err := parseID(ctx)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, errors.New("invalid id"))
|
||||
return
|
||||
}
|
||||
var row models.TrapRule
|
||||
if err := impl.DBService.First(&row, id).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&row); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
row.ID = id
|
||||
if err := impl.DBService.Save(&row).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, row)
|
||||
}
|
||||
|
||||
func DeleteTrapRule(ctx *gin.Context) {
|
||||
id, err := parseID(ctx)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, errors.New("invalid id"))
|
||||
return
|
||||
}
|
||||
if err := impl.DBService.Delete(&models.TrapRule{}, id).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, gin.H{"deleted": id})
|
||||
}
|
||||
|
||||
func ListTrapDictionary(ctx *gin.Context) {
|
||||
var rows []models.TrapDictionaryEntry
|
||||
if err := impl.DBService.Order("id asc").Find(&rows).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, gin.H{"items": rows})
|
||||
}
|
||||
|
||||
func CreateTrapDictionary(ctx *gin.Context) {
|
||||
var row models.TrapDictionaryEntry
|
||||
if err := ctx.ShouldBindJSON(&row); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
row.ID = 0
|
||||
if err := impl.DBService.Create(&row).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, row)
|
||||
}
|
||||
|
||||
func UpdateTrapDictionary(ctx *gin.Context) {
|
||||
id, err := parseID(ctx)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, errors.New("invalid id"))
|
||||
return
|
||||
}
|
||||
var row models.TrapDictionaryEntry
|
||||
if err := impl.DBService.First(&row, id).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&row); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
row.ID = id
|
||||
if err := impl.DBService.Save(&row).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, row)
|
||||
}
|
||||
|
||||
func DeleteTrapDictionary(ctx *gin.Context) {
|
||||
id, err := parseID(ctx)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, errors.New("invalid id"))
|
||||
return
|
||||
}
|
||||
if err := impl.DBService.Delete(&models.TrapDictionaryEntry{}, id).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, gin.H{"deleted": id})
|
||||
}
|
||||
|
||||
func ListTrapShields(ctx *gin.Context) {
|
||||
var rows []models.TrapShield
|
||||
if err := impl.DBService.Order("id asc").Find(&rows).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, gin.H{"items": rows})
|
||||
}
|
||||
|
||||
func CreateTrapShield(ctx *gin.Context) {
|
||||
var row models.TrapShield
|
||||
if err := ctx.ShouldBindJSON(&row); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
row.ID = 0
|
||||
if err := impl.DBService.Create(&row).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, row)
|
||||
}
|
||||
|
||||
func UpdateTrapShield(ctx *gin.Context) {
|
||||
id, err := parseID(ctx)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, errors.New("invalid id"))
|
||||
return
|
||||
}
|
||||
var row models.TrapShield
|
||||
if err := impl.DBService.First(&row, id).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&row); err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
row.ID = id
|
||||
if err := impl.DBService.Save(&row).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, row)
|
||||
}
|
||||
|
||||
func DeleteTrapShield(ctx *gin.Context) {
|
||||
id, err := parseID(ctx)
|
||||
if err != nil {
|
||||
infra.Response.Error(ctx, errors.New("invalid id"))
|
||||
return
|
||||
}
|
||||
if err := impl.DBService.Delete(&models.TrapShield{}, id).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
_ = ingest.Global.Refresh()
|
||||
infra.Response.Success(ctx, gin.H{"deleted": id})
|
||||
}
|
||||
|
||||
func ListLogEvents(ctx *gin.Context) {
|
||||
kind := ctx.Query("source_kind")
|
||||
page, _ := strconv.Atoi(ctx.DefaultQuery("page", "1"))
|
||||
size, _ := strconv.Atoi(ctx.DefaultQuery("page_size", "50"))
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if size < 1 || size > 500 {
|
||||
size = 50
|
||||
}
|
||||
offset := (page - 1) * size
|
||||
q := impl.DBService.Model(&models.LogEvent{})
|
||||
if kind != "" {
|
||||
q = q.Where("source_kind = ?", kind)
|
||||
}
|
||||
var total int64
|
||||
_ = q.Count(&total).Error
|
||||
var rows []models.LogEvent
|
||||
if err := q.Order("id desc").Offset(offset).Limit(size).Find(&rows).Error; err != nil {
|
||||
infra.Response.Error(ctx, err)
|
||||
return
|
||||
}
|
||||
infra.Response.Success(ctx, gin.H{"total": total, "page": page, "page_size": size, "items": rows})
|
||||
}
|
||||
10
internal/logic/ping/ping.go
Normal file
10
internal/logic/ping/ping.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package ping
|
||||
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/infra"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Hello(ctx *gin.Context) {
|
||||
infra.Response.Success(ctx, gin.H{"service": "logs", "status": "ok"})
|
||||
}
|
||||
Reference in New Issue
Block a user