diff --git a/http.go b/http.go index 1f2544b..8c653f8 100644 --- a/http.go +++ b/http.go @@ -133,15 +133,11 @@ func buildHandler() (http.Handler, error) { // Assets for _, path := range allAssetPaths() { - if path != "" { - r.Get(path, serveAsset(path)) - } + r.Get(path, serveAsset) } paginationPath := "/page/{page}" - rssPath := ".rss" - jsonPath := ".json" - atomPath := ".atom" + feedPath := ".{feed:rss|json|atom}" for blog, blogConfig := range appConfig.Blogs { @@ -155,11 +151,9 @@ func buildHandler() (http.Handler, error) { for _, section := range blogConfig.Sections { if section.Name != "" { path := blogPath + "/" + section.Name - r.With(cacheMiddleware, minifier.Middleware).Get(path, serveSection(blog, path, section, noFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(path+rssPath, serveSection(blog, path, section, rssFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(path+jsonPath, serveSection(blog, path, section, jsonFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(path+atomPath, serveSection(blog, path, section, atomFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(path+paginationPath, serveSection(blog, path, section, noFeed)) + r.With(cacheMiddleware, minifier.Middleware).Get(path, serveSection(blog, path, section)) + r.With(cacheMiddleware, minifier.Middleware).Get(path+feedPath, serveSection(blog, path, section)) + r.With(cacheMiddleware, minifier.Middleware).Get(path+paginationPath, serveSection(blog, path, section)) } } @@ -173,11 +167,9 @@ func buildHandler() (http.Handler, error) { } for _, tv := range values { vPath := path + "/" + urlize(tv) - r.With(cacheMiddleware, minifier.Middleware).Get(vPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, noFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(vPath+rssPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, rssFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(vPath+jsonPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, jsonFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(vPath+atomPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, atomFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(vPath+paginationPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, noFeed)) + r.With(cacheMiddleware, minifier.Middleware).Get(vPath, serveTaxonomyValue(blog, vPath, taxonomy, tv)) + r.With(cacheMiddleware, minifier.Middleware).Get(vPath+feedPath, serveTaxonomyValue(blog, vPath, taxonomy, tv)) + r.With(cacheMiddleware, minifier.Middleware).Get(vPath+paginationPath, serveTaxonomyValue(blog, vPath, taxonomy, tv)) } } } @@ -189,11 +181,9 @@ func buildHandler() (http.Handler, error) { } // Blog - r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath, serveHome(blog, blogPath, noFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+rssPath, serveHome(blog, blogPath, rssFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+jsonPath, serveHome(blog, blogPath, jsonFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+atomPath, serveHome(blog, blogPath, atomFeed)) - r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+paginationPath, serveHome(blog, blogPath, noFeed)) + r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath, serveHome(blog, blogPath)) + r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+feedPath, serveHome(blog, blogPath)) + r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+paginationPath, serveHome(blog, blogPath)) // Custom pages for _, cp := range blogConfig.CustomPages { @@ -207,7 +197,7 @@ func buildHandler() (http.Handler, error) { } // Sitemap - r.With(cacheMiddleware).Get("/sitemap.xml", serveSitemap()) + r.With(cacheMiddleware).Get(sitemapPath, serveSitemap) r.With(minifier.Middleware).NotFound(serve404) diff --git a/posts.go b/posts.go index 254538b..952a017 100644 --- a/posts.go +++ b/posts.go @@ -87,20 +87,18 @@ func (p *postPaginationAdapter) Slice(offset, length int, data interface{}) erro return err } -func serveHome(blog string, path string, ft feedType) func(w http.ResponseWriter, r *http.Request) { +func serveHome(blog string, path string) func(w http.ResponseWriter, r *http.Request) { return serveIndex(&indexConfig{ blog: blog, path: path, - feed: ft, }) } -func serveSection(blog string, path string, section *section, ft feedType) func(w http.ResponseWriter, r *http.Request) { +func serveSection(blog string, path string, section *section) func(w http.ResponseWriter, r *http.Request) { return serveIndex(&indexConfig{ blog: blog, path: path, section: section, - feed: ft, }) } @@ -124,13 +122,12 @@ func serveTaxonomy(blog string, tax *taxonomy) func(w http.ResponseWriter, r *ht } } -func serveTaxonomyValue(blog string, path string, tax *taxonomy, value string, ft feedType) func(w http.ResponseWriter, r *http.Request) { +func serveTaxonomyValue(blog string, path string, tax *taxonomy, value string) func(w http.ResponseWriter, r *http.Request) { return serveIndex(&indexConfig{ blog: blog, path: path, tax: tax, taxValue: value, - feed: ft, }) } @@ -149,7 +146,6 @@ type indexConfig struct { section *section tax *taxonomy taxValue string - feed feedType onlyWithParameter string template string } @@ -189,8 +185,8 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) { description = ic.section.Description } // Check if feed - if ic.feed != noFeed { - generateFeed(ic.blog, ic.feed, w, r, posts, title, description) + if ft := feedType(chi.URLParam(r, "feed")); ft != noFeed { + generateFeed(ic.blog, ft, w, r, posts, title, description) return } // Navigation diff --git a/sitemap.go b/sitemap.go index 178e709..4e53b57 100644 --- a/sitemap.go +++ b/sitemap.go @@ -8,29 +8,29 @@ import ( "github.com/snabb/sitemap" ) -func serveSitemap() func(w http.ResponseWriter, r *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - posts, err := getPosts(r.Context(), &postsRequestConfig{}) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } - sm := sitemap.New() - sm.Minify = true - for _, p := range posts { - item := &sitemap.URL{ - Loc: appConfig.Server.PublicAddress + p.Path} - var lastMod time.Time - if p.Updated != "" { - lastMod, _ = dateparse.ParseIn(p.Updated, time.Local) - } - if p.Published != "" && lastMod.IsZero() { - lastMod, _ = dateparse.ParseIn(p.Published, time.Local) - } - if !lastMod.IsZero() { - item.LastMod = &lastMod - } - sm.Add(item) - } - _, _ = sm.WriteTo(w) +const sitemapPath = "/sitemap.xml" + +func serveSitemap(w http.ResponseWriter, r *http.Request) { + posts, err := getPosts(r.Context(), &postsRequestConfig{}) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) } + sm := sitemap.New() + sm.Minify = true + for _, p := range posts { + item := &sitemap.URL{ + Loc: appConfig.Server.PublicAddress + p.Path} + var lastMod time.Time + if p.Updated != "" { + lastMod, _ = dateparse.ParseIn(p.Updated, time.Local) + } + if p.Published != "" && lastMod.IsZero() { + lastMod, _ = dateparse.ParseIn(p.Published, time.Local) + } + if !lastMod.IsZero() { + item.LastMod = &lastMod + } + sm.Add(item) + } + _, _ = sm.WriteTo(w) } diff --git a/templateAssets.go b/templateAssets.go index e4ad929..87cd7d1 100644 --- a/templateAssets.go +++ b/templateAssets.go @@ -92,9 +92,7 @@ func allAssetPaths() []string { } // Gets only called by registered paths -func serveAsset(path string) func(w http.ResponseWriter, r *http.Request) { - return func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Cache-Control", "public,max-age=31536000,immutable") - http.ServeFile(w, r, compiledAssetsFolder+path) - } +func serveAsset(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Cache-Control", "public,max-age=31536000,immutable") + http.ServeFile(w, r, path.Join(compiledAssetsFolder, r.URL.Path)) }