2023-12-10 10:55:13 +00:00
|
|
|
package administration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"code.smartsheep.studio/goatworks/roadsign/pkg/sign"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func doSyncSite(c *fiber.Ctx) error {
|
|
|
|
var req sign.SiteConfig
|
|
|
|
|
|
|
|
if err := c.BodyParser(&req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id := c.Params("slug")
|
|
|
|
path := filepath.Join(viper.GetString("paths.configs"), fmt.Sprintf("%s.yaml", id))
|
|
|
|
|
|
|
|
if file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755); err != nil {
|
|
|
|
return fiber.NewError(fiber.ErrInternalServerError.Code, err.Error())
|
|
|
|
} else {
|
|
|
|
raw, _ := yaml.Marshal(req)
|
|
|
|
file.Write(raw)
|
|
|
|
defer file.Close()
|
|
|
|
}
|
|
|
|
|
2023-12-10 11:19:40 +00:00
|
|
|
pushed := false
|
2023-12-10 10:55:13 +00:00
|
|
|
sign.App.Sites = lo.Map(sign.App.Sites, func(item *sign.SiteConfig, idx int) *sign.SiteConfig {
|
|
|
|
if item.ID == id {
|
2023-12-10 11:19:40 +00:00
|
|
|
pushed = true
|
2023-12-10 10:55:13 +00:00
|
|
|
return &req
|
|
|
|
} else {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-12-10 11:19:40 +00:00
|
|
|
if !pushed {
|
|
|
|
sign.App.Sites = append(sign.App.Sites, &req)
|
|
|
|
}
|
|
|
|
|
2023-12-10 10:55:13 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|