From 97aabcf06f85298036a374bda188d731edd8499b Mon Sep 17 00:00:00 2001 From: Haelnorr Date: Tue, 11 Feb 2025 19:37:39 +1100 Subject: [PATCH] Added middleware to let excluded files skip middleware chain --- middleware/authentication.go | 21 +++++++++++++++++++++ middleware/excluded.go | 25 +++++++++++++++++++++++++ middleware/favicon.go | 17 +++++++++++++++++ middleware/logging.go | 14 +++++--------- server/server.go | 7 +++++++ static/favicon.ico | Bin 0 -> 834 bytes 6 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 middleware/authentication.go create mode 100644 middleware/excluded.go create mode 100644 middleware/favicon.go create mode 100644 static/favicon.ico 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 0000000000000000000000000000000000000000..f052478ecf7f97130fc4544300f1d43b16912fde GIT binary patch literal 834 zcmV-I1HJr-P)q(n>kO!KUmw{ z%xSxKGo5$TX;wn&?kz;A>|R0|l@ZdPhs2jKF#3ZJVMh7jlSR-&R+QL75IvNsOw8u$ zdfc71=h}-u__D*ykKcUe_xsJTg0OB`tRz79crdMmm>dvAF)y8uPYi8=_;@K79}BBt zUKH64Fnt9vWtIRUQvefe`4mgiCWy#2fa&a_ADMxeUgc}fH}}i=0MoDb&6j}8LqJ3Z zGW*c`jsXB7H!}scG64}O&*sDB4G`!E0Af061&OPjY-soc>*^g$-C)cbSCIzx;PE&c zcr?x`X@d=vUUrrNOnzBGx_bxN$mkRs8lFhW0{w1{>zX@EAFk-Rc6%A_I?Di}udN_r z^sIUMlS(obpl>17!3b;X?qh9T_sp@aR0*dUIFq#pFd6aIkX;aw-&vY)+Ji$o9RU;< zYa0O~4>JUx^!RDvUr-tah|HyF(m5}t5l5TbAt0is0Vd=8G!WCz0Mi~22ya_bt^hp* zF?k9x`N0J+ec}m{gF-G=qZwNz)p#qb26HBTR#uD~>0+1o>k5_ykQJla`R0od_LyEw z_d`VQ@RxFVfP^mQ4qz}%(>Cx23n2R3-Xl2y9ErEEib!KxrTPL)-luEioB%2!jTx_J zs&6L0bpRok65yuu%-@(ut2HTs{138Uz5fCr*d8B+ll}}%B6jhV$9_T@Fq2` zFBsg+pLby;c1+XvnwgkaIjO&0D#c#-q}a={aQtIgxc*aFDE^MmaUHI2ZdveGjOtMH z#nt&awG3j~pZyn%h=QBuLE)mRSkrZ@ zL9(5`R@IB_YqF~MxSj<`)jMUy=$BRFLh72_n#?!XxDvX%H^D9Y14dl;BI9vo!T