Added sqlite database
This commit is contained in:
+77
-71
@@ -1,6 +1,7 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"path/filepath"
|
||||
"quay/app/github"
|
||||
"quay/app/models"
|
||||
@@ -8,78 +9,83 @@ import (
|
||||
"quay/internal/envconfig"
|
||||
"strings"
|
||||
|
||||
"crypto/subtle"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
func NewUpdateSiteHandler(cfg *config.Config, envCfg *envconfig.EnvConfig) fiber.Handler {
|
||||
return func(c fiber.Ctx) error {
|
||||
sitename := c.Query("site")
|
||||
if sitename == "" {
|
||||
return c.Status(400).JSON(models.APIError{
|
||||
Error: "Missing 'site' query parameter",
|
||||
})
|
||||
}
|
||||
|
||||
var siteConfig *config.SiteConfig
|
||||
for _, site := range cfg.Sites {
|
||||
if site.Name == sitename {
|
||||
siteConfig = &site
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if siteConfig == nil {
|
||||
return c.Status(404).JSON(models.APIError{
|
||||
Error: "Site not found",
|
||||
})
|
||||
}
|
||||
|
||||
deployToken := siteConfig.DeployToken
|
||||
if deployToken == "" {
|
||||
return c.Status(500).JSON(models.APIError{
|
||||
Error: "Deploy token not configured for this site",
|
||||
})
|
||||
}
|
||||
|
||||
providedToken := c.Get("Authorization")
|
||||
if strings.HasPrefix(providedToken, "Bearer ") {
|
||||
providedToken = strings.TrimPrefix(providedToken, "Bearer ")
|
||||
}
|
||||
if providedToken == "" {
|
||||
return c.Status(401).JSON(models.APIError{
|
||||
Error: "Missing Authorization header",
|
||||
})
|
||||
}
|
||||
|
||||
if subtle.ConstantTimeCompare([]byte(providedToken), []byte(deployToken)) != 1 {
|
||||
return c.Status(403).JSON(models.APIError{
|
||||
Error: "Invalid deploy token",
|
||||
})
|
||||
}
|
||||
|
||||
sitePath := filepath.Join(envCfg.StoragePath, siteConfig.Name)
|
||||
if _, err := filepath.Abs(sitePath); err != nil {
|
||||
return c.Status(500).JSON(models.APIError{
|
||||
Error: "Failed to resolve site path",
|
||||
})
|
||||
}
|
||||
|
||||
err := github.FetchAndDeployBranch(
|
||||
siteConfig.Owner,
|
||||
siteConfig.Repo,
|
||||
siteConfig.Branch,
|
||||
envCfg.GithubPat,
|
||||
sitePath,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return c.Status(500).JSON(models.APIError{
|
||||
Error: "Failed to deploy site: " + err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.SendStatus(201)
|
||||
}
|
||||
type UpdateSiteHandler struct {
|
||||
Cfg *config.Config
|
||||
EnvCfg *envconfig.EnvConfig
|
||||
}
|
||||
|
||||
func NewUpdateSiteHandler(cfg *config.Config, envCfg *envconfig.EnvConfig) *UpdateSiteHandler {
|
||||
return &UpdateSiteHandler{Cfg: cfg, EnvCfg: envCfg}
|
||||
}
|
||||
|
||||
func (h *UpdateSiteHandler) PostUpdate(c fiber.Ctx) error {
|
||||
sitename := c.Query("site")
|
||||
if sitename == "" {
|
||||
return c.Status(400).JSON(models.APIError{
|
||||
Message: "Missing 'site' query parameter",
|
||||
})
|
||||
}
|
||||
|
||||
var siteConfig *config.SiteConfig
|
||||
for _, site := range h.Cfg.Sites {
|
||||
if site.Name == sitename {
|
||||
siteConfig = &site
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if siteConfig == nil {
|
||||
return c.Status(404).JSON(models.APIError{
|
||||
Message: "Site not found",
|
||||
})
|
||||
}
|
||||
|
||||
deployToken := siteConfig.DeployToken
|
||||
if deployToken == "" {
|
||||
return c.Status(500).JSON(models.APIError{
|
||||
Message: "Deploy token not configured for this site",
|
||||
})
|
||||
}
|
||||
|
||||
providedToken := c.Get("Authorization")
|
||||
if strings.HasPrefix(providedToken, "Bearer ") {
|
||||
providedToken = strings.TrimPrefix(providedToken, "Bearer ")
|
||||
}
|
||||
if providedToken == "" {
|
||||
return c.Status(401).JSON(models.APIError{
|
||||
Message: "Missing Authorization header",
|
||||
})
|
||||
}
|
||||
|
||||
if subtle.ConstantTimeCompare([]byte(providedToken), []byte(deployToken)) != 1 {
|
||||
return c.Status(403).JSON(models.APIError{
|
||||
Message: "Invalid deploy token",
|
||||
})
|
||||
}
|
||||
|
||||
sitePath := filepath.Join(h.EnvCfg.StoragePath, siteConfig.Name)
|
||||
if _, err := filepath.Abs(sitePath); err != nil {
|
||||
return c.Status(500).JSON(models.APIError{
|
||||
Message: "Failed to resolve site path",
|
||||
})
|
||||
}
|
||||
|
||||
err := github.FetchAndDeployBranch(
|
||||
siteConfig.Owner,
|
||||
siteConfig.Repo,
|
||||
siteConfig.Branch,
|
||||
h.EnvCfg.GithubPat,
|
||||
sitePath,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return c.Status(500).JSON(models.APIError{
|
||||
Message: "Failed to deploy site: " + err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.SendStatus(201)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user