diff --git a/pkg/activity/activity.go b/pkg/activity/activity.go index 8c49dbd..addc8b9 100644 --- a/pkg/activity/activity.go +++ b/pkg/activity/activity.go @@ -2,6 +2,7 @@ package activity import ( "bufio" + "embed" "errors" "fmt" "os" @@ -26,6 +27,9 @@ const ( relativeAssetLibraryPath = "Library/Mobile Documents/com~apple~CloudDocs/privat/sport/Tourenbuch/" ) +//go:embed templates/* +var content embed.FS + var ErrTourenbuchDirNameWrong = errors.New("directory name does not match expected schema") type Meta struct { @@ -105,7 +109,7 @@ func (a *Activity) createFolder() error { func (a *Activity) initSkeleton(file string) (string, error) { location := fmt.Sprintf("templates/tourenbuch/%s/%s", a.Meta.Category, file) - tmpl, err := template.ParseFiles(location) + tmpl, err := template.ParseFS(content, location) if err != nil { return "", fmt.Errorf("parsing template file: %s; error: %w", location, err) } diff --git a/templates/tourenbuch/mtb/description.md b/pkg/activity/templates/mtb/description.md similarity index 100% rename from templates/tourenbuch/mtb/description.md rename to pkg/activity/templates/mtb/description.md diff --git a/templates/tourenbuch/mtb/elevation.plt b/pkg/activity/templates/mtb/elevation.plt similarity index 100% rename from templates/tourenbuch/mtb/elevation.plt rename to pkg/activity/templates/mtb/elevation.plt diff --git a/templates/tourenbuch/mtb/header.yaml b/pkg/activity/templates/mtb/header.yaml similarity index 100% rename from templates/tourenbuch/mtb/header.yaml rename to pkg/activity/templates/mtb/header.yaml diff --git a/templates/tourenbuch/wandern/description.md b/pkg/activity/templates/wandern/description.md similarity index 100% rename from templates/tourenbuch/wandern/description.md rename to pkg/activity/templates/wandern/description.md diff --git a/templates/tourenbuch/wandern/elevation.plt b/pkg/activity/templates/wandern/elevation.plt similarity index 100% rename from templates/tourenbuch/wandern/elevation.plt rename to pkg/activity/templates/wandern/elevation.plt diff --git a/templates/tourenbuch/wandern/header.yaml b/pkg/activity/templates/wandern/header.yaml similarity index 100% rename from templates/tourenbuch/wandern/header.yaml rename to pkg/activity/templates/wandern/header.yaml diff --git a/pkg/oauth/oauth.go b/pkg/oauth/oauth.go index f7bfc09..ae67562 100644 --- a/pkg/oauth/oauth.go +++ b/pkg/oauth/oauth.go @@ -56,7 +56,7 @@ func handleStravaCallback(tokenFile string) func(w http.ResponseWriter, r *http. return func(w http.ResponseWriter, r *http.Request) (interface{}, error) { if r.FormValue("state") != OauthStateString { log.Error().Msg("Invalid oauth state") - templates.Render(w, "templates/html/strava-failure.html") + templates.HTMLRender(w, "templates/strava-failure.html") // insert html error here return nil, fmt.Errorf("%w: %s", ErrInvalidOauthState, r.FormValue("state")) @@ -65,12 +65,12 @@ func handleStravaCallback(tokenFile string) func(w http.ResponseWriter, r *http. token, err := StravaOauthConfig.Exchange(context.Background(), r.FormValue("code")) if err != nil { log.Error().Err(err).Msg("Code exchange failed") - templates.Render(w, "templates/html/strava-failure.html") + templates.HTMLRender(w, "templates/strava-failure.html") return nil, fmt.Errorf("code exchange failed: %w", err) } - templates.Render(w, "templates/html/strava-success.html") + templates.HTMLRender(w, "templates/strava-success.html") if err := utils.SaveToken(tokenFile, token); err != nil { log.Error().Err(err).Msg("Failed to save token") diff --git a/pkg/templates/template.go b/pkg/templates/template.go index ac0b12e..49c0b05 100644 --- a/pkg/templates/template.go +++ b/pkg/templates/template.go @@ -1,12 +1,16 @@ package templates import ( + "embed" "html/template" "net/http" ) -func Render(w http.ResponseWriter, tmpl string) { - render, err := template.ParseFiles(tmpl) +//go:embed templates/* +var content embed.FS + +func HTMLRender(w http.ResponseWriter, tmpl string) { + render, err := template.ParseFS(content, tmpl) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/templates/html/strava-failure.html b/pkg/templates/templates/strava-failure.html similarity index 100% rename from templates/html/strava-failure.html rename to pkg/templates/templates/strava-failure.html diff --git a/templates/html/strava-success.html b/pkg/templates/templates/strava-success.html similarity index 100% rename from templates/html/strava-success.html rename to pkg/templates/templates/strava-success.html