Go
Summary - Middleware
Combine logging and auth middlewares.
Middleware summary
Middleware = func(http.Handler) http.Handler. In this group you wrote wrappers with http.HandlerFunc, built parameterized middleware, saw how order matters in a chain, recorded the status code by wrapping ResponseWriter, and stopped unauthorized requests with 401.
h := zincir(api, recoverMW, logla, yetki)
http.ListenAndServe(":8080", h)
Recover middleware
A panic in a handler doesn't bring down the whole server but cuts the connection. Recover middleware turns it into a 500 response:
func recoverMW(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if v := recover(); v != nil {
http.Error(w, "sunucu hatası", 500)
}
}()
next.ServeHTTP(w, r)
})
}
Exercise Recover middleware
Write recoverMW(next http.Handler) http.Handler: if a panic is caught with defer + recover(), write http.Error(w, "sunucu hatası", 500). Test with a handler that calls panic("x") and print rec.Code.
Show example solution
package main
import (
"fmt"
"net/http"
"net/http/httptest"
)
func recoverMW(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if v := recover(); v != nil {
http.Error(w, "sunucu hatası", 500)
}
}()
next.ServeHTTP(w, r)
})
}
func main() {
h := recoverMW(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { panic("x") }))
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("GET", "/", nil))
fmt.Println(rec.Code)
}
Test yourself
-
Where should panic-catching recover middleware be in the chain?
- After auth
- It doesn't matter
- Outermost
- Innermost
Show answer
Outermost — recover only catches panics in code running inside it; outermost, it protects the whole chain.
-
Catch the panic
defer func() {
if v := ____(); v != nil {
http.Error(w, "hata", 500)
}
}()
Show answer
defer func() {
if v := recover(); v != nil {
http.Error(w, "hata", 500)
}
}()
-
Middleware and role
- recoverMW
- Panic → 500
- logla
- Path, code, duration
- yetki
- Credentials → 401
- zincir
- Wraps in order
Next lesson: What is gRPC?