Sync config
All checks were successful
release-nightly / build-docker (push) Successful in 1m10s

This commit is contained in:
LittleSheep 2023-12-10 18:55:13 +08:00
parent 7b47004cf7
commit be5ed05175
19 changed files with 89 additions and 25 deletions

View File

@ -29,4 +29,4 @@ As result, roadsign undoubtedly is the fastest one.
It can be found that the prefork feature makes RoadSign more stable in concurrency. We can see this from the **Slowest Time**. At the same time, the **Fastest Time** is affected because reusing ports requires some extra steps to handle load balancing. Enable this feature at your own discretion depending on your use case.
More details can be found at benchmark's [README.md](./test/benchmark/README.md)
More details can be found at benchmark's [README.md](./test/README.md)

View File

@ -1,20 +0,0 @@
{
"name": "Example Site",
"rules": [
{
"host": [
"localhost"
],
"path": [
"/"
]
}
],
"upstreams": [
{
"id": "example",
"name": "Example Upstream",
"uri": "files://./test/benchmark/data"
}
]
}

View File

@ -44,6 +44,7 @@ func InitAdministration() *fiber.App {
webhooks := app.Group("/webhooks").Name("WebHooks")
{
webhooks.Put("/publish/:site/:slug", doPublish)
webhooks.Put("/sync/:slug", doSyncSite)
}
return app

View File

@ -0,0 +1,42 @@
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()
}
sign.App.Sites = lo.Map(sign.App.Sites, func(item *sign.SiteConfig, idx int) *sign.SiteConfig {
if item.ID == id {
return &req
} else {
return item
}
})
return c.SendStatus(fiber.StatusOK)
}

View File

@ -2,18 +2,22 @@ package deploy
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"code.smartsheep.studio/goatworks/roadsign/pkg/cmd/rds/conn"
"code.smartsheep.studio/goatworks/roadsign/pkg/sign"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/mholt/archiver/v4"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)
var DeployCommands = []*cli.Command{
@ -86,9 +90,47 @@ var DeployCommands = []*cli.Command{
return mistake
}
// Well done!
log.Info().Msg("Well done! Your site is successfully published! 🎉")
return nil
},
},
{
Name: "sync",
Aliases: []string{"sc"},
ArgsUsage: "<server> <site> <configuration path>",
Action: func(ctx *cli.Context) error {
if ctx.Args().Len() < 3 {
return fmt.Errorf("must have three arguments: <server> <site> <configuration path>")
}
server, ok := conn.GetConnection(ctx.Args().Get(0))
if !ok {
return fmt.Errorf("server was not found, use \"rds connect\" add one first")
}
var site sign.SiteConfig
if file, err := os.Open(ctx.Args().Get(2)); err != nil {
return err
} else {
raw, _ := io.ReadAll(file)
yaml.Unmarshal(raw, &site)
}
raw, _ := json.Marshal(site)
url := fmt.Sprintf("/webhooks/sync/%s", ctx.Args().Get(1))
client := fiber.Put(server.Url+url).
Body(raw).
BasicAuth("RoadSign CLI", server.Credential)
if status, data, err := client.Bytes(); len(err) > 0 {
return fmt.Errorf("failed to sync to remote: %q", err)
} else if status != 200 {
return fmt.Errorf("server rejected request, status code %d, response %s", status, string(data))
}
log.Info().Msg("Well done! Your site configuration is up-to-date! 🎉")
return nil
},
},

View File

@ -27,9 +27,10 @@ func ReadInConfig(root string) error {
} else if err := yaml.Unmarshal(data, &site); err != nil {
return err
} else {
defer file.Close()
// Extract file name as site id
site.ID = strings.SplitN(filepath.Base(fp), ".", 2)[0]
cfg.Sites = append(cfg.Sites, &site)
}

View File

@ -22,8 +22,6 @@ func (v *AppConfig) Forward(ctx *fiber.Ctx, site *SiteConfig) error {
if err := process.BootProcess(); err != nil {
log.Warn().Err(err).Msgf("An error occurred when booting process (%s) for %s", process.ID, site.ID)
return fiber.ErrBadGateway
} else {
log.Debug().Msg("process is alive!")
}
}