Обновление mux
This commit is contained in:
parent
376839b264
commit
5094ebf213
4 changed files with 79 additions and 21 deletions
20
group.go
20
group.go
|
@ -1,18 +1,8 @@
|
||||||
package mux
|
package mux
|
||||||
|
|
||||||
import (
|
func (m *Mux) Group(prefix string, group func(sm *Mux), middlewares ...Middleware) {
|
||||||
"net/http"
|
child := New()
|
||||||
"strings"
|
group(child)
|
||||||
)
|
child.Use(middlewares...)
|
||||||
|
m.groups[prefix] = child
|
||||||
func Group(mux *http.ServeMux, prefix string, group func(sm *http.ServeMux), middlewares ...Middleware) {
|
|
||||||
groupMux := http.NewServeMux()
|
|
||||||
group(groupMux)
|
|
||||||
if !strings.HasSuffix(prefix, "/") {
|
|
||||||
prefix += "/"
|
|
||||||
}
|
|
||||||
mux.Handle(
|
|
||||||
prefix,
|
|
||||||
http.StripPrefix(strings.TrimSuffix(prefix, "/"), Use(groupMux, middlewares...)),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,10 +4,6 @@ import "net/http"
|
||||||
|
|
||||||
type Middleware func(http.Handler) http.Handler
|
type Middleware func(http.Handler) http.Handler
|
||||||
|
|
||||||
func Use(handler http.Handler, middlewares ...Middleware) http.Handler {
|
func (m *Mux) Use(middlewares ...Middleware) {
|
||||||
for _, h := range middlewares {
|
m.middlewares = append(m.middlewares, middlewares...)
|
||||||
handler = h(handler)
|
|
||||||
}
|
|
||||||
|
|
||||||
return handler
|
|
||||||
}
|
}
|
||||||
|
|
44
middleware/basic_auth.go
Normal file
44
middleware/basic_auth.go
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"go.neonxp.ru/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
const basicAuthScheme = "Basic"
|
||||||
|
|
||||||
|
type BasicAuthConfig struct {
|
||||||
|
Skipper func(r *http.Request) bool
|
||||||
|
Realm string
|
||||||
|
Validator func(r *http.Request, login, password string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultSkipper(*http.Request) bool {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func BasicAuth(config BasicAuthConfig) mux.Middleware {
|
||||||
|
return func(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if config.Skipper(r) {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
authString := r.Header.Get("Authorization")
|
||||||
|
if authString == "" {
|
||||||
|
w.Header().Set("WWW-Authenticate", fmt.Sprintf(`%s realm="%s", charset="UTF-8"`, basicAuthScheme, config.Realm))
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
parts := strings.SplitN(authString, " ", 2)
|
||||||
|
if strings.EqualFold(parts[0], basicAuthScheme) {
|
||||||
|
w.Header().Set("WWW-Authenticate", fmt.Sprintf(`%s realm="%s", charset="UTF-8"`, basicAuthScheme, config.Realm))
|
||||||
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
28
mux.go
Normal file
28
mux.go
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package mux
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
type Mux struct {
|
||||||
|
http.ServeMux
|
||||||
|
middlewares []Middleware
|
||||||
|
groups map[string]*Mux
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Mux {
|
||||||
|
return &Mux{
|
||||||
|
ServeMux: http.ServeMux{},
|
||||||
|
middlewares: []Middleware{},
|
||||||
|
groups: map[string]*Mux{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
for path, g := range m.groups {
|
||||||
|
m.Handle(path, g)
|
||||||
|
}
|
||||||
|
h := http.Handler(&m.ServeMux)
|
||||||
|
for _, mw := range m.middlewares {
|
||||||
|
h = mw(h)
|
||||||
|
}
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
}
|
Loading…
Reference in a new issue