1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-21 07:46:52 +00:00

Refactor /recent-changes/ handlers

This commit is contained in:
handlerug 2021-07-25 20:16:34 +07:00
parent fc6556a819
commit ea1f0a8e51
2 changed files with 10 additions and 17 deletions

View File

@ -5,7 +5,6 @@ import (
"log"
"net/http"
"strconv"
"strings"
"github.com/gorilla/mux"
@ -18,7 +17,11 @@ import (
func initHistory(r *mux.Router) {
r.PathPrefix("/history/").HandlerFunc(handlerHistory)
r.PathPrefix("/recent-changes/").HandlerFunc(handlerRecentChanges)
r.HandleFunc("/recent-changes/{count:[0-9]+}", handlerRecentChanges)
r.HandleFunc("/recent-changes/", func(w http.ResponseWriter, rq *http.Request) {
http.Redirect(w, rq, "/recent-changes/20", http.StatusSeeOther)
})
r.HandleFunc("/recent-changes-rss", handlerRecentChangesRSS)
r.HandleFunc("/recent-changes-atom", handlerRecentChangesAtom)
r.HandleFunc("/recent-changes-json", handlerRecentChangesJSON)
@ -26,7 +29,6 @@ func initHistory(r *mux.Router) {
// handlerHistory lists all revisions of a hypha.
func handlerHistory(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
hyphaName := util.HyphaNameFromRq(rq, "history")
var list string
@ -43,21 +45,13 @@ func handlerHistory(w http.ResponseWriter, rq *http.Request) {
// handlerRecentChanges displays the /recent-changes/ page.
func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
var (
noPrefix = strings.TrimPrefix(rq.URL.String(), "/recent-changes/")
n, err = strconv.Atoi(noPrefix)
)
if err == nil && n < 101 {
util.HTTP200Page(w, views.BaseHTML(strconv.Itoa(n)+" recent changes", views.RecentChangesHTML(n), user.FromRequest(rq)))
} else {
http.Redirect(w, rq, "/recent-changes/20", http.StatusSeeOther)
}
// Error ignored: filtered by regex
n, _ := strconv.Atoi(mux.Vars(rq)["count"])
util.HTTP200Page(w, views.BaseHTML(strconv.Itoa(n)+" recent changes", views.RecentChangesHTML(n), user.FromRequest(rq)))
}
// genericHandlerOfFeeds is a helper function for the web feed handlers.
func genericHandlerOfFeeds(w http.ResponseWriter, rq *http.Request, f func() (string, error), name string) {
util.PrepareRq(rq)
if content, err := f(); err != nil {
w.Header().Set("Content-Type", "text/plain;charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)

View File

@ -42,8 +42,6 @@ func httpErr(w http.ResponseWriter, status int, name, title, errMsg string) {
}
func handlerStyle(w http.ResponseWriter, rq *http.Request) {
util.PrepareRq(rq)
w.Header().Set("Content-Type", mime.TypeByExtension(".css"))
for _, name := range stylesheets {
file, err := static.FS.Open(name)
@ -65,7 +63,8 @@ func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
file, err := static.FS.Open("robots.txt")
if err != nil { return
if err != nil {
return
}
io.Copy(w, file)
file.Close()