refactor(config): scope folder per config file

This commit is contained in:
Anhgelus Morhtuuzh 2025-08-15 21:30:44 +02:00
parent 91f2ef06c0
commit 3b37150f6e
Signed by: anhgelus
GPG key ID: 617773CACE89052C
2 changed files with 29 additions and 22 deletions

28
data.go
View file

@ -32,6 +32,7 @@ type Config struct {
Legal string `json:"legal" toml:"legal"` Legal string `json:"legal" toml:"legal"`
RelMeLinks []string `json:"rel_me_links" toml:"rel_me_links"` RelMeLinks []string `json:"rel_me_links" toml:"rel_me_links"`
CustomPages []string `json:"custom_pages" toml:"custom_pages"` CustomPages []string `json:"custom_pages" toml:"custom_pages"`
folder string
} }
type Person struct { type Person struct {
@ -82,23 +83,6 @@ func getImage(s string) string {
return golatt.GetStaticPath(s) 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 { func (c *Config) GetBackground() template.CSS {
return c.Color.GetBackground() return c.Color.GetBackground()
} }
@ -119,7 +103,7 @@ var legalContent template.HTML
func (c *Config) GetLegal() (template.HTML, error) { func (c *Config) GetLegal() (template.HTML, error) {
if legalContent == "" { if legalContent == "" {
b, err := os.ReadFile(getPath(c.Legal)) b, err := os.ReadFile(c.folder + c.Legal)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -135,6 +119,7 @@ type CustomPage struct {
Description string `json:"description" toml:"description"` Description string `json:"description" toml:"description"`
Color *Color `json:"colors" toml:"colors"` Color *Color `json:"colors" toml:"colors"`
Content string `json:"content" toml:"content"` Content string `json:"content" toml:"content"`
folder string
} }
func (c *Config) LoadCustomPages() ([]*CustomPage, error) { func (c *Config) LoadCustomPages() ([]*CustomPage, error) {
@ -144,7 +129,7 @@ func (c *Config) LoadCustomPages() ([]*CustomPage, error) {
} }
var pages []*CustomPage var pages []*CustomPage
for _, cp := range c.CustomPages { for _, cp := range c.CustomPages {
b, err := os.ReadFile(getPath(cp)) b, err := os.ReadFile(c.folder + cp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -159,6 +144,9 @@ func (c *Config) LoadCustomPages() ([]*CustomPage, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
p.folder = getFolder(c.folder + cp)
pages = append(pages, &p) pages = append(pages, &p)
} }
return pages, nil return pages, nil
@ -213,7 +201,7 @@ var contentsMap = map[string]template.HTML{}
func (p *CustomPage) GetContent() (template.HTML, error) { func (p *CustomPage) GetContent() (template.HTML, error) {
res, ok := contentsMap[p.URI] res, ok := contentsMap[p.URI]
if !ok { if !ok {
b, err := os.ReadFile(getPath(p.Content)) b, err := os.ReadFile(p.folder + p.Content)
if err != nil { if err != nil {
return "", err return "", err
} }

23
main.go
View file

@ -75,6 +75,9 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
cfg.folder = getFolder(configPath)
customPages, err := cfg.LoadCustomPages() customPages, err := cfg.LoadCustomPages()
if err != nil { if err != nil {
panic(err) panic(err)
@ -84,13 +87,13 @@ func main() {
if dev { if dev {
g = golatt.New( g = golatt.New(
golatt.UsableEmbedFS("templates", templates), golatt.UsableEmbedFS("templates", templates),
os.DirFS(getPath("public")), os.DirFS(cfg.folder+"public"),
os.DirFS("dist"), os.DirFS("dist"),
) )
} else { } else {
g = golatt.New( g = golatt.New(
golatt.UsableEmbedFS("templates", templates), golatt.UsableEmbedFS("templates", templates),
os.DirFS(getPath("public")), os.DirFS(cfg.folder+"public"),
golatt.UsableEmbedFS("dist", assets), 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) { func generateConfigFile(isToml bool) {
cfg := Config{ cfg := Config{
Image: "wallpaper.webp", Image: "wallpaper.webp",