From 5619fbfe5b48ddd93eabc08babe0903dd45d6466 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Mon, 10 Jul 2023 21:06:12 -0400 Subject: [PATCH] absolute paths are the way of the light --- cmd/web/main.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/cmd/web/main.go b/cmd/web/main.go index e46f7f9..bb9c548 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -5,12 +5,12 @@ import ( "flag" "fmt" "io/fs" + "log" + "net/http" "os" "path/filepath" - "strings" - // "log" - "net/http" "strconv" + "strings" "code.ndumas.com/ndumas/badges" "github.com/go-chi/chi/v5" @@ -30,7 +30,8 @@ func NewBadgeRenderer(fontDir string) (*BadgeRenderer, error) { root := os.DirFS(fontDir) err := fs.WalkDir(root, ".", func(path string, d fs.DirEntry, err error) error { if strings.HasSuffix(path, ".ttf") { - br.Fonts[filepath.Base(path)] = path + absFont := filepath.Join(fontDir, path) + br.Fonts[filepath.Base(path)] = absFont } return nil }) @@ -133,18 +134,24 @@ func (br *BadgeRenderer) renderBadge(w http.ResponseWriter, r *http.Request) { } } -var ( - port int - fontsDir string -) - func main() { + var ( + port int + fontsDir string + ) + flag.IntVar(&port, "port", 8484, "http listening port") flag.StringVar(&fontsDir, "fonts", "fonts/", "directory containing ttf files") flag.Parse() - br, err := NewBadgeRenderer(fontsDir) + absFontDir, err := filepath.Abs(fontsDir) + if err != nil { + log.Fatalf("error building absolute font dir path: %s\n", err) + } + log.Println("absFontDir:", absFontDir) + br, err := NewBadgeRenderer(absFontDir) + if err != nil { fmt.Println("couldn't create renderer", err) return