Add server configuration management.

This commit is contained in:
2025-05-14 21:50:59 +08:00
parent 703812d3df
commit 5c08ab34ea
16 changed files with 337 additions and 29 deletions

64
server/api/config.go Normal file
View File

@@ -0,0 +1,64 @@
package api
import (
"nysoure/server/config"
"nysoure/server/model"
"nysoure/server/service"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
)
func getServerConfig(c fiber.Ctx) error {
uid, ok := c.Locals("uid").(uint)
if !ok {
return model.NewRequestError("You are not logged in")
}
isAdmin, err := service.CheckUserIsAdmin(uid)
if err != nil {
log.Error("Error checking user admin status: ", err)
return model.NewInternalServerError("Error checking user admin status")
}
if !isAdmin {
return model.NewUnAuthorizedError("You do not have permission to access this resource")
}
sc := config.GetConfig()
return c.JSON(model.Response[config.ServerConfig]{
Success: true,
Data: sc,
})
}
func setServerConfig(c fiber.Ctx) error {
uid, ok := c.Locals("uid").(uint)
if !ok {
return model.NewRequestError("You are not logged in")
}
isAdmin, err := service.CheckUserIsAdmin(uid)
if err != nil {
log.Error("Error checking user admin status: ", err)
return model.NewInternalServerError("Error checking user admin status")
}
if !isAdmin {
return model.NewUnAuthorizedError("You do not have permission to access this resource")
}
var sc config.ServerConfig
if err := c.Bind().Body(&sc); err != nil {
return model.NewRequestError("Invalid request parameters")
}
config.SetConfig(sc)
return c.JSON(model.Response[any]{
Success: true,
})
}
func AddConfigRoutes(r fiber.Router) {
configGroup := r.Group("/config")
{
configGroup.Get("/", getServerConfig)
configGroup.Post("/", setServerConfig)
}
}

View File

@@ -2,11 +2,12 @@ package api
import (
"fmt"
"github.com/gofiber/fiber/v3"
"nysoure/server/model"
"nysoure/server/service"
"strconv"
"strings"
"github.com/gofiber/fiber/v3"
)
func AddFileRoutes(router fiber.Router) {