fork/gdu
git clone https://git.julia.blue/fork/gdu
d672bf4
subvolume
1package webui 2 3import ( 4"encoding/json" 5"errors" 6"fmt" 7"net/http" 8 9 log"github.com/sirupsen/logrus" 10) 11 12// statusResponse describes the current scan state and display preferences. 13type statusResponse struct { 14State string `json:"state"` // "scanning" | "done" | "error" 15Error string `json:"error,omitempty"` 16RootPath string `json:"rootPath"` 17Progress progressJSON `json:"progress"` 18ShowApparentSize bool `json:"showApparentSize"` 19ShowRelativeSize bool `json:"showRelativeSize"` 20UseSIPrefix bool `json:"useSIPrefix"` 21} 22 23type progressJSON struct { 24CurrentItem string `json:"currentItem"` 25ItemCount int64 `json:"itemCount"` 26TotalUsage int64 `json:"totalUsage"` 27} 28 29type deviceJSON struct { 30Name string `json:"name"` 31MountPoint string `json:"mountPoint"` 32Fstype string `json:"fstype"` 33Size int64 `json:"size"` 34Free int64 `json:"free"` 35} 36 37func (ui * UI )buildStatus ()statusResponse { 38ui .mu .RLock () 39defer ui .mu .RUnlock () 40 41state := "done" 42switch { 43case ui .scanErr != nil : 44state = "error" 45case ui .scanning : 46state = "scanning" 47case ui .topDir == nil : 48state = "scanning" 49 } 50 51resp := statusResponse { 52State :state , 53RootPath :ui .topDirPath , 54Progress :progressJSON { 55CurrentItem :ui .progress .CurrentItemName , 56ItemCount :ui .progress .ItemCount , 57TotalUsage :ui .progress .TotalUsage , 58 }, 59ShowApparentSize :ui .ShowApparentSize , 60ShowRelativeSize :ui .ShowRelativeSize , 61UseSIPrefix :ui .UseSIPrefix , 62 } 63if ui .scanErr != nil { 64resp .Error = ui .scanErr .Error () 65 } 66return resp 67} 68 69func (ui * UI )handleStatus (w http.ResponseWriter ,_ * http.Request ) { 70writeJSON (w ,http .StatusOK ,ui .buildStatus ()) 71} 72 73func (ui * UI )handleNodes (w http.ResponseWriter ,r * http.Request ) { 74path := r .URL .Query ().Get ("path" ) 75 76node ,err := ui .findNode (path ) 77if err != nil { 78switch { 79case errors .Is (err ,errOutsideRoot ): 80writeError (w ,http .StatusForbidden ,err .Error ()) 81case errors .Is (err ,errNotFound ): 82writeError (w ,http .StatusNotFound ,err .Error ()) 83default : 84writeError (w ,http .StatusInternalServerError ,err .Error ()) 85 } 86return 87 } 88 89sortBy ,order := parseSort (r .URL .Query ().Get ("sort" ),r .URL .Query ().Get ("order" )) 90writeJSON (w ,http .StatusOK ,buildNodeResponse (node ,sortBy ,order )) 91} 92 93func (ui * UI )handleDevices (w http.ResponseWriter ,_ * http.Request ) { 94ui .mu .RLock () 95devices := ui .devices 96ui .mu .RUnlock () 97 98out := make ([]deviceJSON ,0 ,len (devices )) 99for _ ,d := range devices { 100out = append (out ,deviceJSON { 101Name :d .Name , 102MountPoint :d .MountPoint , 103Fstype :d .Fstype , 104Size :d .Size , 105Free :d .Free , 106 }) 107 } 108writeJSON (w ,http .StatusOK ,out ) 109} 110 111// handleEvents streams scan status updates using Server-Sent Events. 112func (ui * UI )handleEvents (w http.ResponseWriter ,r * http.Request ) { 113flusher ,ok := w .(http.Flusher ) 114if ! ok { 115writeError (w ,http .StatusInternalServerError ,"streaming unsupported" ) 116return 117 } 118 119w .Header ().Set ("Content-Type" ,"text/event-stream" ) 120w .Header ().Set ("Cache-Control" ,"no-cache" ) 121w .Header ().Set ("Connection" ,"keep-alive" ) 122 123ch ,last := ui .hub .subscribe () 124defer ui .hub .unsubscribe (ch ) 125 126// send current state immediately on connect 127if last == "" { 128last = ui .statusJSON () 129 } 130fmt .Fprintf (w ,"data: %s\n\n" ,last ) 131flusher .Flush () 132 133for { 134select { 135case <- r .Context ().Done (): 136return 137case msg ,open := <- ch : 138if ! open { 139return 140 } 141fmt .Fprintf (w ,"data: %s\n\n" ,msg ) 142flusher .Flush () 143 } 144 } 145} 146 147func writeJSON (w http.ResponseWriter ,status int ,payload any ) { 148w .Header ().Set ("Content-Type" ,"application/json" ) 149w .WriteHeader (status ) 150if err := json .NewEncoder (w ).Encode (payload );err != nil { 151log .Printf ("webui: encoding response: %s" ,err ) 152 } 153} 154 155func writeError (w http.ResponseWriter ,status int ,message string ) { 156writeJSON (w ,status ,map [string ]string {"error" :message }) 157}