Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperkha committed Mar 14, 2024
1 parent 27bf25f commit 1911ffd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 8 additions & 8 deletions internal/server/middleware.go → internal/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package server
import "net/http"

type HandlerWithCtx struct {
w http.ResponseWriter
r *http.Request
res http.ResponseWriter
req *http.Request
name string
}

Expand All @@ -14,14 +14,14 @@ func NewHandler(f HandlerFunc) HandlerFunc {
return f
}

type Middleware func(HandlerFunc) HandlerFunc

func NewMiddleware(m Middleware) Middleware {
return m
}

func ToHttpHandler(f HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
f(HandlerWithCtx{w, r, ""}) // Create empty base context
}
}

type Middleware func(HandlerFunc) HandlerFunc

func NewMiddleware(m Middleware) Middleware {
return m
}
4 changes: 3 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ func (s *Server) Run(addr string) error {

func (s *Server) MountHandlers() {

// Create simple base handler using a context
handler := NewHandler(func(hwc HandlerWithCtx) {
hwc.w.Write([]byte("Hello " + hwc.name))
hwc.res.Write([]byte("Hello " + hwc.name))
})

// Create middleware that writes to the context before calling the handler
middleware := NewMiddleware(func(hf HandlerFunc) HandlerFunc {
return func(hwc HandlerWithCtx) {
hwc.name = "John"
Expand Down

0 comments on commit 1911ffd

Please sign in to comment.