Skip to content

Commit e7af1bc

Browse files
committed
fix: template from StaticFS
1 parent 97a22aa commit e7af1bc

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
)
2929

3030
const (
31-
version = "v0.0.1"
31+
version = "v0.0.4"
3232
)
3333

3434
var (

cmd/serve.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"fmt"
2020
"os"
2121
"strings"
22+
"text/template"
2223
"vimbin/internal/config"
2324
"vimbin/internal/handlers"
2425
"vimbin/internal/server"
@@ -48,10 +49,11 @@ var serveCmd = &cobra.Command{
4849
}
4950

5051
// Read the HTML template file
51-
htmlTemplate, err := os.ReadFile("web/templates/index.html")
52+
htmlTemplate, err := template.ParseFS(server.StaticFS, "web/templates/index.html")
5253
if err != nil {
53-
panic(err)
54+
log.Fatal().Err(err)
5455
}
56+
5557
config.App.HtmlTemplate = htmlTemplate
5658

5759
// Parse the configuration

internal/config/structs.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package config
22

3-
import "sync"
3+
import (
4+
"sync"
5+
"text/template"
6+
)
47

58
// App is the global configuration instance.
69
var App Config
710

811
// Config represents the application configuration.
912
type Config struct {
10-
HtmlTemplate []byte `yaml:"-"` // HtmlTemplate contains the HTML template content.
11-
Server Server `yaml:"server"` // Server represents the server configuration.
12-
Storage Storage `yaml:"storage"` // Storage represents the storage configuration.
13+
HtmlTemplate *template.Template `yaml:"-"` // HtmlTemplate contains the HTML template content.
14+
Server Server `yaml:"server"` // Server represents the server configuration.
15+
Storage Storage `yaml:"storage"` // Storage represents the storage configuration.
1316
}
1417

1518
// Web represents the web configuration.

internal/handlers/home.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handlers
22

33
import (
4-
"html/template"
54
"net/http"
65
"vimbin/internal/config"
76
"vimbin/internal/server"
@@ -30,14 +29,7 @@ func Home(w http.ResponseWriter, r *http.Request) {
3029
Content: config.App.Storage.Content.Get(),
3130
}
3231

33-
tmpl, err := template.New("index").Parse(string(config.App.HtmlTemplate))
34-
if err != nil {
35-
http.Error(w, err.Error(), http.StatusInternalServerError)
36-
return
37-
}
38-
39-
err = tmpl.Execute(w, page)
40-
if err != nil {
32+
if err := config.App.HtmlTemplate.Execute(w, page); err != nil {
4133
http.Error(w, err.Error(), http.StatusInternalServerError)
4234
}
4335
}

0 commit comments

Comments
 (0)