Skip to content

Commit 476ad2e

Browse files
committed
embed templates, data, static files with go:embed
1 parent 4681dad commit 476ad2e

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

main.go

+37-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package main
22

33
import (
44
"compress/gzip"
5+
"embed"
56
"encoding/json"
67
"flag"
78
"fmt"
89
"html/template"
910
"log"
1011
"net/http"
11-
"os"
1212
"strings"
1313
"time"
1414

@@ -21,7 +21,16 @@ const (
2121
description = "The world's best Japanese dictionary."
2222
)
2323

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+
)
2534

2635
// Entry is a dictionary entry
2736
type Entry struct {
@@ -34,7 +43,7 @@ type Entry struct {
3443
var dict dictionary.Dictionary
3544

3645
func initialize() {
37-
file, err := os.Open("data/edict2.json.gz")
46+
file, err := data.Open("data/edict2.json.gz")
3847
if err != nil {
3948
log.Fatal("Could not load edict2.json.gz: ", err)
4049
}
@@ -82,7 +91,14 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
8291
"description": description,
8392
}
8493

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)
86102
if err != nil {
87103
log.Println("ERROR:", err)
88104
}
@@ -172,14 +188,28 @@ func search(w http.ResponseWriter, r *http.Request) {
172188
"description": description,
173189
}
174190

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)
176199
if err != nil {
177200
log.Println("ERROR:", err)
178201
}
179202
}
180203

181204
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)
183213
if err != nil {
184214
log.Println("ERROR:", err)
185215
}
@@ -196,7 +226,7 @@ func main() {
196226
http.HandleFunc("/search", search)
197227
http.HandleFunc("/search/", search)
198228
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)))
200230

201231
log.Printf("Running on %s ...", addr)
202232

0 commit comments

Comments
 (0)