@@ -3,12 +3,14 @@ package main
3
3
import (
4
4
"crypto/hmac"
5
5
"crypto/sha256"
6
+ "embed"
6
7
"encoding/base64"
7
8
"encoding/hex"
8
9
"encoding/json"
9
10
"errors"
10
11
"flag"
11
12
"fmt"
13
+ "html/template"
12
14
"io/ioutil"
13
15
"log"
14
16
"net/http"
@@ -17,7 +19,8 @@ import (
17
19
"time"
18
20
)
19
21
20
- //go:generate go run gentmpl.go form success
22
+ //go:embed form.html success.html
23
+ var templateFS embed.FS
21
24
22
25
var (
23
26
// Generate using e.g. “openssl rand -hex 32”.
39
42
"host:port to listen on for HTTP requests" )
40
43
)
41
44
45
+ var templates = template .Must (template .New ("" ).ParseFS (templateFS , "*.html" ))
46
+
42
47
func verifyCaptcha (response string ) error {
43
48
resp , err := http .PostForm ("https://www.google.com/recaptcha/api/siteverify" ,
44
49
url.Values {
@@ -131,7 +136,7 @@ func fromHTTP(r *http.Request) (request, error) {
131
136
}
132
137
133
138
func writeForm (w http.ResponseWriter , msg string ) {
134
- if err := formTpl . Execute ( w , struct {
139
+ if err := templates . ExecuteTemplate ( w , "form.html" , struct {
135
140
SiteKey string
136
141
Msg string
137
142
}{
@@ -157,7 +162,7 @@ func main() {
157
162
var err error
158
163
hmacSecret , err = hex .DecodeString (* hmacSecretStr )
159
164
if err != nil {
160
- log .Fatalf ("Could not decode -hmac_secret=%q as hex string: %v" , hmacSecretStr , err )
165
+ log .Fatalf ("Could not decode -hmac_secret=%q as hex string: %v" , * hmacSecretStr , err )
161
166
}
162
167
163
168
http .HandleFunc ("/submit" , func (w http.ResponseWriter , r * http.Request ) {
@@ -203,7 +208,7 @@ func main() {
203
208
204
209
purposeparts := strings .Split (string (req .purpose ), ":" )
205
210
206
- if err := successTpl . Execute ( w , struct {
211
+ if err := templates . ExecuteTemplate ( w , "success.html" , struct {
207
212
Purposeparts []string
208
213
Token string
209
214
}{
0 commit comments