From be5ed05175daec0df8d2d022d3de812155c11105 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 10 Dec 2023 18:55:13 +0800 Subject: [PATCH] :sparkles: Sync config --- README.md | 2 +- config/example.json | 20 --------- pkg/administration/server.go | 1 + pkg/administration/sites.go | 42 ++++++++++++++++++ pkg/cmd/rds/deploy/commands.go | 44 ++++++++++++++++++- pkg/sign/configurator.go | 3 +- pkg/sign/router.go | 2 - test/{benchmark => }/README.md | 0 test/{benchmark => }/data/.gitignore | 0 test/{benchmark => }/data/index.html | 0 test/{benchmark => }/nginx/nginx.conf | 0 .../roadsign-spa/config/example.yaml | 0 .../{benchmark => }/roadsign-spa/settings.yml | 0 .../roadsign-ssr/config/example.yaml | 0 .../{benchmark => }/roadsign-ssr/settings.yml | 0 .../roadsign-with-prefork/config/example.yaml | 0 .../roadsign-with-prefork/settings.yml | 0 .../roadsign/config/example.yaml | 0 test/{benchmark => }/roadsign/settings.yml | 0 19 files changed, 89 insertions(+), 25 deletions(-) delete mode 100644 config/example.json create mode 100644 pkg/administration/sites.go rename test/{benchmark => }/README.md (100%) rename test/{benchmark => }/data/.gitignore (100%) rename test/{benchmark => }/data/index.html (100%) rename test/{benchmark => }/nginx/nginx.conf (100%) rename test/{benchmark => }/roadsign-spa/config/example.yaml (100%) rename test/{benchmark => }/roadsign-spa/settings.yml (100%) rename test/{benchmark => }/roadsign-ssr/config/example.yaml (100%) rename test/{benchmark => }/roadsign-ssr/settings.yml (100%) rename test/{benchmark => }/roadsign-with-prefork/config/example.yaml (100%) rename test/{benchmark => }/roadsign-with-prefork/settings.yml (100%) rename test/{benchmark => }/roadsign/config/example.yaml (100%) rename test/{benchmark => }/roadsign/settings.yml (100%) diff --git a/README.md b/README.md index 46eb18b..3102567 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file +More details can be found at benchmark's [README.md](./test/README.md) \ No newline at end of file diff --git a/config/example.json b/config/example.json deleted file mode 100644 index 89fa133..0000000 --- a/config/example.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "name": "Example Site", - "rules": [ - { - "host": [ - "localhost" - ], - "path": [ - "/" - ] - } - ], - "upstreams": [ - { - "id": "example", - "name": "Example Upstream", - "uri": "files://./test/benchmark/data" - } - ] -} \ No newline at end of file diff --git a/pkg/administration/server.go b/pkg/administration/server.go index 9488364..8184bf8 100644 --- a/pkg/administration/server.go +++ b/pkg/administration/server.go @@ -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 diff --git a/pkg/administration/sites.go b/pkg/administration/sites.go new file mode 100644 index 0000000..42231a1 --- /dev/null +++ b/pkg/administration/sites.go @@ -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) +} diff --git a/pkg/cmd/rds/deploy/commands.go b/pkg/cmd/rds/deploy/commands.go index 46c1202..84507af 100644 --- a/pkg/cmd/rds/deploy/commands.go +++ b/pkg/cmd/rds/deploy/commands.go @@ -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: " ", + Action: func(ctx *cli.Context) error { + if ctx.Args().Len() < 3 { + return fmt.Errorf("must have three arguments: ") + } + + 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 }, }, diff --git a/pkg/sign/configurator.go b/pkg/sign/configurator.go index 671d22c..d49dc3f 100644 --- a/pkg/sign/configurator.go +++ b/pkg/sign/configurator.go @@ -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) } diff --git a/pkg/sign/router.go b/pkg/sign/router.go index 0336aaf..e23794d 100644 --- a/pkg/sign/router.go +++ b/pkg/sign/router.go @@ -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!") } } diff --git a/test/benchmark/README.md b/test/README.md similarity index 100% rename from test/benchmark/README.md rename to test/README.md diff --git a/test/benchmark/data/.gitignore b/test/data/.gitignore similarity index 100% rename from test/benchmark/data/.gitignore rename to test/data/.gitignore diff --git a/test/benchmark/data/index.html b/test/data/index.html similarity index 100% rename from test/benchmark/data/index.html rename to test/data/index.html diff --git a/test/benchmark/nginx/nginx.conf b/test/nginx/nginx.conf similarity index 100% rename from test/benchmark/nginx/nginx.conf rename to test/nginx/nginx.conf diff --git a/test/benchmark/roadsign-spa/config/example.yaml b/test/roadsign-spa/config/example.yaml similarity index 100% rename from test/benchmark/roadsign-spa/config/example.yaml rename to test/roadsign-spa/config/example.yaml diff --git a/test/benchmark/roadsign-spa/settings.yml b/test/roadsign-spa/settings.yml similarity index 100% rename from test/benchmark/roadsign-spa/settings.yml rename to test/roadsign-spa/settings.yml diff --git a/test/benchmark/roadsign-ssr/config/example.yaml b/test/roadsign-ssr/config/example.yaml similarity index 100% rename from test/benchmark/roadsign-ssr/config/example.yaml rename to test/roadsign-ssr/config/example.yaml diff --git a/test/benchmark/roadsign-ssr/settings.yml b/test/roadsign-ssr/settings.yml similarity index 100% rename from test/benchmark/roadsign-ssr/settings.yml rename to test/roadsign-ssr/settings.yml diff --git a/test/benchmark/roadsign-with-prefork/config/example.yaml b/test/roadsign-with-prefork/config/example.yaml similarity index 100% rename from test/benchmark/roadsign-with-prefork/config/example.yaml rename to test/roadsign-with-prefork/config/example.yaml diff --git a/test/benchmark/roadsign-with-prefork/settings.yml b/test/roadsign-with-prefork/settings.yml similarity index 100% rename from test/benchmark/roadsign-with-prefork/settings.yml rename to test/roadsign-with-prefork/settings.yml diff --git a/test/benchmark/roadsign/config/example.yaml b/test/roadsign/config/example.yaml similarity index 100% rename from test/benchmark/roadsign/config/example.yaml rename to test/roadsign/config/example.yaml diff --git a/test/benchmark/roadsign/settings.yml b/test/roadsign/settings.yml similarity index 100% rename from test/benchmark/roadsign/settings.yml rename to test/roadsign/settings.yml