website/api/routes/admin.go

155 lines
2.9 KiB
Go
Raw Normal View History

2023-11-12 17:43:23 +03:00
package routes
import (
"fmt"
2023-11-12 17:43:23 +03:00
"time"
"github.com/gofiber/fiber/v2"
2024-10-06 17:30:25 +03:00
"github.com/ngn13/website/api/config"
"github.com/ngn13/website/api/database"
"github.com/ngn13/website/api/status"
2023-11-12 17:43:23 +03:00
"github.com/ngn13/website/api/util"
)
func admin_log(c *fiber.Ctx, m string) error {
return c.Locals("database").(*database.Type).AdminLogAdd(&database.AdminLog{
Action: m, // action that the admin peformed
Time: time.Now().Unix(), // current time
})
}
2023-11-12 17:43:23 +03:00
func AuthMiddleware(c *fiber.Ctx) error {
conf := c.Locals("config").(*config.Type)
2023-11-12 17:43:23 +03:00
if c.Get("Authorization") != conf.GetStr("password") {
return util.ErrAuth(c)
}
2023-11-12 17:43:23 +03:00
return c.Next()
2023-11-12 17:43:23 +03:00
}
func GET_AdminLogs(c *fiber.Ctx) error {
var (
list []database.AdminLog
log database.AdminLog
)
2023-11-12 17:43:23 +03:00
db := c.Locals("database").(*database.Type)
for db.AdminLogNext(&log) {
list = append(list, log)
}
return util.JSON(c, 200, fiber.Map{
"result": list,
})
2023-11-12 17:43:23 +03:00
}
func DEL_DelService(c *fiber.Ctx) error {
var (
name string
err error
)
db := c.Locals("database").(*database.Type)
if name = c.Query("name"); name == "" {
util.ErrBadReq(c)
}
if err = admin_log(c, fmt.Sprintf("Removed service \"%s\"", name)); err != nil {
return util.ErrInternal(c, err)
2024-10-06 17:30:25 +03:00
}
if err = db.ServiceRemove(name); err != nil {
return util.ErrInternal(c, err)
2024-10-06 17:30:25 +03:00
}
return util.JSON(c, 200, nil)
2023-11-12 17:43:23 +03:00
}
2024-10-06 17:30:25 +03:00
func PUT_AddService(c *fiber.Ctx) error {
var (
2024-10-06 17:30:25 +03:00
service database.Service
err error
)
db := c.Locals("database").(*database.Type)
if c.BodyParser(&service) != nil {
return util.ErrBadJSON(c)
}
if !service.IsValid() {
return util.ErrBadReq(c)
}
if err = admin_log(c, fmt.Sprintf("Added service \"%s\"", service.Name)); err != nil {
return util.ErrInternal(c, err)
}
if err = db.ServiceUpdate(&service); err != nil {
return util.ErrInternal(c, err)
}
// force a status check so we can get the status of the new service
c.Locals("status").(*status.Type).Check()
return util.JSON(c, 200, nil)
2023-11-12 17:43:23 +03:00
}
func GET_CheckService(c *fiber.Ctx) error {
c.Locals("status").(*status.Type).Check()
return util.JSON(c, 200, nil)
}
func DEL_DelNews(c *fiber.Ctx) error {
var (
id string
err error
)
db := c.Locals("database").(*database.Type)
2023-11-12 17:43:23 +03:00
2024-10-06 17:30:25 +03:00
if id = c.Query("id"); id == "" {
util.ErrBadReq(c)
}
2023-11-12 17:43:23 +03:00
if err = admin_log(c, fmt.Sprintf("Removed news \"%s\"", id)); err != nil {
return util.ErrInternal(c, err)
2024-10-06 17:30:25 +03:00
}
if err = db.NewsRemove(id); err != nil {
return util.ErrInternal(c, err)
2024-10-06 17:30:25 +03:00
}
return util.JSON(c, 200, nil)
2023-11-12 17:43:23 +03:00
}
func PUT_AddNews(c *fiber.Ctx) error {
var (
news database.News
2024-10-06 17:30:25 +03:00
err error
)
db := c.Locals("database").(*database.Type)
2023-11-12 17:43:23 +03:00
if c.BodyParser(&news) != nil {
return util.ErrBadJSON(c)
}
2023-11-12 17:43:23 +03:00
if !news.IsValid() {
return util.ErrBadReq(c)
}
2023-11-12 17:43:23 +03:00
if err = admin_log(c, fmt.Sprintf("Added news \"%s\"", news.ID)); err != nil {
return util.ErrInternal(c, err)
}
2023-11-12 17:43:23 +03:00
if err = db.NewsAdd(&news); err != nil {
return util.ErrInternal(c, err)
}
2023-11-12 17:43:23 +03:00
return util.JSON(c, 200, nil)
2023-11-12 17:43:23 +03:00
}