numerus/pkg/router.go

41 lines
1.3 KiB
Go

package pkg
import (
"net/http"
)
func NewRouter(db *Db) http.Handler {
companyRouter := http.NewServeMux()
companyRouter.Handle("/tax-details", CompanyTaxDetailsHandler())
companyRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
mustRenderAppTemplate(w, r, "dashboard.html", nil)
})
router := http.NewServeMux()
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static"))))
router.Handle("/login", LoginHandler())
router.Handle("/logout", Authenticated(LogoutHandler()))
router.Handle("/profile", Authenticated(ProfileHandler()))
router.Handle("/company/", Authenticated(http.StripPrefix("/company/", CompanyHandler(companyRouter))))
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
user := getUser(r)
if user.LoggedIn {
conn := getConn(r)
var slug string
err := conn.QueryRow(r.Context(), "select slug::text from company order by company_id limit 1").Scan(&slug)
if err != nil {
panic(err)
}
http.Redirect(w, r, "/company/"+slug, http.StatusFound)
} else {
http.Redirect(w, r, "/login", http.StatusSeeOther)
}
})
var handler http.Handler = router
handler = SetLocale(db, handler)
handler = CheckLogin(db, handler)
handler = Recoverer(handler)
handler = Logger(handler)
return handler
}