2023-12-13 11:52:56 +00:00
|
|
|
package sideload
|
2023-11-17 16:23:40 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-30 11:21:03 +00:00
|
|
|
"fmt"
|
2024-09-29 15:03:11 +00:00
|
|
|
roadsign "git.solsynth.dev/goatworks/roadsign/pkg"
|
2023-11-17 16:23:40 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2023-11-24 16:34:51 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/basicauth"
|
2023-11-25 15:06:23 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
2024-09-29 15:51:05 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2023-11-25 15:06:23 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-11-17 16:23:40 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2023-12-13 11:52:56 +00:00
|
|
|
func InitSideload() *fiber.App {
|
2023-11-17 16:23:40 +00:00
|
|
|
app := fiber.New(fiber.Config{
|
2023-12-13 11:52:56 +00:00
|
|
|
AppName: "RoadSign Sideload",
|
2023-12-16 04:03:25 +00:00
|
|
|
ServerHeader: "RoadSign Sideload",
|
2023-11-17 16:23:40 +00:00
|
|
|
DisableStartupMessage: true,
|
|
|
|
EnableIPValidation: true,
|
2023-12-16 04:35:42 +00:00
|
|
|
JSONDecoder: jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal,
|
|
|
|
JSONEncoder: jsoniter.ConfigCompatibleWithStandardLibrary.Marshal,
|
2024-02-12 04:40:54 +00:00
|
|
|
ProxyHeader: fiber.HeaderXForwardedFor,
|
2023-11-18 06:30:35 +00:00
|
|
|
EnablePrintRoutes: viper.GetBool("debug.print_routes"),
|
2024-09-29 15:51:05 +00:00
|
|
|
TrustedProxies: viper.GetStringSlice("sideload.trusted_proxies"),
|
2023-12-10 10:15:22 +00:00
|
|
|
BodyLimit: viper.GetInt("hypertext.limitation.max_body_size"),
|
2023-11-17 16:23:40 +00:00
|
|
|
})
|
|
|
|
|
2024-01-26 07:31:19 +00:00
|
|
|
if viper.GetBool("telemetry.request_logging") {
|
2023-11-30 14:35:40 +00:00
|
|
|
app.Use(logger.New(logger.Config{
|
|
|
|
Output: log.Logger,
|
2023-12-13 11:52:56 +00:00
|
|
|
Format: "[Sideload] [${time}] ${status} - ${latency} ${method} ${path}\n",
|
2023-11-30 14:35:40 +00:00
|
|
|
}))
|
|
|
|
}
|
2023-11-25 15:06:23 +00:00
|
|
|
|
2023-11-24 16:34:51 +00:00
|
|
|
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")
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
2023-12-10 04:30:34 +00:00
|
|
|
cgi := app.Group("/cgi").Name("CGI")
|
|
|
|
{
|
2024-01-24 16:09:39 +00:00
|
|
|
cgi.Get("/metadata", getMetadata)
|
2024-01-31 05:17:52 +00:00
|
|
|
cgi.Get("/traffic", getTraffic)
|
2024-01-25 06:46:43 +00:00
|
|
|
cgi.Get("/traces", getTraces)
|
2024-01-25 16:11:44 +00:00
|
|
|
cgi.Get("/stats", getStats)
|
|
|
|
cgi.Get("/regions", getRegions)
|
|
|
|
cgi.Get("/regions/cfg/:id", getRegionConfig)
|
|
|
|
cgi.Get("/applications", getApplications)
|
2024-09-29 15:51:05 +00:00
|
|
|
cgi.Get("/applications/:id/logs", getApplicationLogs)
|
2024-10-02 03:18:36 +00:00
|
|
|
cgi.Post("/applications/:id/start", letApplicationStart)
|
|
|
|
cgi.Post("/applications/:id/stop", letApplicationStop)
|
|
|
|
cgi.Post("/applications/:id/restart", letApplicationRestart)
|
2024-01-31 05:46:12 +00:00
|
|
|
|
|
|
|
cgi.Post("/reload", doReload)
|
2023-12-10 04:30:34 +00:00
|
|
|
}
|
|
|
|
|
2023-11-18 06:30:35 +00:00
|
|
|
webhooks := app.Group("/webhooks").Name("WebHooks")
|
|
|
|
{
|
2023-12-10 10:15:22 +00:00
|
|
|
webhooks.Put("/publish/:site/:slug", doPublish)
|
2024-01-24 16:09:39 +00:00
|
|
|
webhooks.Put("/sync/:slug", doSync)
|
2023-11-18 06:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:23:40 +00:00
|
|
|
return app
|
|
|
|
}
|