mirror of https://github.com/jlelse/GoBlog
Simplify routing code a bit
This commit is contained in:
parent
50b4bce82f
commit
dff1da6f68
34
http.go
34
http.go
|
@ -133,15 +133,11 @@ func buildHandler() (http.Handler, error) {
|
||||||
|
|
||||||
// Assets
|
// Assets
|
||||||
for _, path := range allAssetPaths() {
|
for _, path := range allAssetPaths() {
|
||||||
if path != "" {
|
r.Get(path, serveAsset)
|
||||||
r.Get(path, serveAsset(path))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
paginationPath := "/page/{page}"
|
paginationPath := "/page/{page}"
|
||||||
rssPath := ".rss"
|
feedPath := ".{feed:rss|json|atom}"
|
||||||
jsonPath := ".json"
|
|
||||||
atomPath := ".atom"
|
|
||||||
|
|
||||||
for blog, blogConfig := range appConfig.Blogs {
|
for blog, blogConfig := range appConfig.Blogs {
|
||||||
|
|
||||||
|
@ -155,11 +151,9 @@ func buildHandler() (http.Handler, error) {
|
||||||
for _, section := range blogConfig.Sections {
|
for _, section := range blogConfig.Sections {
|
||||||
if section.Name != "" {
|
if section.Name != "" {
|
||||||
path := blogPath + "/" + 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, serveSection(blog, path, section))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(path+rssPath, serveSection(blog, path, section, rssFeed))
|
r.With(cacheMiddleware, minifier.Middleware).Get(path+feedPath, serveSection(blog, path, section))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(path+jsonPath, serveSection(blog, path, section, jsonFeed))
|
r.With(cacheMiddleware, minifier.Middleware).Get(path+paginationPath, serveSection(blog, path, section))
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,11 +167,9 @@ func buildHandler() (http.Handler, error) {
|
||||||
}
|
}
|
||||||
for _, tv := range values {
|
for _, tv := range values {
|
||||||
vPath := path + "/" + urlize(tv)
|
vPath := path + "/" + urlize(tv)
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(vPath, 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+rssPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, rssFeed))
|
r.With(cacheMiddleware, minifier.Middleware).Get(vPath+feedPath, serveTaxonomyValue(blog, vPath, taxonomy, tv))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(vPath+jsonPath, serveTaxonomyValue(blog, vPath, taxonomy, tv, jsonFeed))
|
r.With(cacheMiddleware, minifier.Middleware).Get(vPath+paginationPath, serveTaxonomyValue(blog, vPath, taxonomy, tv))
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,11 +181,9 @@ func buildHandler() (http.Handler, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blog
|
// Blog
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath, serveHome(blog, blogPath, noFeed))
|
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath, serveHome(blog, blogPath))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+rssPath, serveHome(blog, blogPath, rssFeed))
|
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+feedPath, serveHome(blog, blogPath))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+jsonPath, serveHome(blog, blogPath, jsonFeed))
|
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+paginationPath, serveHome(blog, blogPath))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+atomPath, serveHome(blog, blogPath, atomFeed))
|
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+paginationPath, serveHome(blog, blogPath, noFeed))
|
|
||||||
|
|
||||||
// Custom pages
|
// Custom pages
|
||||||
for _, cp := range blogConfig.CustomPages {
|
for _, cp := range blogConfig.CustomPages {
|
||||||
|
@ -207,7 +197,7 @@ func buildHandler() (http.Handler, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sitemap
|
// Sitemap
|
||||||
r.With(cacheMiddleware).Get("/sitemap.xml", serveSitemap())
|
r.With(cacheMiddleware).Get(sitemapPath, serveSitemap)
|
||||||
|
|
||||||
r.With(minifier.Middleware).NotFound(serve404)
|
r.With(minifier.Middleware).NotFound(serve404)
|
||||||
|
|
||||||
|
|
14
posts.go
14
posts.go
|
@ -87,20 +87,18 @@ func (p *postPaginationAdapter) Slice(offset, length int, data interface{}) erro
|
||||||
return err
|
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{
|
return serveIndex(&indexConfig{
|
||||||
blog: blog,
|
blog: blog,
|
||||||
path: path,
|
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{
|
return serveIndex(&indexConfig{
|
||||||
blog: blog,
|
blog: blog,
|
||||||
path: path,
|
path: path,
|
||||||
section: section,
|
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{
|
return serveIndex(&indexConfig{
|
||||||
blog: blog,
|
blog: blog,
|
||||||
path: path,
|
path: path,
|
||||||
tax: tax,
|
tax: tax,
|
||||||
taxValue: value,
|
taxValue: value,
|
||||||
feed: ft,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,7 +146,6 @@ type indexConfig struct {
|
||||||
section *section
|
section *section
|
||||||
tax *taxonomy
|
tax *taxonomy
|
||||||
taxValue string
|
taxValue string
|
||||||
feed feedType
|
|
||||||
onlyWithParameter string
|
onlyWithParameter string
|
||||||
template string
|
template string
|
||||||
}
|
}
|
||||||
|
@ -189,8 +185,8 @@ func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) {
|
||||||
description = ic.section.Description
|
description = ic.section.Description
|
||||||
}
|
}
|
||||||
// Check if feed
|
// Check if feed
|
||||||
if ic.feed != noFeed {
|
if ft := feedType(chi.URLParam(r, "feed")); ft != noFeed {
|
||||||
generateFeed(ic.blog, ic.feed, w, r, posts, title, description)
|
generateFeed(ic.blog, ft, w, r, posts, title, description)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Navigation
|
// Navigation
|
||||||
|
|
48
sitemap.go
48
sitemap.go
|
@ -8,29 +8,29 @@ import (
|
||||||
"github.com/snabb/sitemap"
|
"github.com/snabb/sitemap"
|
||||||
)
|
)
|
||||||
|
|
||||||
func serveSitemap() func(w http.ResponseWriter, r *http.Request) {
|
const sitemapPath = "/sitemap.xml"
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
posts, err := getPosts(r.Context(), &postsRequestConfig{})
|
func serveSitemap(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
posts, err := getPosts(r.Context(), &postsRequestConfig{})
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
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)
|
|
||||||
}
|
}
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,9 +92,7 @@ func allAssetPaths() []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets only called by registered paths
|
// Gets only called by registered paths
|
||||||
func serveAsset(path string) func(w http.ResponseWriter, r *http.Request) {
|
func serveAsset(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
w.Header().Add("Cache-Control", "public,max-age=31536000,immutable")
|
||||||
w.Header().Add("Cache-Control", "public,max-age=31536000,immutable")
|
http.ServeFile(w, r, path.Join(compiledAssetsFolder, r.URL.Path))
|
||||||
http.ServeFile(w, r, compiledAssetsFolder+path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue