Skip to content

Commit

Permalink
router: use http.ServeMux instead of Chi
Browse files Browse the repository at this point in the history
Closes #122

Signed-off-by: Sumner Evans <me@sumnerevans.com>
  • Loading branch information
sumnerevans committed May 22, 2024
1 parent 4ec70e0 commit 9eab670
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.22
toolchain go1.22.0

require (
github.com/go-chi/chi/v5 v5.0.12
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/rs/zerolog v1.32.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
Expand Down
71 changes: 34 additions & 37 deletions internal/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"regexp"
"strings"

"github.com/go-chi/chi/v5"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
"github.com/sendgrid/sendgrid-go"
Expand Down Expand Up @@ -98,7 +97,7 @@ func (a *Application) Start() {

a.Log.Info().Msg("Starting router")

r := chi.NewRouter()
router := http.NewServeMux()

noArgs := func(r *http.Request) map[string]any { return nil }

Expand All @@ -107,7 +106,7 @@ func (a *Application) Start() {
Template string
ArgGenerator func(r *http.Request) map[string]any
}{
"/": {"home.html", noArgs},
"/{$}": {"home.html", noArgs},
"/info": {"info.html", noArgs},
"/authors": {"authors.html", noArgs},
"/rules": {"rules.html", noArgs},
Expand All @@ -116,11 +115,11 @@ func (a *Application) Start() {
"/archive": {"archive.html", a.GetArchiveTemplate},
}
for path, templateInfo := range staticPages {
r.Get(path, a.ServeTemplate(a.Log, templateInfo.Template, templateInfo.ArgGenerator))
router.HandleFunc("GET "+path, a.ServeTemplate(a.Log, templateInfo.Template, templateInfo.ArgGenerator))
}

// Serve static files
r.Handle("/static/*", http.FileServer(http.FS(website.StaticFS)))
router.Handle("GET /static/", http.FileServer(http.FS(website.StaticFS)))

// Redirect pages
redirects := map[string]string{
Expand All @@ -134,7 +133,7 @@ func (a *Application) Start() {
http.Redirect(w, r, redirectPath, http.StatusTemporaryRedirect)
}
}
r.Get(path, redirFn(redirectPath))
router.HandleFunc("GET "+path, redirFn(redirectPath))
}

// Registration renderers
Expand Down Expand Up @@ -187,17 +186,17 @@ func (a *Application) Start() {
rend.RenderFn(w, r, nil)
}
}
r.Get(path, renderFn(path, rend))
router.HandleFunc("GET "+path, renderFn(path, rend))
}

// Delete Team member
r.Get("/register/teacher/team/delete", a.HandleTeacherDeleteMember)
router.HandleFunc("GET /register/teacher/team/delete", a.HandleTeacherDeleteMember)

// Email confirmation code handling
r.Get("/register/teacher/emaillogin", a.HandleTeacherEmailLogin)
router.HandleFunc("GET /register/teacher/emaillogin", a.HandleTeacherEmailLogin)

// Logout
r.Get("/register/teacher/logout", a.HandleTeacherLogout)
router.HandleFunc("GET /register/teacher/logout", a.HandleTeacherLogout)

// Form Post Handlers
formHandlers := map[string]func(w http.ResponseWriter, r *http.Request){
Expand All @@ -215,41 +214,39 @@ func (a *Application) Start() {
handler(w, r)
}
}
r.Post(path, renderFn(fn))
router.HandleFunc("POST "+path, renderFn(fn))
}

// Admin pages
r.Get("/admin", a.ServeTemplate(a.Log, "adminhome.html", noArgs))
r.Get("/admin/login", a.ServeTemplate(a.Log, "adminlogin.html", noArgs))
r.Get("/admin/emaillogin", a.HandleAdminEmailLogin)
r.Post("/admin/emaillogin", a.HandleAdminLogin)
r.Get("/admin/resendstudentemail", a.HandleResendStudentEmail)
r.Get("/admin/resendparentemail", a.HandleResendParentEmail)
r.Get("/admin/confirmationlink/student", a.HandleGetStudentEmailConfirmationLink)
r.Get("/admin/confirmationlink/parent", a.HandleGetParentEmailConfirmationLink)
r.Get("/admin/dietaryrestrictions", a.ServeTemplate(a.Log, "admindietaryrestrictions.html", a.GetAdminDietaryRestrictionsTemplate))
r.Get("/admin/teams", a.ServeTemplate(a.Log, "adminteams.html", a.GetAdminTeamsTemplate))
r.Get("/admin/sendemailconfirmationreminders", a.HandleSendEmailConfirmationReminders)
r.Get("/admin/sendparentreminders", a.HandleSendParentReminders)
r.Get("/admin/sendqrcodes", a.HandleSendQRCodes)
r.Get("/admin/kattis/participants", a.HandleKattisParticipantsExport)
r.Get("/admin/kattis/teams", a.HandleKattisTeamsExport)
r.Get("/admin/zoom/breakout", a.HandleZoomBreakoutExport)
router.HandleFunc("GET /admin", a.ServeTemplate(a.Log, "adminhome.html", noArgs))
router.HandleFunc("GET /admin/login", a.ServeTemplate(a.Log, "adminlogin.html", noArgs))
router.HandleFunc("GET /admin/emaillogin", a.HandleAdminEmailLogin)
router.HandleFunc("POST /admin/emaillogin", a.HandleAdminLogin)
router.HandleFunc("GET /admin/resendstudentemail", a.HandleResendStudentEmail)
router.HandleFunc("GET /admin/resendparentemail", a.HandleResendParentEmail)
router.HandleFunc("GET /admin/confirmationlink/student", a.HandleGetStudentEmailConfirmationLink)
router.HandleFunc("GET /admin/confirmationlink/parent", a.HandleGetParentEmailConfirmationLink)
router.HandleFunc("GET /admin/dietaryrestrictions", a.ServeTemplate(a.Log, "admindietaryrestrictions.html", a.GetAdminDietaryRestrictionsTemplate))
router.HandleFunc("GET /admin/teams", a.ServeTemplate(a.Log, "adminteams.html", a.GetAdminTeamsTemplate))
router.HandleFunc("GET /admin/sendemailconfirmationreminders", a.HandleSendEmailConfirmationReminders)
router.HandleFunc("GET /admin/sendparentreminders", a.HandleSendParentReminders)
router.HandleFunc("GET /admin/sendqrcodes", a.HandleSendQRCodes)
router.HandleFunc("GET /admin/kattis/participants", a.HandleKattisParticipantsExport)
router.HandleFunc("GET /admin/kattis/teams", a.HandleKattisTeamsExport)
router.HandleFunc("GET /admin/zoom/breakout", a.HandleZoomBreakoutExport)

// Volunteer pages
r.Get("/volunteer", a.ServeTemplate(a.Log, "volunteerhome.html", noArgs))
r.Get("/volunteer/login", a.ServeTemplate(a.Log, "volunteerlogin.html", noArgs))
r.Get("/volunteer/emaillogin", a.HandleVolunteerEmailLogin)
r.Post("/volunteer/emaillogin", a.HandleVolunteerLogin)
r.Get("/volunteer/scan", a.ServeTemplate(a.Log, "volunteerscan.html", a.GetVolunteerScanTemplate))
r.Get("/volunteer/checkin", a.HandleVolunteerCheckIn)

var handler http.Handler = r
router.HandleFunc("GET /volunteer", a.ServeTemplate(a.Log, "volunteerhome.html", noArgs))
router.HandleFunc("GET /volunteer/login", a.ServeTemplate(a.Log, "volunteerlogin.html", noArgs))
router.HandleFunc("GET /volunteer/emaillogin", a.HandleVolunteerEmailLogin)
router.HandleFunc("POST /volunteer/emaillogin", a.HandleVolunteerLogin)
router.HandleFunc("GET /volunteer/scan", a.ServeTemplate(a.Log, "volunteerscan.html", a.GetVolunteerScanTemplate))
router.HandleFunc("GET /volunteer/checkin", a.HandleVolunteerCheckIn)

var handler http.Handler = router
handler = hlog.RequestIDHandler("request_id", "RequestID")(handler)
handler = hlog.NewHandler(*a.Log)(handler)

http.Handle("/", r)

a.Log.Info().Msg("Listening on port 8090")
http.ListenAndServe(":8090", handler)
}

0 comments on commit 9eab670

Please sign in to comment.