8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / server / files.go
1.1 KB
1package server 2 3import ( 4 "mime" 5 "net/http" 6 "path" 7 "strings" 8 9 "xonecas.io/site/internal/site" 10) 11 12// files serves what is not a page: the assets, robots.txt, sitemap.xml and llms.txt, and clones of <name>.git. 13// Anything else is the not-found page. 14func (s *Server) files(w http.ResponseWriter, r *http.Request) { 15 name, rest, _ := strings.Cut(strings.TrimPrefix(r.URL.Path, "/"), "/") 16 if strings.HasSuffix(name, ".git") { 17 s.clone(w, r, strings.TrimSuffix(name, ".git")) 18 return 19 } 20 if r.Method != http.MethodGet && r.Method != http.MethodHead || rest != "" { 21 s.notFound(w, r) 22 return 23 } 24 st, err := s.state() 25 if err != nil { 26 http.Error(w, err.Error(), http.StatusInternalServerError) 27 return 28 } 29 b, ok := s.assets[name] 30 if text, seo := st.seo[name]; seo { 31 b, ok = []byte(text), true 32 } 33 if !ok { 34 s.notFound(w, r) 35 return 36 } 37 w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(name))) 38 if v := r.URL.Query().Get("v"); v != "" && v == site.Version(name) { 39 w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") // a new build gets a new ?v= 40 } else { 41 w.Header().Set("Cache-Control", "no-cache") 42 } 43 w.Write(b) 44}