package server import ( "fmt" "gitea.inhome.blapointe.com/local/gziphttp" "gitea.inhome.blapointe.com/local/notes-server/config" "gitea.inhome.blapointe.com/local/router" "net/http" "path/filepath" "strings" ) func (s *Server) Routes() error { wildcard := router.Wildcard endpoints := map[string]struct { handler http.HandlerFunc }{ "/": { handler: s.root, }, fmt.Sprintf("raw/%s%s", wildcard, wildcard): { handler: s.gzip(s.authenticate(s.raw)), }, fmt.Sprintf("notes/%s%s", wildcard, wildcard): { handler: s.gzip(s.authenticate(s.notes)), }, fmt.Sprintf("edit/%s%s", wildcard, wildcard): { handler: s.gzip(s.authenticate(s.edit)), }, fmt.Sprintf("delete/%s%s", wildcard, wildcard): { handler: s.gzip(s.authenticate(s.delete)), }, fmt.Sprintf("submit/%s%s", wildcard, wildcard): { handler: s.gzip(s.authenticate(s.submit)), }, fmt.Sprintf("create/%s%s", wildcard, wildcard): { handler: s.gzip(s.authenticate(s.create)), }, fmt.Sprintf("attach/%s%s", wildcard, wildcard): { handler: s.gzip(s.authenticate(s.attach)), }, fmt.Sprintf("comment/%s%s", wildcard, wildcard): { handler: s.gzip(s.authenticate(s.comment)), }, fmt.Sprintf("search"): { handler: s.gzip(s.authenticate(s.search)), }, } for path, endpoint := range endpoints { if config.ReadOnly { for _, prefix := range []string{ "edit/", "delete/", "create/", "submit/", "attach/", } { if strings.HasPrefix(path, prefix) { endpoint.handler = http.NotFound } } } if err := s.Add(path, endpoint.handler); err != nil { return err } } return nil } func (s *Server) root(w http.ResponseWriter, r *http.Request) { r.URL.Path = "/notes" http.Redirect(w, r, r.URL.String(), http.StatusPermanentRedirect) } func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if gziphttp.Can(r) { gz := gziphttp.New(w) defer gz.Close() w = gz } if filepath.Ext(r.URL.Path) == ".css" { w.Header().Set("Content-Type", "text/css; charset=utf-8") } h(w, r) } }