forked from LukeMauldin/rego
/
rego.go
112 lines (92 loc) · 2.83 KB
/
rego.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
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"encoding/json"
"net/http"
"appengine"
)
type MatchInput struct {
Expr string `datastore:",noindex"`
Text string `datastore:",noindex"`
NumMatches int `datastore:",noindex"`
}
// MatchResultResponse is a json type
type MatchResultResponse struct {
Matches [][]string `json:"matches"`
GroupsName []string `json:"groupsName"`
}
func handlerEvalRegexp(rw http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
inputData := &MatchInput{}
err := json.NewDecoder(req.Body).Decode(inputData)
if err != nil {
ctx.Errorf("Error parsing input: %v", err)
http.Error(rw, "Error parsing input", http.StatusInternalServerError)
return
}
result, err := EvalRegex(ctx, inputData)
if err != nil {
ctx.Errorf("%v", err)
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
err = json.NewEncoder(rw).Encode(result)
if err != nil {
ctx.Errorf("JSON encoding err: %v", err)
http.Error(rw, "JSON encoding error", http.StatusInternalServerError)
return
}
}
func handlerShareRegexp(rw http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
inputData := &MatchInput{}
err := json.NewDecoder(req.Body).Decode(inputData)
if err != nil {
ctx.Errorf("Error parsing input: %v", err)
http.Error(rw, "Error parsing input", http.StatusInternalServerError)
return
}
result, err := CreatePermLink(ctx, inputData)
if err != nil {
ctx.Errorf("%v", err)
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
_, err = rw.Write([]byte(result))
if err != nil {
ctx.Errorf("%v", err)
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
}
func handlerLoadRegexp(rw http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
key := req.URL.Query().Get("key")
if key == "" {
http.RedirectHandler("/assets/html/index.html", http.StatusMovedPermanently)
return
}
matchInput, err := RetrievePermLink(ctx, key)
if err != nil {
ctx.Errorf("%v", err)
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
//TODO: figure out how to get loadRegexp results to evaluate and display to user (need to replace defaults of $scope.regexpInput)
//TODO: OR have JS type the presence of ?key= and replace the defaults on page load
err = json.NewEncoder(rw).Encode(matchInput)
if err != nil {
ctx.Errorf("JSON encoding err: %v", err)
http.Error(rw, "JSON encoding error", http.StatusInternalServerError)
return
}
}
func init() {
// Regex eval service
http.HandleFunc("/eval_regexp/", handlerEvalRegexp)
//Regex share service
http.HandleFunc("/share_regexp/", handlerShareRegexp)
//Regex load share service
http.HandleFunc("/load_regexp", handlerLoadRegexp)
// Main handler (index.html)
http.Handle("/", http.RedirectHandler("/assets/html/index.html", http.StatusMovedPermanently))
}