From 7c3c1561390cc3064c8814b2d33d3a67d1902814 Mon Sep 17 00:00:00 2001 From: Mans Ziesel Date: Mon, 3 Jun 2024 13:56:16 +0200 Subject: [PATCH] embed templates in binary --- main.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 6764e61..202fb2a 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "embed" "encoding/json" "fmt" "html/template" @@ -27,6 +28,9 @@ type contextKey string const netAddrKey = contextKey("netAddr") +//go:embed templates/* +var templatesFS embed.FS + func RealIpMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { netAddr := extractNetAddress(r) @@ -85,7 +89,7 @@ func mainHandler(w http.ResponseWriter, r *http.Request) { switch r.URL.Path { case "/": fp := path.Join("templates", "index.html") - tmpl, err := template.ParseFiles(fp) + tmpl, err := template.ParseFS(templatesFS, fp) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return