Implement review suggestions

This commit is contained in:
Daniel 2021-01-06 13:34:25 +01:00
parent 5daeac8cf7
commit 3244fefd43
10 changed files with 118 additions and 111 deletions

View file

@ -2,6 +2,7 @@ package api
import (
"context"
"errors"
"net/http"
"sync"
"time"
@ -127,21 +128,17 @@ func (mh *mainHandler) handle(w http.ResponseWriter, r *http.Request) error {
}
// Check authentication.
token := authenticateRequest(lrw, r, handler)
if token == nil {
apiRequest.AuthToken = authenticateRequest(lrw, r, handler)
if apiRequest.AuthToken == nil {
// Authenticator already replied.
return nil
}
apiRequest.AuthToken = &AuthToken{
Read: token.Read,
Write: token.Write,
}
// Handle request.
switch {
case handler != nil:
handler.ServeHTTP(lrw, r)
case match.MatchErr == mux.ErrMethodMismatch:
case errors.Is(match.MatchErr, mux.ErrMethodMismatch):
http.Error(lrw, "Method not allowed.", http.StatusMethodNotAllowed)
default: // handler == nil or other error
http.Error(lrw, "Not found.", http.StatusNotFound)