@@ -2,13 +2,13 @@ package main
2
2
3
3
import (
4
4
"compress/gzip"
5
+ "embed"
5
6
"encoding/json"
6
7
"flag"
7
8
"fmt"
8
9
"html/template"
9
10
"log"
10
11
"net/http"
11
- "os"
12
12
"strings"
13
13
"time"
14
14
@@ -21,7 +21,16 @@ const (
21
21
description = "The world's best Japanese dictionary."
22
22
)
23
23
24
- var templates = template .Must (template .ParseFiles ("templates/home.html" , "templates/about.html" , "templates/base.html" ))
24
+ var (
25
+ //go:embed templates/*
26
+ content embed.FS
27
+
28
+ //go:embed data/*
29
+ data embed.FS
30
+
31
+ //go:embed static/*
32
+ static embed.FS
33
+ )
25
34
26
35
// Entry is a dictionary entry
27
36
type Entry struct {
@@ -34,7 +43,7 @@ type Entry struct {
34
43
var dict dictionary.Dictionary
35
44
36
45
func initialize () {
37
- file , err := os .Open ("data/edict2.json.gz" )
46
+ file , err := data .Open ("data/edict2.json.gz" )
38
47
if err != nil {
39
48
log .Fatal ("Could not load edict2.json.gz: " , err )
40
49
}
@@ -82,7 +91,14 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
82
91
"description" : description ,
83
92
}
84
93
85
- err = templates .ExecuteTemplate (w , "home.html" , m )
94
+ t , err := template .ParseFS (content , "templates/home.html" )
95
+ if err != nil {
96
+ log .Println ("ERROR:" , err )
97
+ http .Error (w , err .Error (), http .StatusInternalServerError )
98
+ return
99
+ }
100
+
101
+ err = t .ExecuteTemplate (w , "home.html" , m )
86
102
if err != nil {
87
103
log .Println ("ERROR:" , err )
88
104
}
@@ -172,14 +188,28 @@ func search(w http.ResponseWriter, r *http.Request) {
172
188
"description" : description ,
173
189
}
174
190
175
- err = templates .ExecuteTemplate (w , "home.html" , m )
191
+ t , err := template .ParseFS (content , "templates/home.html" )
192
+ if err != nil {
193
+ log .Println ("ERROR:" , err )
194
+ http .Error (w , err .Error (), http .StatusInternalServerError )
195
+ return
196
+ }
197
+
198
+ err = t .ExecuteTemplate (w , "home.html" , m )
176
199
if err != nil {
177
200
log .Println ("ERROR:" , err )
178
201
}
179
202
}
180
203
181
204
func aboutHandler (w http.ResponseWriter , r * http.Request ) {
182
- err := templates .ExecuteTemplate (w , "about.html" , nil )
205
+ t , err := template .ParseFS (content , "templates/*.html" )
206
+ if err != nil {
207
+ log .Println ("ERROR:" , err )
208
+ http .Error (w , err .Error (), http .StatusInternalServerError )
209
+ return
210
+ }
211
+
212
+ err = t .ExecuteTemplate (w , "about.html" , nil )
183
213
if err != nil {
184
214
log .Println ("ERROR:" , err )
185
215
}
@@ -196,7 +226,7 @@ func main() {
196
226
http .HandleFunc ("/search" , search )
197
227
http .HandleFunc ("/search/" , search )
198
228
http .HandleFunc ("/about" , aboutHandler )
199
- http .Handle ("/static/" , http .StripPrefix ( "/static" , http . FileServer (http .Dir ( " static" ) )))
229
+ http .Handle ("/static/" , http .FileServer (http .FS ( static )))
200
230
201
231
log .Printf ("Running on %s ..." , addr )
202
232
0 commit comments