44 lines
902 B
Go
44 lines
902 B
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
func HandleReboot(w http.ResponseWriter, r *http.Request) {
|
|
MOUNT_POINT := os.Getenv("MOUNT_POINT")
|
|
response := map[string]string{
|
|
"status": "success",
|
|
"message": "System rebooting...",
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
fmt.Printf("Failed to send reboot response: %v\n", err)
|
|
return
|
|
}
|
|
|
|
syscall.Sync()
|
|
|
|
fmt.Printf("Unmounting %s...\n", MOUNT_POINT)
|
|
if err := syscall.Unmount(MOUNT_POINT, 0); err != nil {
|
|
fmt.Printf("Unmount error: %v\n", err)
|
|
}
|
|
|
|
syscall.Sync()
|
|
|
|
fmt.Println("Initiating soft reboot via shutdown...")
|
|
|
|
go func() {
|
|
time.Sleep(500 * time.Millisecond)
|
|
cmd := exec.Command("shutdown", "-r", "now")
|
|
if err := cmd.Run(); err != nil {
|
|
fmt.Printf("Reboot failed: %v\n", err)
|
|
}
|
|
}()
|
|
}
|