fs.go (1498B)
1 package handlers 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "strconv" 7 "time" 8 9 "git.prime8.dev/moncheeta/prime8dev/internal/config" 10 "git.prime8.dev/moncheeta/prime8dev/internal/content" 11 "git.prime8.dev/moncheeta/prime8dev/internal/vfs" 12 ) 13 14 // fsResponse is the read-only virtual filesystem behind the web shell on the 15 // about page: the same tree the SSH shell uses, for the anonymous user. 16 type fsResponse struct { 17 User string `json:"user"` 18 Home string `json:"home"` 19 Root *vfs.Node `json:"root"` 20 } 21 22 const fsCacheMaxAgeSeconds = 300 23 24 // FSHandler serves GET /api/fs. Everything in it is generated from 25 // config/content; nothing is read from the server's filesystem. 26 func (h *Handler) FSHandler(w http.ResponseWriter, r *http.Request) { 27 projects, err := content.LoadProjects(h.gitService) 28 if err != nil { 29 http.Error(w, "Failed to load projects", http.StatusInternalServerError) 30 return 31 } 32 exp := config.GetExperience() 33 user := vfs.FallbackUser 34 root := vfs.Build(vfs.Source{ 35 User: user, 36 About: config.GetAbout(), 37 CurrentJob: config.CurrentJob(exp), 38 Experience: exp, 39 Projects: projects, 40 Now: time.Now(), 41 }) 42 43 w.Header().Set("Content-Type", "application/json; charset=utf-8") 44 w.Header().Set("Cache-Control", "public, max-age="+strconv.Itoa(fsCacheMaxAgeSeconds)) 45 if err := json.NewEncoder(w).Encode(fsResponse{User: user, Home: vfs.HomeDir(user), Root: root}); err != nil { 46 http.Error(w, "Failed to encode fs", http.StatusInternalServerError) 47 } 48 }