Added middleware to let excluded files skip middleware chain
This commit is contained in:
21
middleware/authentication.go
Normal file
21
middleware/authentication.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Take current request
|
||||||
|
// Get cookies from browser
|
||||||
|
// Parse the tokens
|
||||||
|
// Check if tokens blacklisted
|
||||||
|
// Trigger refresh if required
|
||||||
|
// Create context with state of user authorization
|
||||||
|
// Pass request on with context
|
||||||
|
|
||||||
|
func Authentication(logger *zerolog.Logger, next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
|
}
|
||||||
25
middleware/excluded.go
Normal file
25
middleware/excluded.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var excludedFiles = map[string]bool{
|
||||||
|
"/static/css/output.css": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks is path requested if for an excluded file and returns the file
|
||||||
|
// instead of passing the request onto the next middleware
|
||||||
|
func ExcludedFiles(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if excludedFiles[r.URL.Path] {
|
||||||
|
filePath := strings.TrimPrefix(r.URL.Path, "/")
|
||||||
|
http.ServeFile(w, r, filePath)
|
||||||
|
} else {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
17
middleware/favicon.go
Normal file
17
middleware/favicon.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Favicon(next http.Handler) http.Handler {
|
||||||
|
return http.HandlerFunc(
|
||||||
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path == "/favicon.ico" {
|
||||||
|
http.ServeFile(w, r, "static/favicon.ico")
|
||||||
|
} else {
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -2,7 +2,6 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
@@ -29,13 +28,10 @@ func Logging(logger *zerolog.Logger, next http.Handler) http.Handler {
|
|||||||
statusCode: http.StatusOK,
|
statusCode: http.StatusOK,
|
||||||
}
|
}
|
||||||
next.ServeHTTP(wrapped, r)
|
next.ServeHTTP(wrapped, r)
|
||||||
if !strings.Contains(r.URL.Path, "favicon.ico") &&
|
logger.Info().
|
||||||
!strings.Contains(r.URL.Path, "output.css") {
|
Int("status", wrapped.statusCode).
|
||||||
logger.Info().
|
Str("method", r.Method).
|
||||||
Int("status", wrapped.statusCode).
|
Str("resource", r.URL.Path).
|
||||||
Str("method", r.Method).
|
Dur("time_elapsed", time.Since(start)).Msg("Served")
|
||||||
Str("resource", r.URL.Path).
|
|
||||||
Dur("time_elapsed", time.Since(start)).Msg("Served")
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,13 @@ func NewServer(
|
|||||||
conn,
|
conn,
|
||||||
)
|
)
|
||||||
var handler http.Handler = mux
|
var handler http.Handler = mux
|
||||||
|
// Add middleware here, must be added in reverse order of execution
|
||||||
|
// i.e. First in list will get executed last during the request handling
|
||||||
handler = middleware.Logging(logger, handler)
|
handler = middleware.Logging(logger, handler)
|
||||||
|
handler = middleware.Authentication(logger, handler)
|
||||||
|
|
||||||
|
// Serve the favicon and exluded files before any middleware is added
|
||||||
|
handler = middleware.ExcludedFiles(handler)
|
||||||
|
handler = middleware.Favicon(handler)
|
||||||
return handler
|
return handler
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
static/favicon.ico
Normal file
BIN
static/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 834 B |
Reference in New Issue
Block a user