From 3b37150f6ee5d4aa40028f249891bbed73c96f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Fri, 15 Aug 2025 21:30:44 +0200 Subject: [PATCH 1/2] refactor(config): scope folder per config file --- data.go | 28 ++++++++-------------------- main.go | 23 +++++++++++++++++++++-- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/data.go b/data.go index 94cd08d..103207b 100644 --- a/data.go +++ b/data.go @@ -32,6 +32,7 @@ type Config struct { Legal string `json:"legal" toml:"legal"` RelMeLinks []string `json:"rel_me_links" toml:"rel_me_links"` CustomPages []string `json:"custom_pages" toml:"custom_pages"` + folder string } type Person struct { @@ -82,23 +83,6 @@ func getImage(s string) string { return golatt.GetStaticPath(s) } -func getPath(filename string) string { - if !strings.Contains(configPath, "/") { - return filename - } - sp := strings.Split(configPath, "/") - before := strings.Join(sp[1:len(sp)-1], "/") - if configPath[0] != '/' && !strings.HasPrefix(configPath, "./") { - if len(before) == 0 { - before = sp[0] - } else { - before = sp[0] + "/" + before - } - } - before += "/" - return before + filename -} - func (c *Config) GetBackground() template.CSS { return c.Color.GetBackground() } @@ -119,7 +103,7 @@ var legalContent template.HTML func (c *Config) GetLegal() (template.HTML, error) { if legalContent == "" { - b, err := os.ReadFile(getPath(c.Legal)) + b, err := os.ReadFile(c.folder + c.Legal) if err != nil { return "", err } @@ -135,6 +119,7 @@ type CustomPage struct { Description string `json:"description" toml:"description"` Color *Color `json:"colors" toml:"colors"` Content string `json:"content" toml:"content"` + folder string } func (c *Config) LoadCustomPages() ([]*CustomPage, error) { @@ -144,7 +129,7 @@ func (c *Config) LoadCustomPages() ([]*CustomPage, error) { } var pages []*CustomPage for _, cp := range c.CustomPages { - b, err := os.ReadFile(getPath(cp)) + b, err := os.ReadFile(c.folder + cp) if err != nil { return nil, err } @@ -159,6 +144,9 @@ func (c *Config) LoadCustomPages() ([]*CustomPage, error) { if err != nil { return nil, err } + + p.folder = getFolder(c.folder + cp) + pages = append(pages, &p) } return pages, nil @@ -213,7 +201,7 @@ var contentsMap = map[string]template.HTML{} func (p *CustomPage) GetContent() (template.HTML, error) { res, ok := contentsMap[p.URI] if !ok { - b, err := os.ReadFile(getPath(p.Content)) + b, err := os.ReadFile(p.folder + p.Content) if err != nil { return "", err } diff --git a/main.go b/main.go index 35f8e78..f8f82aa 100644 --- a/main.go +++ b/main.go @@ -75,6 +75,9 @@ func main() { if err != nil { panic(err) } + + cfg.folder = getFolder(configPath) + customPages, err := cfg.LoadCustomPages() if err != nil { panic(err) @@ -84,13 +87,13 @@ func main() { if dev { g = golatt.New( golatt.UsableEmbedFS("templates", templates), - os.DirFS(getPath("public")), + os.DirFS(cfg.folder+"public"), os.DirFS("dist"), ) } else { g = golatt.New( golatt.UsableEmbedFS("templates", templates), - os.DirFS(getPath("public")), + os.DirFS(cfg.folder+"public"), golatt.UsableEmbedFS("dist", assets), ) } @@ -148,6 +151,22 @@ func main() { } } +func getFolder(path string) string { + if !strings.Contains(path, "/") { + return "" + } + sp := strings.Split(path, "/") + folder := strings.Join(sp[1:len(sp)-1], "/") + if path[0] != '/' && !strings.HasPrefix(path, "./") { + if len(folder) == 0 { + folder = sp[0] + } else { + folder = sp[0] + "/" + folder + } + } + return folder + "/" +} + func generateConfigFile(isToml bool) { cfg := Config{ Image: "wallpaper.webp", From 614a654824372aaa81131c17122bd3463c006baa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Fri, 15 Aug 2025 21:40:38 +0200 Subject: [PATCH 2/2] feat(cli): customize public dir --- main.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index f8f82aa..d69e3cd 100644 --- a/main.go +++ b/main.go @@ -22,17 +22,19 @@ var ( ) var ( - domain string - configPath string - dev = false - generateToml bool - generateJson bool - port = 80 + domain string + configPath string + publicDirPath string + dev = false + generateToml bool + generateJson bool + port = 80 ) func init() { flag.StringVar(&domain, "domain", "", "domain to use") flag.StringVar(&configPath, "config", "", "config to use") + flag.StringVar(&publicDirPath, "public-dir", "", "public directory to use, default is 'public' inside the folder of your config") flag.BoolVar(&dev, "dev", dev, "dev mode enabled") flag.BoolVar(&generateJson, "generate-json-config", false, "generate a config example") flag.BoolVar(&generateToml, "generate-toml-config", false, "generate a config example") @@ -83,17 +85,22 @@ func main() { panic(err) } + publicFolder := cfg.folder + "public" + if len(publicDirPath) != 0 { + publicFolder = publicDirPath + } + var g *golatt.Golatt if dev { g = golatt.New( golatt.UsableEmbedFS("templates", templates), - os.DirFS(cfg.folder+"public"), + os.DirFS(publicFolder), os.DirFS("dist"), ) } else { g = golatt.New( golatt.UsableEmbedFS("templates", templates), - os.DirFS(cfg.folder+"public"), + os.DirFS(publicFolder), golatt.UsableEmbedFS("dist", assets), ) }