Noah Petherbridge
2a8a1df6ab
* Initial codebase (lot of work!) * Uses vanilla Go net/http and implements by hand: session cookies backed by Redis; log in/out; CSRF protection; email verification flow; initial database models (User table)
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
// Package router configures web routes.
|
|
package router
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/config"
|
|
"git.kirsle.net/apps/gosocial/pkg/controller/account"
|
|
"git.kirsle.net/apps/gosocial/pkg/controller/api"
|
|
"git.kirsle.net/apps/gosocial/pkg/controller/index"
|
|
"git.kirsle.net/apps/gosocial/pkg/middleware"
|
|
)
|
|
|
|
func New() http.Handler {
|
|
mux := http.NewServeMux()
|
|
|
|
// Register controller endpoints.
|
|
mux.HandleFunc("/", index.Create())
|
|
mux.HandleFunc("/login", account.Login())
|
|
mux.HandleFunc("/logout", account.Logout())
|
|
mux.HandleFunc("/signup", account.Signup())
|
|
|
|
// Login Required.
|
|
mux.Handle("/me", middleware.LoginRequired(account.Dashboard()))
|
|
|
|
// JSON API endpoints.
|
|
mux.HandleFunc("/v1/version", api.Version())
|
|
mux.HandleFunc("/v1/users/me", api.LoginOK())
|
|
|
|
// Static files.
|
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(config.StaticPath))))
|
|
|
|
// Global middlewares.
|
|
withSession := middleware.Session(mux)
|
|
withCSRF := middleware.CSRF(withSession)
|
|
withRecovery := middleware.Recovery(withCSRF)
|
|
withLogger := middleware.Logging(withRecovery)
|
|
return withLogger
|
|
}
|