install.go
1 package web 2 3 import ( 4 "encoding/json" 5 "net/http" 6 7 "github.com/dogeorg/dogeboxd/pkg/system" 8 ) 9 10 type InstallToDiskRequest struct { 11 Disk string `json:"disk"` 12 Secret string `json:"secret"` 13 } 14 15 func (t api) getInstallDisks(w http.ResponseWriter, r *http.Request) { 16 disks, err := system.GetSystemDisks() 17 if err != nil { 18 sendErrorResponse(w, http.StatusInternalServerError, err.Error()) 19 return 20 } 21 22 sendResponse(w, disks) 23 } 24 25 func (t api) installToDisk(w http.ResponseWriter, r *http.Request) { 26 var req InstallToDiskRequest 27 if err := json.NewDecoder(r.Body).Decode(&req); err != nil { 28 sendErrorResponse(w, http.StatusBadRequest, "Error unmarshalling JSON: "+err.Error()) 29 return 30 } 31 32 if req.Disk == "" { 33 sendErrorResponse(w, http.StatusBadRequest, "Disk is required") 34 return 35 } 36 37 if req.Secret == "" { 38 sendErrorResponse(w, http.StatusBadRequest, "Secret is required") 39 return 40 } 41 42 if req.Secret != system.DBXRootSecret { 43 sendErrorResponse(w, http.StatusForbidden, "Invalid secret") 44 return 45 } 46 47 dbxState := t.sm.Get().Dogebox 48 49 if err := system.InstallToDisk(t.config, dbxState, req.Disk); err != nil { 50 sendErrorResponse(w, http.StatusInternalServerError, "Error installing to disk: "+err.Error()) 51 return 52 } 53 54 sendResponse(w, map[string]string{"status": "ok"}) 55 }