log management
parent
7805844697
commit
7bae66c3aa
|
@ -7,7 +7,9 @@ import (
|
||||||
"jannex/log-manager/modules/structs"
|
"jannex/log-manager/modules/structs"
|
||||||
"jannex/log-manager/modules/utils"
|
"jannex/log-manager/modules/utils"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -73,8 +75,16 @@ func GetAvailableLogFiles(logType string) ([]string, error) {
|
||||||
return []string{}, err
|
return []string{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sort the files by date
|
||||||
|
sort.Slice(files, func(i, j int) bool {
|
||||||
|
dateFormat := "2-1-2006"
|
||||||
|
dateA, _ := time.Parse(dateFormat, strings.Split(files[i].Name(), ".")[0])
|
||||||
|
dateB, _ := time.Parse(dateFormat, strings.Split(files[j].Name(), ".")[0])
|
||||||
|
return dateA.After(dateB)
|
||||||
|
})
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
availableLogs = append(availableLogs, file.Name())
|
availableLogs = append(availableLogs, strings.Split(file.Name(), ".")[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
return availableLogs, nil
|
return availableLogs, nil
|
||||||
|
@ -111,3 +121,20 @@ func GetLogByDate(logType string, date string) ([]string, error) {
|
||||||
|
|
||||||
return logs, nil
|
return logs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAvailableLogTypes() []string {
|
||||||
|
var availableLogTypes []string
|
||||||
|
|
||||||
|
files, err := os.ReadDir(config.Cfg.LogFolder)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
availableLogTypes = append(availableLogTypes, file.Name())
|
||||||
|
}
|
||||||
|
|
||||||
|
return availableLogTypes
|
||||||
|
}
|
||||||
|
|
|
@ -12,14 +12,10 @@ import (
|
||||||
func AddLog(c *fiber.Ctx) error {
|
func AddLog(c *fiber.Ctx) error {
|
||||||
var body structs.LogBody
|
var body structs.LogBody
|
||||||
|
|
||||||
fmt.Println("c", string(c.Body()))
|
|
||||||
|
|
||||||
if err := utils.BodyParserHelper(c, &body); err != nil {
|
if err := utils.BodyParserHelper(c, &body); err != nil {
|
||||||
return c.SendStatus(fiber.StatusBadRequest)
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("body", body)
|
|
||||||
|
|
||||||
loghandler.AddLog(body)
|
loghandler.AddLog(body)
|
||||||
|
|
||||||
return c.SendStatus(fiber.StatusOK)
|
return c.SendStatus(fiber.StatusOK)
|
||||||
|
@ -65,3 +61,9 @@ func GetLog(c *fiber.Ctx) error {
|
||||||
return c.JSON(logs)
|
return c.JSON(logs)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetAvailableLogTypes(c *fiber.Ctx) error {
|
||||||
|
logTypes := loghandler.GetAvailableLogTypes()
|
||||||
|
|
||||||
|
return c.JSON(logTypes)
|
||||||
|
}
|
||||||
|
|
|
@ -11,5 +11,8 @@ func SetupRoutes(app *fiber.App) {
|
||||||
|
|
||||||
l := v1.Group("/log")
|
l := v1.Group("/log")
|
||||||
l.Post("/", log.AddLog)
|
l.Post("/", log.AddLog)
|
||||||
l.Get("/:type", log.GetLog)
|
l.Get("/types", log.GetAvailableLogTypes)
|
||||||
|
|
||||||
|
ls := v1.Group("/logs")
|
||||||
|
ls.Get("/:type", log.GetLog)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue