small pixel drawing of a pufferfish vore

move rootHandler to site.go
Jes Olson j3s@c3f.net
Sat, 13 May 2023 22:33:09 -0700
commit

01a8280c31c1653c999e148e0f1468c712836d27

parent

f7f63bcb84087e3f67ed5aaf49767eb641910555

2 files changed, 16 insertions(+), 13 deletions(-)

jump to
M main.gomain.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.gosite.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