Skip to content

Commit 6927ea1

Browse files
authored
Restore accidentally removed middleware (#25)
1 parent a300f68 commit 6927ea1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

handlers/ListenAndServe.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"bdo-rest-api/config"
10+
"bdo-rest-api/middleware"
1011
)
1112

1213
func ListenAndServe() {
@@ -20,10 +21,14 @@ func ListenAndServe() {
2021
mux.HandleFunc("GET /v1/guild/search", getGuildSearch)
2122
mux.HandleFunc("/", catchall)
2223

24+
middlewareStack := middleware.CreateStack(
25+
middleware.SetHeaders,
26+
)
27+
2328
log.Println("Listening for requests")
2429
srv := &http.Server{
2530
Addr: fmt.Sprintf("0.0.0.0:%v", config.GetPort()),
26-
Handler: mux,
31+
Handler: middlewareStack(mux),
2732
IdleTimeout: 60 * time.Second,
2833
ReadTimeout: 15 * time.Second,
2934
WriteTimeout: 15 * time.Second,

middleware/CreateStack.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package middleware
2+
3+
import "net/http"
4+
5+
type Middleware func(http.Handler) http.Handler
6+
7+
func CreateStack(xs ...Middleware) Middleware {
8+
return func(next http.Handler) http.Handler {
9+
for i := len(xs) - 1; i >= 0; i-- {
10+
x := xs[i]
11+
next = x(next)
12+
}
13+
14+
return next
15+
}
16+
}

0 commit comments

Comments
 (0)