1
1
package main
2
2
3
3
import (
4
+ "embed"
4
5
"fmt"
5
6
"html/template"
6
7
"log"
@@ -12,27 +13,30 @@ func main() {
12
13
render (w , "test.page.gohtml" )
13
14
})
14
15
15
- fmt .Println ("Starting front end service on port 80 " )
16
+ fmt .Println ("Starting front end service on port 8081 " )
16
17
17
- err := http .ListenAndServe (":80 " , nil )
18
+ err := http .ListenAndServe (":8081 " , nil )
18
19
if err != nil {
19
20
log .Panic (err )
20
21
}
21
22
}
22
23
24
+ //go:embed templates
25
+ var templateFS embed.FS
26
+
23
27
func render (w http.ResponseWriter , t string ) {
24
28
partials := []string {
25
- "./cmd/web/ templates/base.layout.gohtml" ,
26
- "./cmd/web/ templates/header.partial.gohtml" ,
27
- "./cmd/web/ templates/footer.partial.gohtml" ,
29
+ "templates/base.layout.gohtml" ,
30
+ "templates/header.partial.gohtml" ,
31
+ "templates/footer.partial.gohtml" ,
28
32
}
29
33
30
34
var templateSlice []string
31
- templateSlice = append (templateSlice , fmt .Sprintf ("./cmd/web/ templates/%s" , t ))
35
+ templateSlice = append (templateSlice , fmt .Sprintf ("templates/%s" , t ))
32
36
33
37
templateSlice = append (templateSlice , partials ... )
34
38
35
- tmpl , err := template .ParseFiles ( templateSlice ... )
39
+ tmpl , err := template .ParseFS ( templateFS , templateSlice ... )
36
40
if err != nil {
37
41
http .Error (w , err .Error (), http .StatusInternalServerError )
38
42
return
0 commit comments