🚚 Rename management APIs
This commit is contained in:
13
pkg/sideload/connectivity.go
Normal file
13
pkg/sideload/connectivity.go
Normal file
@ -0,0 +1,13 @@
|
||||
package sideload
|
||||
|
||||
import (
|
||||
roadsign "code.smartsheep.studio/goatworks/roadsign/pkg"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func responseConnectivity(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||
"server": "RoadSign",
|
||||
"version": roadsign.AppVersion,
|
||||
})
|
||||
}
|
85
pkg/sideload/publish.go
Normal file
85
pkg/sideload/publish.go
Normal file
@ -0,0 +1,85 @@
|
||||
package sideload
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"code.smartsheep.studio/goatworks/roadsign/pkg/sign"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/samber/lo"
|
||||
"github.com/saracen/fastzip"
|
||||
)
|
||||
|
||||
func doPublish(c *fiber.Ctx) error {
|
||||
var workdir string
|
||||
var site *sign.SiteConfig
|
||||
var upstream *sign.UpstreamConfig
|
||||
var process *sign.ProcessConfig
|
||||
for _, item := range sign.App.Sites {
|
||||
if item.ID == c.Params("site") {
|
||||
site = item
|
||||
for _, stream := range item.Upstreams {
|
||||
if stream.ID == c.Params("slug") {
|
||||
upstream = stream
|
||||
workdir, _ = stream.GetRawURI()
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, proc := range item.Processes {
|
||||
if proc.ID == c.Params("slug") {
|
||||
process = proc
|
||||
workdir = proc.Workdir
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if upstream == nil && process == nil {
|
||||
return fiber.ErrNotFound
|
||||
} else if upstream != nil && upstream.GetType() != sign.UpstreamTypeFile {
|
||||
return fiber.ErrUnprocessableEntity
|
||||
}
|
||||
|
||||
for _, process := range site.Processes {
|
||||
process.StopProcess()
|
||||
}
|
||||
|
||||
if c.Query("overwrite", "yes") == "yes" {
|
||||
files, _ := filepath.Glob(filepath.Join(workdir, "*"))
|
||||
for _, file := range files {
|
||||
_ = os.Remove(file)
|
||||
}
|
||||
}
|
||||
|
||||
if form, err := c.MultipartForm(); err == nil {
|
||||
files := form.File["attachments"]
|
||||
for _, file := range files {
|
||||
mimetype := lo.Ternary(len(c.Query("mimetype")) > 0, c.Query("mimetype"), file.Header["Content-Type"][0])
|
||||
switch mimetype {
|
||||
case "application/zip":
|
||||
dst := filepath.Join(os.TempDir(), uuid.NewString()+".zip")
|
||||
if err := c.SaveFile(file, dst); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if ex, err := fastzip.NewExtractor(dst, workdir); err != nil {
|
||||
return err
|
||||
} else if err = ex.Extract(context.Background()); err != nil {
|
||||
defer ex.Close()
|
||||
return err
|
||||
}
|
||||
}
|
||||
default:
|
||||
dst := filepath.Join(workdir, file.Filename)
|
||||
if err := c.SaveFile(file, dst); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
51
pkg/sideload/server.go
Normal file
51
pkg/sideload/server.go
Normal file
@ -0,0 +1,51 @@
|
||||
package sideload
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
roadsign "code.smartsheep.studio/goatworks/roadsign/pkg"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/basicauth"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func InitSideload() *fiber.App {
|
||||
app := fiber.New(fiber.Config{
|
||||
AppName: "RoadSign Sideload",
|
||||
ServerHeader: fmt.Sprintf("RoadSign Sideload v%s", roadsign.AppVersion),
|
||||
DisableStartupMessage: true,
|
||||
EnableIPValidation: true,
|
||||
EnablePrintRoutes: viper.GetBool("debug.print_routes"),
|
||||
TrustedProxies: viper.GetStringSlice("security.sideload_trusted_proxies"),
|
||||
BodyLimit: viper.GetInt("hypertext.limitation.max_body_size"),
|
||||
})
|
||||
|
||||
if viper.GetBool("performance.request_logging") {
|
||||
app.Use(logger.New(logger.Config{
|
||||
Output: log.Logger,
|
||||
Format: "[Sideload] [${time}] ${status} - ${latency} ${method} ${path}\n",
|
||||
}))
|
||||
}
|
||||
|
||||
app.Use(basicauth.New(basicauth.Config{
|
||||
Realm: fmt.Sprintf("RoadSign v%s", roadsign.AppVersion),
|
||||
Authorizer: func(_, password string) bool {
|
||||
return password == viper.GetString("security.credential")
|
||||
},
|
||||
}))
|
||||
|
||||
cgi := app.Group("/cgi").Name("CGI")
|
||||
{
|
||||
cgi.All("/connectivity", responseConnectivity)
|
||||
}
|
||||
|
||||
webhooks := app.Group("/webhooks").Name("WebHooks")
|
||||
{
|
||||
webhooks.Put("/publish/:site/:slug", doPublish)
|
||||
webhooks.Put("/sync/:slug", doSyncSite)
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
45
pkg/sideload/sites.go
Normal file
45
pkg/sideload/sites.go
Normal file
@ -0,0 +1,45 @@
|
||||
package sideload
|
||||
|
||||
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()
|
||||
}
|
||||
if site, ok := lo.Find(sign.App.Sites, func(item *sign.SiteConfig) bool {
|
||||
return item.ID == id
|
||||
}); ok {
|
||||
for _, process := range site.Processes {
|
||||
process.StopProcess()
|
||||
}
|
||||
}
|
||||
|
||||
// Reload
|
||||
sign.ReadInConfig(viper.GetString("paths.configs"))
|
||||
sign.App.PreheatProcesses()
|
||||
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
Reference in New Issue
Block a user