-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.go
More file actions
155 lines (139 loc) · 4.45 KB
/
server.go
File metadata and controls
155 lines (139 loc) · 4.45 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package internal
import (
"fmt"
"html"
"io"
"net/http"
"sort"
"strings"
"github.com/CAFxX/httpcompression"
)
func Server(addr string) error {
compress, _ := httpcompression.DefaultAdapter()
examples := GetCommonRegex()
var exampleNames []string
for k := range examples {
exampleNames = append(exampleNames, k)
}
sort.Strings(exampleNames)
var bodyExamples string
for _, name := range exampleNames {
bodyExamples += fmt.Sprintf(`
<a href='' onclick='document.querySelector("#regex").value = "%[2]s"; return false'>%[1]s</a>,`,
strings.TrimSuffix(name, "Pattern"),
html.EscapeString(examples[name]),
)
}
bodyExamples = bodyExamples[:len(bodyExamples)-1]
body := `
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<title>regexp2go demo</title>
</head>
<body>
<div class=container>
<div class=row>
<div class="col py-3">
<h1><a href="https://github.com/CAFxX/regexp2go">regexp2go</a> demo</h1>
<p>
Compile <a href="https://golang.org/pkg/regexp/syntax/">Go regular expressions</a> to Go code.
</p>
</div>
</div>
<div class=row>
<div class="col py-3">
<form method=post action="/generate">
<div class="input-group">
<input type=text name=regex id=regex placeholder=Regexp required=required class=form-control>
<button type=submit name=submit value=raw class="btn btn-primary">Compile</button>
<button type=submit name=submit value=ce class="btn btn-secondary">Open in Compiler Explorer</button>
</div>
</form>
</div>
</div>
<div class=row>
<div class="col py-3">
<h2>Examples</h2>
<p>Regular expressions from <a href="https://github.com/mingrammer/commonregex">mingrammer/commonregex</a>: ` + bodyExamples + `</p>
<h2>Notes</h2>
<p>
The generated code generally runs faster than the corresponding regexp run by the native
implementation. The speedup depends on the regular expression as well as the input data,
but it is generally in the +35% to +500% range.
</p>
<p>
Warning: regexp2go is alpha quality.
The code generated by regexp2go should currently only be used for demonstration purposes.
</p>
<p>
See <a href="https://github.com/CAFxX/regexp2go">github.com/CAFxX/regexp2go</a> for details.
</p>
</div>
</div>
</div>
</body>
</html>`
http.Handle("/", compress(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
errorResponse(w, http.StatusNotFound, "")
return
}
if r.Method != http.MethodGet {
errorResponse(w, http.StatusMethodNotAllowed, "")
return
}
io.WriteString(w, body)
})))
http.Handle("/generate", compress(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
errorResponse(w, http.StatusMethodNotAllowed, "method not allowed: %s", r.Method)
return
}
if err := r.ParseForm(); err != nil {
errorResponse(w, http.StatusBadRequest, "bad request")
return
}
regex := r.Form.Get("regex")
if regex == "" {
errorResponse(w, http.StatusUnprocessableEntity, "invalid request: empty regexp")
return
}
if len(regex) > 2000 {
errorResponse(w, http.StatusUnprocessableEntity, "invalid request: regexp is too long for demo")
return
}
usePool := r.Form.Get("submit") != "ce"
res, err := Generate(regex, "regexp2go_demo", "Match", 212, usePool)
if err != nil {
errorResponse(w, http.StatusUnprocessableEntity, "generate: %v\n", err)
return
}
if r.Form.Get("submit") == "ce" {
url, err := OpenInCompilerExplorer(string(res))
if err != nil {
errorResponse(w, http.StatusUnprocessableEntity, "open in compiler explorer: %v\n", err)
return
}
w.Header().Set("Location", url)
w.WriteHeader(http.StatusSeeOther)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write(res)
})))
return http.ListenAndServe(addr, nil)
}
func errorResponse(w http.ResponseWriter, status int, msg string, args ...interface{}) {
w.WriteHeader(status)
if msg == "" {
return
}
fmt.Fprintf(w, msg, args...)
if !strings.HasSuffix(msg, "\n") {
io.WriteString(w, "\n")
}
}