-
Notifications
You must be signed in to change notification settings - Fork 39
/
proxy.go
54 lines (45 loc) · 1.28 KB
/
proxy.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package structural
import "net/http"
type server interface {
handleRequest(string, string) (int, string)
}
type nginx struct {
app *application
maxAllowRequest int
rateLimiter map[string]int
}
type application struct {
}
func NewNginxServer(maxReq int) *nginx {
return &nginx{
app: &application{},
maxAllowRequest: maxReq,
rateLimiter: make(map[string]int),
}
}
func (n *nginx) handleRequest(url, method string) (int, string) {
if allowed := n.checkRateLimiting(url); !allowed {
return http.StatusForbidden, http.StatusText(http.StatusForbidden)
}
return n.app.handleRequest(url, method)
}
func (n *nginx) checkRateLimiting(url string) bool {
// In the real environment, n.rateLimiter needs concurrency lock.
if _, ok := n.rateLimiter[url]; !ok {
n.rateLimiter[url] = 1
}
if n.rateLimiter[url] > n.maxAllowRequest {
return false
}
n.rateLimiter[url] = n.rateLimiter[url] + 1
return true
}
func (a *application) handleRequest(url, method string) (int, string) {
if url == "/app/list" && method == "GET" {
return http.StatusOK, http.StatusText(http.StatusOK)
}
if url == "/app/create" && method == "POST" {
return http.StatusCreated, http.StatusText(http.StatusCreated)
}
return http.StatusNotFound, http.StatusText(http.StatusNotFound)
}