From 6b323ba74650fd1eece1f4f000fdbff4ffb40e54 Mon Sep 17 00:00:00 2001 From: efim Date: Tue, 3 Oct 2023 14:05:53 +0000 Subject: [PATCH] feat: embedding templates as well maybe it's possible to somehow limit file server to a directory --- 17-results-summary-component-go/main.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/17-results-summary-component-go/main.go b/17-results-summary-component-go/main.go index 0bc4006..fa9f8d2 100644 --- a/17-results-summary-component-go/main.go +++ b/17-results-summary-component-go/main.go @@ -11,8 +11,12 @@ import ( //go:embed public var publicContent embed.FS +//go:embed templates +var templates embed.FS + // starts webserver that serves html page for exercise func main() { + // serves public static resources: bundled images, fonts, css // visible on http://localhost:8080/static/public/some-text.txt http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(publicContent)))) @@ -20,8 +24,10 @@ func main() { io.WriteString(w, "This is temporary here, hello") }) + // main page with results summary http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { - tmpl := template.Must(template.ParseFiles("templates/summary-component.gohtml")) + templateName := "templates/summary-component.gohtml" + tmpl := template.Must(template.ParseFS(templates, templateName)) tmpl.Execute(w, nil) })