Skip to content

Commit a42e5ee

Browse files
committed
switch from gentmpl.go to go:embed
1 parent b83aef7 commit a42e5ee

File tree

4 files changed

+12
-176
lines changed

4 files changed

+12
-176
lines changed

captchasrv.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package main
33
import (
44
"crypto/hmac"
55
"crypto/sha256"
6+
"embed"
67
"encoding/base64"
78
"encoding/hex"
89
"encoding/json"
910
"errors"
1011
"flag"
1112
"fmt"
13+
"html/template"
1214
"io/ioutil"
1315
"log"
1416
"net/http"
@@ -17,7 +19,8 @@ import (
1719
"time"
1820
)
1921

20-
//go:generate go run gentmpl.go form success
22+
//go:embed form.html success.html
23+
var templateFS embed.FS
2124

2225
var (
2326
// Generate using e.g. “openssl rand -hex 32”.
@@ -39,6 +42,8 @@ var (
3942
"host:port to listen on for HTTP requests")
4043
)
4144

45+
var templates = template.Must(template.New("").ParseFS(templateFS, "*.html"))
46+
4247
func verifyCaptcha(response string) error {
4348
resp, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify",
4449
url.Values{
@@ -131,7 +136,7 @@ func fromHTTP(r *http.Request) (request, error) {
131136
}
132137

133138
func writeForm(w http.ResponseWriter, msg string) {
134-
if err := formTpl.Execute(w, struct {
139+
if err := templates.ExecuteTemplate(w, "form.html", struct {
135140
SiteKey string
136141
Msg string
137142
}{
@@ -157,7 +162,7 @@ func main() {
157162
var err error
158163
hmacSecret, err = hex.DecodeString(*hmacSecretStr)
159164
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)
161166
}
162167

163168
http.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) {
@@ -203,7 +208,7 @@ func main() {
203208

204209
purposeparts := strings.Split(string(req.purpose), ":")
205210

206-
if err := successTpl.Execute(w, struct {
211+
if err := templates.ExecuteTemplate(w, "success.html", struct {
207212
Purposeparts []string
208213
Token string
209214
}{

gentmpl.go

-61
This file was deleted.

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/robustirc/captchasrv
2+
3+
go 1.19

templates.go

-111
This file was deleted.

0 commit comments

Comments
 (0)