feat: add reboot and shutdown

This commit is contained in:
2025-05-13 14:37:32 +02:00
parent 9c80c1e771
commit ee0489dcbb
7 changed files with 144 additions and 21 deletions

View File

@@ -7,6 +7,7 @@ import (
"net/http"
network "github.com/0x1d/rcond/pkg/network"
"github.com/0x1d/rcond/pkg/system"
"github.com/0x1d/rcond/pkg/user"
"github.com/gorilla/mux"
)
@@ -256,3 +257,33 @@ func HandleRemoveAuthorizedKey(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
}
func HandleReboot(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeError(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if err := system.Restart(); err != nil {
writeError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
}
func HandleShutdown(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
writeError(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if err := system.Shutdown(); err != nil {
writeError(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
}

View File

@@ -67,6 +67,8 @@ func (s *Server) RegisterRoutes() {
s.router.HandleFunc("/hostname", s.verifyToken(HandleSetHostname)).Methods(http.MethodPost)
s.router.HandleFunc("/users/{user}/keys", s.verifyToken(HandleAddAuthorizedKey)).Methods(http.MethodPost)
s.router.HandleFunc("/users/{user}/keys/{fingerprint}", s.verifyToken(HandleRemoveAuthorizedKey)).Methods(http.MethodDelete)
s.router.HandleFunc("/system/restart", s.verifyToken(HandleReboot)).Methods(http.MethodPost)
s.router.HandleFunc("/system/shutdown", s.verifyToken(HandleShutdown)).Methods(http.MethodPost)
}
func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {