Skip to content

Commit

Permalink
embed static files
Browse files Browse the repository at this point in the history
  • Loading branch information
nce committed Sep 21, 2024
1 parent 035fdcf commit 0241a46
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 6 deletions.
6 changes: 5 additions & 1 deletion pkg/activity/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package activity

import (
"bufio"
"embed"
"errors"
"fmt"
"os"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions pkg/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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")
Expand Down
8 changes: 6 additions & 2 deletions pkg/templates/template.go
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 0241a46

Please sign in to comment.