8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / server / project.go
2.8 KB
1package server 2 3import ( 4 "mime" 5 "net/http" 6 "path" 7 "strings" 8 9 "xonecas.io/site/internal/repo" 10 "xonecas.io/site/internal/site" 11) 12 13func projectHome(_ http.ResponseWriter, r *http.Request, st *state) (string, any, error) { 14 i, f, err := projectFiles(r, st) 15 if err != nil { 16 return "", nil, err 17 } 18 page, err := site.NewProjectPage(st.shown[i], st.rows[i], f.tree, f.kids) 19 return "project.html", page, err 20} 21 22// browse is a folder under tree/ or a file under blob/. Their addresses end in a slash. 23func browse(_ http.ResponseWriter, r *http.Request, st *state) (string, any, error) { 24 i, f, err := projectFiles(r, st) 25 if err != nil { 26 return "", nil, err 27 } 28 p := strings.TrimSuffix(r.PathValue("path"), "/") 29 if f.tree.Dirs[p] != strings.Contains(r.URL.Path, "/tree/") { 30 return "", nil, site.ErrNotFound 31 } 32 page, err := site.NewBrowsePage(st.shown[i].Name, st.shown[i].Sources[0].Dir, st.rows[i], f.tree, f.kids, p) 33 return "browse.html", page, err 34} 35 36func commits(_ http.ResponseWriter, r *http.Request, st *state) (string, any, error) { 37 i, log, err := projectLog(r, st) 38 if err != nil { 39 return "", nil, err 40 } 41 n, err := pageNumber(r) 42 if err != nil { 43 return "", nil, err 44 } 45 page, err := site.NewLogPage(st.shown[i].Name, st.rows[i], log, n) 46 return "commits.html", page, err 47} 48 49func commit(_ http.ResponseWriter, r *http.Request, st *state) (string, any, error) { 50 i, log, err := projectLog(r, st) 51 if err != nil { 52 return "", nil, err 53 } 54 page, err := site.NewCommitPage(st.shown[i].Name, st.rows[i], log, r.PathValue("hash")) 55 return "commit.html", page, err 56} 57 58// raw serves a file on HEAD as itself, for README images. It runs no scripts, as an SVG could have. 59func (s *Server) raw(w http.ResponseWriter, r *http.Request) { 60 st, err := s.state() 61 if err != nil { 62 http.Error(w, err.Error(), http.StatusInternalServerError) 63 return 64 } 65 i, f, err := projectFiles(r, st) 66 p := r.PathValue("path") 67 if _, ok := f.tree.Files[p]; err != nil || !ok { 68 s.notFound(w, r) 69 return 70 } 71 b, err := repo.Blob(st.shown[i].Sources[0].Dir, "HEAD:"+p) 72 if err != nil { 73 http.Error(w, err.Error(), http.StatusInternalServerError) 74 return 75 } 76 kind := mime.TypeByExtension(path.Ext(p)) 77 if kind == "" { 78 kind = http.DetectContentType(b) 79 } 80 w.Header().Set("Content-Type", kind) 81 w.Header().Set("Content-Security-Policy", "sandbox") 82 w.Header().Set("Cache-Control", "no-cache") 83 w.Write(b) 84} 85 86// projectFiles is the project {name} and its files. 87func projectFiles(r *http.Request, st *state) (int, files, error) { 88 i, err := st.project(r.PathValue("name")) 89 if err != nil { 90 return 0, files{}, err 91 } 92 f, err := st.files(i) 93 return i, f, err 94} 95 96// projectLog is the project {name} and its commits. 97func projectLog(r *http.Request, st *state) (int, []site.LogEntry, error) { 98 i, err := st.project(r.PathValue("name")) 99 if err != nil { 100 return 0, nil, err 101 } 102 log, err := st.log(i) 103 return i, log, err 104}