1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-28 08:58:38 +02:00
authboss/defaults/router.go
Aaron L d4f4f6c443 Rewrite auth module
Discovered many problems with the abstractions along the way
and did small fixes to get to the end of the auth module.

- Use more constants for random strings
- Create forcing functions to deal with the upgrades to different
  interfaces
2018-02-04 21:24:55 -08:00

66 lines
1.4 KiB
Go

package defaults
import (
"io"
"net/http"
)
// Router implementation
// Does not use a dynamic map to hope to be slightly more performant
type Router struct {
gets *http.ServeMux
posts *http.ServeMux
deletes *http.ServeMux
}
// NewRouter creates a new router
func NewRouter() *Router {
r := &Router{
gets: http.NewServeMux(),
posts: http.NewServeMux(),
deletes: http.NewServeMux(),
}
// Nothing gets handled at the root of the authboss router
r.gets.Handle("/", http.NotFoundHandler())
r.posts.Handle("/", http.NotFoundHandler())
r.deletes.Handle("/", http.NotFoundHandler())
return r
}
// Get method route
func (r *Router) Get(path string, handler http.Handler) {
r.gets.Handle(path, handler)
}
// Post method route
func (r *Router) Post(path string, handler http.Handler) {
r.posts.Handle(path, handler)
}
// Delete method route
func (r *Router) Delete(path string, handler http.Handler) {
r.deletes.Handle(path, handler)
}
// ServeHTTP for http.Handler
// Only does get/posts, all other request types are a bad request
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var router http.Handler
switch req.Method {
case "GET":
router = r.gets
case "POST":
router = r.posts
case "DELETE":
router = r.deletes
default:
w.WriteHeader(http.StatusMethodNotAllowed)
io.WriteString(w, "method not allowed")
return
}
router.ServeHTTP(w, req)
}