A fast and simple blog backend.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
mediocre-blog/src/http/index.go

36 lines
760 B

package http
import (
"net/http"
"path/filepath"
"regexp"
"strings"
)
func (a *api) renderIndexHandler() http.Handler {
legacyPostPathRegexp := regexp.MustCompile(
`^/[0-9]{4}/[0-9]{2}/[0-9]{2}/([^/.]+)\.html$`,
)
tpl := a.mustParseBasedTpl("index.html")
const pageCount = 10
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if matches := legacyPostPathRegexp.FindStringSubmatch(path); len(matches) == 2 {
id := matches[1]
http.Redirect(rw, r, filepath.Join("/posts", id), http.StatusMovedPermanently)
return
}
if !strings.HasSuffix(path, "/") && filepath.Base(path) != "index.html" {
http.Error(rw, "Page not found", 404)
return
}
executeTemplate(rw, r, tpl, nil)
})
}