mirror of
https://github.com/luckjiawei/podux.git
synced 2026-11-30 00:56:47 +00:00
116 lines
3.1 KiB
Go
116 lines
3.1 KiB
Go
package httphandler
|
|
|
|
import (
|
|
"podux/internal/application/system"
|
|
"net/http"
|
|
|
|
"github.com/pocketbase/pocketbase/core"
|
|
)
|
|
|
|
type SystemHandler struct {
|
|
app core.App
|
|
service *system.Service
|
|
}
|
|
|
|
func NewSystemHandler(app core.App, service *system.Service) *SystemHandler {
|
|
return &SystemHandler{
|
|
app: app,
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
func (h *SystemHandler) RegisterHandlers(e *core.ServeEvent) {
|
|
e.Router.GET("/api/system/initialized", func(e *core.RequestEvent) error {
|
|
initialized, err := h.service.CheckInitialized()
|
|
if err != nil {
|
|
return e.JSON(http.StatusInternalServerError, map[string]interface{}{
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, map[string]interface{}{
|
|
"initialized": initialized,
|
|
})
|
|
})
|
|
|
|
e.Router.GET("/api/system/settings", requireAuth(func(e *core.RequestEvent) error {
|
|
settings, err := h.service.GetSettings()
|
|
if err != nil {
|
|
return e.JSON(http.StatusInternalServerError, map[string]interface{}{
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, settings)
|
|
}))
|
|
|
|
// Update system settings
|
|
e.Router.PUT("/api/system/settings", requireAuth(func(e *core.RequestEvent) error {
|
|
var req system.UpdateSettingsRequest
|
|
if err := e.BindBody(&req); err != nil {
|
|
return e.JSON(http.StatusBadRequest, map[string]interface{}{
|
|
"error": "Invalid request body",
|
|
})
|
|
}
|
|
|
|
if err := h.service.UpdateSettings(&req); err != nil {
|
|
return e.JSON(http.StatusInternalServerError, map[string]interface{}{
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, map[string]interface{}{
|
|
"message": "Settings updated successfully",
|
|
})
|
|
}))
|
|
|
|
e.Router.GET("/api/system/version", requireAuth(func(e *core.RequestEvent) error {
|
|
return e.JSON(http.StatusOK, map[string]string{
|
|
"version": h.service.GetAppVersion(),
|
|
})
|
|
}))
|
|
|
|
e.Router.GET("/api/system/latest-version", requireAuth(func(e *core.RequestEvent) error {
|
|
latest, err := h.service.GetLatestVersion()
|
|
if err != nil {
|
|
return e.JSON(http.StatusInternalServerError, map[string]interface{}{
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
return e.JSON(http.StatusOK, latest)
|
|
}))
|
|
|
|
// Initialize system (create admin user and settings).
|
|
// Rejected if the system is already initialized to prevent unauthorized reset.
|
|
e.Router.POST("/api/system/initialize", func(e *core.RequestEvent) error {
|
|
initialized, err := h.service.CheckInitialized()
|
|
if err != nil {
|
|
return e.JSON(http.StatusInternalServerError, map[string]interface{}{
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
if initialized {
|
|
return e.JSON(http.StatusForbidden, map[string]interface{}{
|
|
"error": "system already initialized",
|
|
})
|
|
}
|
|
|
|
var req system.InitializeRequest
|
|
if err := e.BindBody(&req); err != nil {
|
|
return e.JSON(http.StatusBadRequest, map[string]interface{}{
|
|
"error": "Invalid request body",
|
|
})
|
|
}
|
|
|
|
if err := h.service.InitializeSystem(&req); err != nil {
|
|
return e.JSON(http.StatusBadRequest, map[string]interface{}{
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, map[string]interface{}{
|
|
"message": "System initialized successfully",
|
|
})
|
|
})
|
|
}
|