Updated update route to use db sites
This commit is contained in:
+31
-22
@@ -1,49 +1,58 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/subtle"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"quay/app/github"
|
"quay/app/github"
|
||||||
"quay/app/models"
|
"quay/app/models"
|
||||||
"quay/internal/config"
|
"quay/app/repository"
|
||||||
"quay/internal/envconfig"
|
"quay/internal/envconfig"
|
||||||
|
"quay/internal/security"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v3"
|
"github.com/gofiber/fiber/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UpdateSiteHandler struct {
|
type UpdateSiteHandler struct {
|
||||||
Cfg *config.Config
|
EnvCfg *envconfig.EnvConfig
|
||||||
EnvCfg *envconfig.EnvConfig
|
SiteRepo repository.SiteRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUpdateSiteHandler(cfg *config.Config, envCfg *envconfig.EnvConfig) *UpdateSiteHandler {
|
func NewUpdateSiteHandler(envCfg *envconfig.EnvConfig, siteRepo repository.SiteRepository) *UpdateSiteHandler {
|
||||||
return &UpdateSiteHandler{Cfg: cfg, EnvCfg: envCfg}
|
return &UpdateSiteHandler{EnvCfg: envCfg, SiteRepo: siteRepo}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *UpdateSiteHandler) PostUpdate(c fiber.Ctx) error {
|
func (h *UpdateSiteHandler) PostUpdate(c fiber.Ctx) error {
|
||||||
sitename := c.Query("site")
|
siteId := c.Query("site")
|
||||||
if sitename == "" {
|
if siteId == "" {
|
||||||
return c.Status(400).JSON(models.APIError{
|
return c.Status(400).JSON(models.APIError{
|
||||||
Message: "Missing 'site' query parameter",
|
Message: "Missing 'site' query parameter",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var siteConfig *config.SiteConfig
|
site, err := h.SiteRepo.GetSite(siteId)
|
||||||
for _, site := range h.Cfg.Sites {
|
|
||||||
if site.Name == sitename {
|
if err != nil {
|
||||||
siteConfig = &site
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
break
|
return c.Status(fiber.StatusNotFound).JSON(&models.APIError{
|
||||||
|
Message: "Site not found",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
log.Println("Message getting site: ", err)
|
||||||
|
return c.Status(fiber.StatusInternalServerError).JSON(&models.APIError{
|
||||||
|
Message: "Unexpected error while getting site",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if siteConfig == nil {
|
if site == nil {
|
||||||
return c.Status(404).JSON(models.APIError{
|
return c.Status(fiber.StatusNotFound).JSON(&models.APIError{
|
||||||
Message: "Site not found",
|
Message: "Site not found",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
deployToken := siteConfig.DeployToken
|
deployToken := site.DeployToken
|
||||||
if deployToken == "" {
|
if deployToken == "" {
|
||||||
return c.Status(500).JSON(models.APIError{
|
return c.Status(500).JSON(models.APIError{
|
||||||
Message: "Deploy token not configured for this site",
|
Message: "Deploy token not configured for this site",
|
||||||
@@ -60,23 +69,23 @@ func (h *UpdateSiteHandler) PostUpdate(c fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if subtle.ConstantTimeCompare([]byte(providedToken), []byte(deployToken)) != 1 {
|
if security.CompareDeployTokens(providedToken, deployToken) == false {
|
||||||
return c.Status(403).JSON(models.APIError{
|
return c.Status(403).JSON(models.APIError{
|
||||||
Message: "Invalid deploy token",
|
Message: "Invalid deploy token",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
sitePath := filepath.Join(h.EnvCfg.StoragePath, siteConfig.Name)
|
sitePath := filepath.Join(h.EnvCfg.StoragePath, site.ID)
|
||||||
if _, err := filepath.Abs(sitePath); err != nil {
|
if _, err := filepath.Abs(sitePath); err != nil {
|
||||||
return c.Status(500).JSON(models.APIError{
|
return c.Status(500).JSON(models.APIError{
|
||||||
Message: "Failed to resolve site path",
|
Message: "Failed to resolve site path",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
err := github.FetchAndDeployBranch(
|
err = github.FetchAndDeployBranch(
|
||||||
siteConfig.Owner,
|
site.Owner,
|
||||||
siteConfig.Repo,
|
site.Repository,
|
||||||
siteConfig.Branch,
|
site.Branch,
|
||||||
h.EnvCfg.GithubPat,
|
h.EnvCfg.GithubPat,
|
||||||
sitePath,
|
sitePath,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
func Register(app *fiber.App, cfg *config.Config, envCfg *envconfig.EnvConfig, db *sql.DB) {
|
func Register(app *fiber.App, cfg *config.Config, envCfg *envconfig.EnvConfig, db *sql.DB) {
|
||||||
siteRepository := cachedrepo.NewCachedSiteRepository(database.NewSQLiteSiteRepository(db))
|
siteRepository := cachedrepo.NewCachedSiteRepository(database.NewSQLiteSiteRepository(db))
|
||||||
|
|
||||||
updateSiteHandler := handlers.NewUpdateSiteHandler(cfg, envCfg)
|
updateSiteHandler := handlers.NewUpdateSiteHandler(envCfg, siteRepository)
|
||||||
siteHandler := handlers.NewSiteHandler(siteRepository)
|
siteHandler := handlers.NewSiteHandler(siteRepository)
|
||||||
|
|
||||||
api := app.Group("/api/v1", middleware.APIHostGuard(envCfg.DashboardHost))
|
api := app.Group("/api/v1", middleware.APIHostGuard(envCfg.DashboardHost))
|
||||||
|
|||||||
Reference in New Issue
Block a user