-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
53 lines (47 loc) · 1.1 KB
/
api.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
package main
import (
"log"
"net/http"
)
type api struct {
p *proxy
}
func newApi(p *proxy) *api {
return &api{p: p}
}
// allowAction register a regex in whitelist
func (api *api) allowAction(w http.ResponseWriter, r *http.Request) {
rgx := r.PostFormValue("regex")
if rgx == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
err := api.p.addToEndpointList(rgx, true)
if err == nil {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
}
}
// forbid action register a regex in blacklist
func (api *api) forbidAction(w http.ResponseWriter, r *http.Request) {
rgx := r.PostFormValue("regex")
if rgx == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
err := api.p.addToEndpointList(rgx, false)
if err == nil {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
}
}
// run set handlers and launch api
func (api *api) run(port string) {
http.HandleFunc("/allow", api.allowAction)
http.HandleFunc("/forbid", api.forbidAction)
log.Fatal(http.ListenAndServe(":"+port, nil))
}