refactor: consolidate maintenance mode endpoints and enhance site status handling

This commit is contained in:
Jacky 2025-04-08 02:53:21 +00:00
parent a2d5f005b0
commit df304b41bc
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
29 changed files with 2102 additions and 1816 deletions

View file

@ -23,7 +23,5 @@ func InitRouter(r *gin.RouterGroup) {
// duplicate site
r.POST("sites/:name/duplicate", DuplicateSite)
// enable maintenance mode for site
r.POST("sites/:name/maintenance/enable", EnableMaintenanceSite)
// disable maintenance mode for site
r.POST("sites/:name/maintenance/disable", DisableMaintenanceSite)
r.POST("sites/:name/maintenance", EnableMaintenanceSite)
}

View file

@ -155,7 +155,21 @@ func RenameSite(c *gin.Context) {
}
func EnableSite(c *gin.Context) {
err := site.Enable(c.Param("name"))
name := c.Param("name")
// Check if the site is in maintenance mode, if yes, disable maintenance mode first
maintenanceConfigPath := nginx.GetConfPath("sites-enabled", name+site.MaintenanceSuffix)
if _, err := os.Stat(maintenanceConfigPath); err == nil {
// Site is in maintenance mode, disable it first
err := site.DisableMaintenance(name)
if err != nil {
cosy.ErrHandler(c, err)
return
}
}
// Then enable the site normally
err := site.Enable(name)
if err != nil {
cosy.ErrHandler(c, err)
return
@ -167,7 +181,21 @@ func EnableSite(c *gin.Context) {
}
func DisableSite(c *gin.Context) {
err := site.Disable(c.Param("name"))
name := c.Param("name")
// Check if the site is in maintenance mode, if yes, disable maintenance mode first
maintenanceConfigPath := nginx.GetConfPath("sites-enabled", name+site.MaintenanceSuffix)
if _, err := os.Stat(maintenanceConfigPath); err == nil {
// Site is in maintenance mode, disable it first
err := site.DisableMaintenance(name)
if err != nil {
cosy.ErrHandler(c, err)
return
}
}
// Then disable the site normally
err := site.Disable(name)
if err != nil {
cosy.ErrHandler(c, err)
return
@ -217,19 +245,21 @@ func BatchUpdateSites(c *gin.Context) {
}
func EnableMaintenanceSite(c *gin.Context) {
err := site.EnableMaintenance(c.Param("name"))
if err != nil {
cosy.ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"message": "ok",
})
}
func DisableMaintenanceSite(c *gin.Context) {
err := site.DisableMaintenance(c.Param("name"))
name := c.Param("name")
// If site is already enabled, disable the normal site first
enabledConfigPath := nginx.GetConfPath("sites-enabled", name)
if _, err := os.Stat(enabledConfigPath); err == nil {
// Site is already enabled, disable normal site first
err := site.Disable(name)
if err != nil {
cosy.ErrHandler(c, err)
return
}
}
// Then enable maintenance mode
err := site.EnableMaintenance(name)
if err != nil {
cosy.ErrHandler(c, err)
return