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) }) }