Skip to content

Commit

Permalink
refactor: Create middleware pacakge (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonjek authored Nov 22, 2024
1 parent b06a605 commit 2d95027
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package web
package middleware

import (
"log"
Expand All @@ -11,20 +11,6 @@ type wrappedWriter struct {
statusCode int
}

type Middleware func(http.Handler) http.Handler

func CreateMiddlewareStack(ms ...Middleware) Middleware {
return func(next http.Handler) http.Handler {
// Call from the end of the stack to the beginning
for i := len(ms) - 1; i >= 0; i-- {
x := ms[i]
next = x(next)
}

return next
}
}

func (w *wrappedWriter) WriteHeader(sc int) {
w.ResponseWriter.WriteHeader(sc)
w.statusCode = sc
Expand All @@ -44,10 +30,3 @@ func LoggingMiddleware(next http.Handler) http.Handler {
log.Println(wrapped.statusCode, r.Method, r.URL.Path, time.Since(start))
})
}

func DemoMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Millisecond)
next.ServeHTTP(w, r)
})
}
13 changes: 13 additions & 0 deletions internal/web/middleware/slowdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package middleware

import (
"net/http"
"time"
)

func DemoMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Millisecond)
next.ServeHTTP(w, r)
})
}
19 changes: 19 additions & 0 deletions internal/web/middleware/stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package middleware

import (
"net/http"
)

type Middleware func(http.Handler) http.Handler

func CreateMiddlewareStack(ms ...Middleware) Middleware {
return func(next http.Handler) http.Handler {
// Call from the end of the stack to the beginning
for i := len(ms) - 1; i >= 0; i-- {
x := ms[i]
next = x(next)
}

return next
}
}
7 changes: 4 additions & 3 deletions internal/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/a-h/templ"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/notes"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/middleware"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/components"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/page"
"github.com/sonjek/go-templ-htmx-picocss-example/internal/web/templ/view"
Expand Down Expand Up @@ -170,9 +171,9 @@ func (ws Server) Start() {
fmt.Println("Starting web interface on port: 8089")

// Create stack for handle multiple middlewares
middlewares := CreateMiddlewareStack(
LoggingMiddleware,
DemoMiddleware,
middlewares := middleware.CreateMiddlewareStack(
middleware.LoggingMiddleware,
middleware.DemoMiddleware,
)

server := &http.Server{
Expand Down

0 comments on commit 2d95027

Please sign in to comment.