-
Notifications
You must be signed in to change notification settings - Fork 4
/
rules.go
101 lines (75 loc) · 2.33 KB
/
rules.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package web
import (
"encoding/json"
"fmt"
"net/http"
"path"
"strconv"
"github.com/asalih/http-auto-responder/responder"
"github.com/asalih/http-auto-responder/utils"
)
func init() {
HTTPHandlerFuncs["/http-auto-responder/save-rule"] = func(w http.ResponseWriter, r *http.Request, ar responder.AutoResponder) {
w.Header().Set("Content-Type", "application/json")
decoder := json.NewDecoder(r.Body)
var rule responder.Rule
err := decoder.Decode(&rule)
if err != nil {
panic(err)
}
rule.IsActive = true
ar.AddOrUpdateRule(&rule)
w.Write([]byte(`{"status": "OK"}`))
}
HTTPHandlerFuncs["/http-auto-responder/get-rules"] = func(w http.ResponseWriter, r *http.Request, ar responder.AutoResponder) {
w.Header().Set("Content-Type", "application/json")
rules := ar.GetRules()
encoder := json.NewEncoder(w)
encoder.Encode(rules)
}
HTTPHandlerFuncs["/http-auto-responder/get-rule"] = func(w http.ResponseWriter, r *http.Request, ar responder.AutoResponder) {
w.Header().Set("Content-Type", "application/json")
idq := r.URL.Query().Get("id")
id, err := strconv.ParseUint(idq, 0, 64)
if err != nil || idq == "" {
http.NotFound(w, r)
return
}
rule := ar.GetRule(id)
encoder := json.NewEncoder(w)
encoder.Encode(rule)
}
HTTPHandlerFuncs["/http-auto-responder/remove-rule"] = func(w http.ResponseWriter, r *http.Request, ar responder.AutoResponder) {
w.Header().Set("Content-Type", "application/json")
idq := r.URL.Query().Get("id")
id, err := strconv.ParseUint(idq, 0, 64)
if err != nil || idq == "" {
http.NotFound(w, r)
return
}
ar.RemoveRule(id)
w.Write([]byte(`{"status": "OK"}`))
}
HTTPHandlerFuncs["/http-auto-responder/reload"] = func(w http.ResponseWriter, r *http.Request, ar responder.AutoResponder) {
w.Header().Set("Content-Type", "application/json")
if utils.Configuration.FarxFilesFolderPath == "" {
w.Write([]byte(`{"status": "NA"}`))
return
}
pq := r.URL.Query().Get("path")
if path.Ext(pq) != ".farx" {
w.Write([]byte(`{"status": "Unexpected extension"}`))
return
}
data := ar.(*responder.FarxAutoResponder)
success, err := data.Reload(utils.Configuration.FarxFilesFolderPath + "/" + pq)
if err != nil {
fmt.Println(err)
}
if success {
w.Write([]byte(`{"status": "OK"}`))
} else {
w.Write([]byte(`{"status": "Err"}`))
}
}
}