Skip to content

Commit f2932a0

Browse files
committed
go: fix setHTMLTemplate is not thread safe warning 371622
1 parent b59d0c4 commit f2932a0

File tree

7 files changed

+47
-18
lines changed

7 files changed

+47
-18
lines changed

note.go.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,18 @@ docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443 --workload 7"
105105
2024/01/09 10:52:50 投票者の感心がなくなりました
106106
2024/01/09 10:52:50 {"score": 322438, "success": 216894, "failure": 0}
107107
```
108+
109+
go: fix setHTMLTemplate is not thread safe warning 371622
110+
111+
```
112+
❯ make bench
113+
docker exec -i ishocon2-bench-1 sh -c "./benchmark --ip app:443 --workload 7"
114+
2024/01/10 09:01:50 Start GET /initialize
115+
2024/01/10 09:01:50 期日前投票を開始します
116+
2024/01/10 09:01:51 期日前投票が終了しました
117+
2024/01/10 09:01:51 投票を開始します Workload: 7
118+
2024/01/10 09:02:36 投票が終了しました
119+
2024/01/10 09:02:36 投票者が結果を確認しています
120+
2024/01/10 09:02:51 投票者の感心がなくなりました
121+
2024/01/10 09:02:51 {"score": 371622, "success": 246886, "failure": 0}
122+
```

webapp/go/main.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package main
22

33
import (
44
"database/sql"
5+
"github.com/gin-contrib/multitemplate"
56
"html/template"
6-
"log"
7+
// "log"
78
"net/http"
89
"os"
10+
"path/filepath"
911
"sort"
1012
"strconv"
13+
"strings"
1114

1215
"github.com/gin-gonic/contrib/sessions"
1316
"github.com/gin-gonic/contrib/static"
@@ -24,6 +27,25 @@ func getEnv(key, fallback string) string {
2427
return fallback
2528
}
2629

30+
func createRender() multitemplate.Renderer {
31+
r := multitemplate.NewRenderer()
32+
pagesPath := "templates/includes"
33+
34+
layout := "templates/layouts/layout.tmpl"
35+
files, err := os.ReadDir(pagesPath)
36+
if err != nil {
37+
panic(err)
38+
}
39+
funcs := template.FuncMap{"indexPlus1": func(i int) int { return i + 1 }}
40+
41+
for _, file := range files {
42+
fileNameWithoutExt := strings.TrimSuffix(file.Name(), filepath.Ext(file.Name()))
43+
//fileName := file.Name()
44+
r.AddFromFilesFuncs(fileNameWithoutExt, funcs, layout, pagesPath+"/"+file.Name())
45+
}
46+
return r
47+
}
48+
2749
func main() {
2850
// database setting
2951
user := getEnv("ISHOCON2_DB_USER", "ishocon")
@@ -35,7 +57,10 @@ func main() {
3557
gin.SetMode(gin.DebugMode)
3658
r := gin.Default()
3759
r.Use(static.Serve("/css", static.LocalFile("public/css", true)))
38-
layout := "templates/layout.tmpl"
60+
61+
r.LoadHTMLFiles("templates/**/*.tmpl")
62+
//r.HTMLRender = createRender()
63+
r.HTMLRender = createRender()
3964

4065
// session store
4166
store := sessions.NewCookieStore([]byte("mysession"))
@@ -67,9 +92,6 @@ func main() {
6792
// Sort partyNames
6893
sort.Strings(partyNames)
6994

70-
// log partyNames value for debug
71-
log.Println(partyNames)
72-
7395
//partyNames := getAllPartyName()
7496
partyResultMap := map[string]int{}
7597
for _, name := range partyNames {
@@ -100,9 +122,7 @@ func main() {
100122
}
101123
}
102124

103-
funcs := template.FuncMap{"indexPlus1": func(i int) int { return i + 1 }}
104-
r.SetHTMLTemplate(template.Must(template.New("main").Funcs(funcs).ParseFiles(layout, "templates/index.tmpl")))
105-
c.HTML(http.StatusOK, "base", gin.H{
125+
c.HTML(http.StatusOK, "index", gin.H{
106126
"candidates": candidates,
107127
"parties": partyResults,
108128
"sexRatio": sexRatio,
@@ -129,8 +149,7 @@ func main() {
129149
candidateIDs := []int{candidateID}
130150
keywords := getVoiceOfSupporter(candidateIDs)
131151

132-
r.SetHTMLTemplate(template.Must(template.ParseFiles(layout, "templates/candidate.tmpl")))
133-
c.HTML(http.StatusOK, "base", gin.H{
152+
c.HTML(http.StatusOK, "candidate", gin.H{
134153
"candidate": candidate,
135154
"votes": votes,
136155
"keywords": keywords,
@@ -161,8 +180,7 @@ func main() {
161180
}
162181
keywords := getVoiceOfSupporter(candidateIDs)
163182

164-
r.SetHTMLTemplate(template.Must(template.ParseFiles(layout, "templates/political_party.tmpl")))
165-
c.HTML(http.StatusOK, "base", gin.H{
183+
c.HTML(http.StatusOK, "political_party", gin.H{
166184
"politicalParty": partyName,
167185
"votes": votes,
168186
"candidates": candidatesByParty,
@@ -172,8 +190,7 @@ func main() {
172190

173191
// GET /vote
174192
r.GET("/vote", func(c *gin.Context) {
175-
r.SetHTMLTemplate(template.Must(template.ParseFiles(layout, "templates/vote.tmpl")))
176-
c.HTML(http.StatusOK, "base", gin.H{
193+
c.HTML(http.StatusOK, "vote", gin.H{
177194
"candidates": candidates,
178195
"message": "",
179196
})
@@ -194,7 +211,6 @@ func main() {
194211
voteCount, _ := strconv.Atoi(c.PostForm("vote_count"))
195212

196213
var message string
197-
r.SetHTMLTemplate(template.Must(template.ParseFiles(layout, "templates/vote.tmpl")))
198214
if userErr != nil {
199215
message = "個人情報に誤りがあります"
200216
} else if user.Votes < voteCount+votedCount {
@@ -213,7 +229,7 @@ func main() {
213229
createVote(user.ID, candidate.ID, c.PostForm("keyword"), voteCount)
214230
message = "投票に成功しました"
215231
}
216-
c.HTML(http.StatusOK, "base", gin.H{
232+
c.HTML(http.StatusOK, "vote", gin.H{
217233
"candidates": candidates,
218234
"message": message,
219235
})

webapp/go/templates/layout.tmpl renamed to webapp/go/templates/layouts/layout.tmpl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{{ define "base" }}
21
<!DOCTYPE html>
32
<html>
43
<head>
@@ -27,4 +26,3 @@
2726

2827
</body>
2928
</html>
30-
{{ end }}

0 commit comments

Comments
 (0)