diff --git a/middleware/authentication.go b/middleware/authentication.go new file mode 100644 index 0000000..de72c46 --- /dev/null +++ b/middleware/authentication.go @@ -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) + }) +} diff --git a/middleware/excluded.go b/middleware/excluded.go new file mode 100644 index 0000000..cc31749 --- /dev/null +++ b/middleware/excluded.go @@ -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) + } + }, + ) +} diff --git a/middleware/favicon.go b/middleware/favicon.go new file mode 100644 index 0000000..41385fa --- /dev/null +++ b/middleware/favicon.go @@ -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) + } + }, + ) +} diff --git a/middleware/logging.go b/middleware/logging.go index 3b8307c..ef37c5c 100644 --- a/middleware/logging.go +++ b/middleware/logging.go @@ -2,7 +2,6 @@ package middleware import ( "net/http" - "strings" "time" "github.com/rs/zerolog" @@ -29,13 +28,10 @@ func Logging(logger *zerolog.Logger, next http.Handler) http.Handler { statusCode: http.StatusOK, } next.ServeHTTP(wrapped, r) - if !strings.Contains(r.URL.Path, "favicon.ico") && - !strings.Contains(r.URL.Path, "output.css") { - logger.Info(). - Int("status", wrapped.statusCode). - Str("method", r.Method). - Str("resource", r.URL.Path). - Dur("time_elapsed", time.Since(start)).Msg("Served") - } + logger.Info(). + Int("status", wrapped.statusCode). + Str("method", r.Method). + Str("resource", r.URL.Path). + Dur("time_elapsed", time.Since(start)).Msg("Served") }) } diff --git a/server/server.go b/server/server.go index d0a1fe2..49896d8 100644 --- a/server/server.go +++ b/server/server.go @@ -24,6 +24,13 @@ func NewServer( conn, ) 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.Authentication(logger, handler) + + // Serve the favicon and exluded files before any middleware is added + handler = middleware.ExcludedFiles(handler) + handler = middleware.Favicon(handler) return handler } diff --git a/static/favicon.ico b/static/favicon.ico new file mode 100644 index 0000000..f052478 Binary files /dev/null and b/static/favicon.ico differ