move rootHandler to site.go
Jes Olson j3s@c3f.net
Sat, 13 May 2023 22:33:09 -0700
M
main.go
→
main.go
@@ -3,7 +3,6 @@
import ( "fmt" "net/http" - "strings" ) func main() {@@ -12,18 +11,9 @@ mux := http.NewServeMux()
// since "/" is a wildcard, this anonymous function acts // as a router for patterns that can't be registered at // start time. e.g. /j3s or /j3s/feeds - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path == "/" { - s.indexHandler(w, r) - return - } - // handles /<username> - if s.db.UserExists(strings.TrimPrefix(r.URL.Path, "/")) { - s.userHandler(w, r) - return - } - http.NotFound(w, r) - }) + + // handles /, /<username>, and 404 + mux.HandleFunc("/", s.rootHandler) mux.HandleFunc("/feeds", s.feedsHandler) mux.HandleFunc("/feeds/submit", s.feedsSubmitHandler) mux.HandleFunc("/login", s.loginHandler)
M
site.go
→
site.go
@@ -41,6 +41,19 @@ }
return &s } +func (s *Site) rootHandler(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/" { + s.indexHandler(w, r) + return + } + // handles /<username> + if s.db.UserExists(strings.TrimPrefix(r.URL.Path, "/")) { + s.userHandler(w, r) + return + } + http.NotFound(w, r) +} + func (s *Site) indexHandler(w http.ResponseWriter, r *http.Request) { if !s.methodAllowed(w, r, "GET") { return