|
| 1 | +# Convert Middleware |
| 2 | + |
| 3 | +For this example, i use `https://github.com/didip/tollbooth` |
| 4 | + |
| 5 | +- Create `rate_limit.go` inside `middlewares` folder (you can create when folder not exists) |
| 6 | + |
| 7 | +- Copas placeholder code |
| 8 | + |
| 9 | +```go |
| 10 | +package middlewares |
| 11 | + |
| 12 | +import ( |
| 13 | + "net/http" |
| 14 | +) |
| 15 | + |
| 16 | +type RateLimit struct { |
| 17 | +} |
| 18 | + |
| 19 | +func (r *RateLimit) Attach(request *http.Request, response http.ResponseWriter) bool { |
| 20 | +} |
| 21 | + |
| 22 | +func (r *RateLimit) Priority() int { |
| 23 | + return 0 |
| 24 | +} |
| 25 | + |
| 26 | +``` |
| 27 | + |
| 28 | +- Look up to the handler function |
| 29 | + |
| 30 | +From readme, we know when we want to use `https://github.com/didip/tollbooth`, we just add one line like below |
| 31 | + |
| 32 | +```go |
| 33 | +tollbooth.LimitFuncHandler(tollbooth.NewLimiter(1, nil), HelloHandler) |
| 34 | +``` |
| 35 | + |
| 36 | +See inside `tollbooth.LimitFuncHandler()` and here the code |
| 37 | + |
| 38 | +```go |
| 39 | +middle := func(w http.ResponseWriter, r *http.Request) { |
| 40 | + httpError := LimitByRequest(lmt, w, r) |
| 41 | + if httpError != nil { |
| 42 | + lmt.ExecOnLimitReached(w, r) |
| 43 | + if lmt.GetOverrideDefaultResponseWriter() { |
| 44 | + return |
| 45 | + } |
| 46 | + w.Header().Add("Content-Type", lmt.GetMessageContentType()) |
| 47 | + w.WriteHeader(httpError.StatusCode) |
| 48 | + w.Write([]byte(httpError.Message)) |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + // There's no rate-limit error, serve the next handler. |
| 53 | + next.ServeHTTP(w, r) |
| 54 | +} |
| 55 | + |
| 56 | +return http.HandlerFunc(middle) |
| 57 | +``` |
| 58 | + |
| 59 | +- Find `func(w http.ResponseWriter, r *http.Request)` function and copas the body to your middleware, like below (i change variable names) |
| 60 | + |
| 61 | +```go |
| 62 | +limiter := tollbooth.NewLimiter(1, nil) |
| 63 | +httpError := tollbooth.LimitByRequest(limiter, response, request) |
| 64 | +if httpError != nil { |
| 65 | + limiter.ExecOnLimitReached(response, request) |
| 66 | + if limiter.GetOverrideDefaultResponseWriter() { |
| 67 | + return true |
| 68 | + } |
| 69 | + |
| 70 | + response.Header().Add("Content-Type", limiter.GetMessageContentType()) |
| 71 | + response.WriteHeader(httpError.StatusCode) |
| 72 | + response.Write([]byte(httpError.Message)) |
| 73 | + |
| 74 | + return true |
| 75 | +} |
| 76 | + |
| 77 | +return false |
| 78 | +``` |
| 79 | + |
| 80 | +Here full code |
| 81 | + |
| 82 | +```go |
| 83 | +package middlewares |
| 84 | + |
| 85 | +import ( |
| 86 | + "net/http" |
| 87 | + |
| 88 | + "github.com/didip/tollbooth/v6" |
| 89 | +) |
| 90 | + |
| 91 | +type RateLimit struct { |
| 92 | +} |
| 93 | + |
| 94 | +func (r *RateLimit) Attach(request *http.Request, response http.ResponseWriter) bool { |
| 95 | + limiter := tollbooth.NewLimiter(1, nil) |
| 96 | + httpError := tollbooth.LimitByRequest(limiter, response, request) |
| 97 | + if httpError != nil { |
| 98 | + limiter.ExecOnLimitReached(response, request) |
| 99 | + if limiter.GetOverrideDefaultResponseWriter() { |
| 100 | + return true |
| 101 | + } |
| 102 | + |
| 103 | + response.Header().Add("Content-Type", limiter.GetMessageContentType()) |
| 104 | + response.WriteHeader(httpError.StatusCode) |
| 105 | + response.Write([]byte(httpError.Message)) |
| 106 | + |
| 107 | + return true |
| 108 | + } |
| 109 | + |
| 110 | + return false |
| 111 | +} |
| 112 | + |
| 113 | +func (r *RateLimit) Priority() int { |
| 114 | + return 0 |
| 115 | +} |
| 116 | + |
| 117 | +``` |
| 118 | + |
| 119 | +- Add definition to `dics/container.go` |
| 120 | + |
| 121 | +```go |
| 122 | +{ |
| 123 | + Name: "bima:middleware:rate-limiter", |
| 124 | + Scope: bima.Application, |
| 125 | + Build: (*middlewares.RateLimit)(nil), |
| 126 | +}, |
| 127 | +``` |
| 128 | + |
| 129 | +- Add to `configs/middlewares.yaml` |
| 130 | + |
| 131 | +```yaml |
| 132 | +middlewares: |
| 133 | + - rate-limiter |
| 134 | +``` |
| 135 | +
|
| 136 | +- Add `max` value optional |
| 137 | + |
| 138 | +```go |
| 139 | +type RateLimit struct { |
| 140 | + Max float64 |
| 141 | +} |
| 142 | +
|
| 143 | +func (r *RateLimit) Attach(request *http.Request, response http.ResponseWriter) bool { |
| 144 | + limiter := tollbooth.NewLimiter(r.Max, nil) |
| 145 | + httpError := tollbooth.LimitByRequest(limiter, response, request) |
| 146 | + if httpError != nil { |
| 147 | + limiter.ExecOnLimitReached(response, request) |
| 148 | + if limiter.GetOverrideDefaultResponseWriter() { |
| 149 | + return true |
| 150 | + } |
| 151 | +
|
| 152 | + response.Header().Add("Content-Type", limiter.GetMessageContentType()) |
| 153 | + response.WriteHeader(httpError.StatusCode) |
| 154 | + response.Write([]byte(httpError.Message)) |
| 155 | +
|
| 156 | + return true |
| 157 | + } |
| 158 | +
|
| 159 | + return false |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +- Change definition |
| 164 | + |
| 165 | +```go |
| 166 | +{ |
| 167 | + Name: "bima:middleware:rate-limiter", |
| 168 | + Scope: bima.Application, |
| 169 | + Build: (*middlewares.RateLimit)(nil), |
| 170 | + Params: dingo.Params{ |
| 171 | + "Max": float64(1), |
| 172 | + }, |
| 173 | +}, |
| 174 | +``` |
| 175 | + |
| 176 | + |
0 commit comments