numerus/pkg/router.go

27 lines
688 B
Go

package pkg
import (
"net/http"
)
func NewRouter(db *Db) http.Handler {
router := http.NewServeMux()
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
router.Handle("/login", LoginHandler())
router.Handle("/logout", LogoutHandler())
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
user := getUser(r)
if user.LoggedIn {
renderTemplate(w, r, "index.html", nil)
} else {
http.Redirect(w, r, "/login", http.StatusSeeOther)
}
})
var handler http.Handler = router
handler = Locale(handler)
handler = CheckLogin(db, handler)
handler = Recoverer(handler)
handler = Logger(handler)
return handler
}