8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / server / server.go
3.1 KB
1// Package server serves xonecas.io: every page rendered on request from the repos and the data file, 2// the assets, and clones of each project. 3package server 4 5import ( 6 "bytes" 7 "errors" 8 "log" 9 "net/http" 10 "sync" 11 12 "xonecas.io/site/internal/site" 13) 14 15// Server is the site's HTTP handler. 16type Server struct { 17 data string // data/projects.json, from site index 18 assets map[string][]byte // by path, from site.Assets 19 mux *http.ServeMux 20 mu sync.Mutex 21 st *state 22} 23 24// page renders one template with its data for a request, or fails with site.ErrNotFound. 25type page func(w http.ResponseWriter, r *http.Request, st *state) (name string, data any, err error) 26 27// New loads the data file and compiles the assets with a tailwind binary ("" for none). 28func New(data, tailwind string) (*Server, error) { 29 assets, err := site.Assets(tailwind) 30 if err != nil { 31 return nil, err 32 } 33 s := &Server{data: data, assets: assets, mux: http.NewServeMux()} 34 if _, err := s.state(); err != nil { 35 return nil, err 36 } 37 for pattern, p := range map[string]page{ 38 "GET /{$}": home, 39 "GET /projects/{$}": projects, 40 "GET /activity/{$}": activity, 41 "GET /activity/{year}/{$}": activity, 42 "GET /activity/{year}/{page}/{$}": activity, 43 "GET /style/{$}": style, 44 "GET /suggest": suggest, 45 "GET /projects/{name}/{$}": projectHome, 46 "GET /projects/{name}/tree/{path...}": browse, 47 "GET /projects/{name}/blob/{path...}": browse, 48 "GET /projects/{name}/commits/{$}": commits, 49 "GET /projects/{name}/commits/{page}/{$}": commits, 50 "GET /projects/{name}/commit/{hash}/{$}": commit, 51 } { 52 s.mux.HandleFunc(pattern, s.render(p)) 53 } 54 s.mux.HandleFunc("GET /projects/{name}/raw/{path...}", s.raw) 55 s.mux.HandleFunc("/", s.files) 56 return s, nil 57} 58 59func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.mux.ServeHTTP(w, r) } 60 61// render serves p: the page, the not-found page for site.ErrNotFound, or a 500 for anything else. 62// htmx asks for the same pages as a browser, and picks the part it swaps with hx-select. 63func (s *Server) render(p page) http.HandlerFunc { 64 return func(w http.ResponseWriter, r *http.Request) { 65 st, err := s.state() 66 name, data := "", any(nil) 67 if err == nil { 68 name, data, err = p(w, r, st) 69 } 70 code := http.StatusOK 71 if errors.Is(err, site.ErrNotFound) { 72 code, name, data, err = http.StatusNotFound, "notfound.html", nil, nil 73 } 74 var buf bytes.Buffer 75 if err == nil { 76 err = site.Render(&buf, name, data) 77 } 78 if err != nil { 79 log.Printf("%s: %v", r.URL, err) 80 http.Error(w, "Something broke. It is logged.", http.StatusInternalServerError) 81 return 82 } 83 w.Header().Set("Content-Type", "text/html; charset=utf-8") 84 w.Header().Set("Cache-Control", "no-cache") 85 w.WriteHeader(code) 86 w.Write(buf.Bytes()) 87 } 88} 89 90// notFound serves the not-found page. 91func (s *Server) notFound(w http.ResponseWriter, r *http.Request) { 92 s.render(func(http.ResponseWriter, *http.Request, *state) (string, any, error) { 93 return "", nil, site.ErrNotFound 94 })(w, r) 95}