mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
Add server configuration management.
This commit is contained in:
64
server/api/config.go
Normal file
64
server/api/config.go
Normal 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)
|
||||
}
|
||||
}
|
@@ -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) {
|
||||
|
Reference in New Issue
Block a user