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
package main
import (
"fmt"
"net/http"
)
func authMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
if token == "" {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintln(w, "Authentication required")
return
}
next.ServeHTTP(w, r)
})
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "REST API Response")
}
func main() {
api := http.HandlerFunc(apiHandler)
handler := authMiddleware(api)
http.Handle("/users", handler)
fmt.Println("API Gateway running on :8080")
fmt.Println("Rate Limit: 100 requests/minute")
http.ListenAndServe(":8080", nil)
}Coding works best on desktop or with an external keyboard.