photoprism/internal/api/server.go
Michael Mayer 3e4121cc78 Server: Add "restart required" flag and button to restart the server
Signed-off-by: Michael Mayer <michael@photoprism.app>
2023-03-27 19:27:19 +02:00

40 lines
929 B
Go

package api
import (
"net/http"
"os"
"syscall"
"github.com/gin-gonic/gin"
"github.com/photoprism/photoprism/internal/acl"
"github.com/photoprism/photoprism/internal/get"
)
// StopServer shuts down the server.
//
// POST /api/v1/server/stop
func StopServer(router *gin.RouterGroup) {
router.POST("/server/stop", func(c *gin.Context) {
s := Auth(c, acl.ResourceConfig, acl.ActionManage)
conf := get.Config()
// Abort if permission was not granted.
if s.Invalid() || conf.Public() || conf.DisableSettings() || conf.DisableRestart() {
AbortForbidden(c)
return
}
process, err := os.FindProcess(os.Getpid())
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, NewResponse(http.StatusInternalServerError, err, ""))
return
} else {
c.JSON(http.StatusOK, conf.Options())
}
if err = process.Signal(syscall.SIGTERM); err != nil {
log.Errorf("server: %s", err)
}
})
}