Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"fmt"
"net/http"
)
func authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Auth: validating JWT token")
next.ServeHTTP(w, r)
})
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Log: request received", r.URL.Path)
next.ServeHTTP(w, r)
})
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "API Healthy")
}
func userHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "User API Response")
}
func main() {
api := http.NewServeMux()
api.HandleFunc("/health", healthHandler)
api.HandleFunc("/users", userHandler)
handler := loggingMiddleware(authMiddleware(api))
fmt.Println("Production API running on :8080")
http.ListenAndServe(":8080", handler)
}Coding works best on desktop or with an external keyboard.