Refactor routing and middleware architecture

This commit is contained in:
bronku 2025-03-22 11:22:45 +01:00
parent 08d41b0348
commit fb97ba20c0
5 changed files with 62 additions and 51 deletions

View file

@ -17,8 +17,8 @@ type Authenticator struct {
sessions map[string]token
}
func New() Authenticator {
return Authenticator{sessions: make(map[string]token)}
func New() *Authenticator {
return &Authenticator{sessions: make(map[string]token)}
}
func (a *Authenticator) login(w http.ResponseWriter, r *http.Request) {
@ -55,8 +55,8 @@ func (a *Authenticator) login(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound)
}
func (a *Authenticator) Authenticate(in http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
func (a *Authenticator) Middleware(in http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.String() == "/login" {
a.login(w, r)
return
@ -73,6 +73,6 @@ func (a *Authenticator) Authenticate(in http.HandlerFunc) http.HandlerFunc {
}
value.lastAccess = time.Now()
a.sessions[c.Value] = value
in(w, r)
}
in.ServeHTTP(w, r)
})
}