feat(config): support every path

This commit is contained in:
Anhgelus Morhtuuzh 2025-08-15 21:12:11 +02:00
parent 7b5d816992
commit 91f2ef06c0
Signed by: anhgelus
GPG key ID: 617773CACE89052C
3 changed files with 29 additions and 11 deletions

5
.gitignore vendored
View file

@ -175,7 +175,4 @@ dist
# Test files # Test files
public/ public/
config.json test
legal.html
test.html
test.json

23
data.go
View file

@ -82,6 +82,23 @@ 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()
} }
@ -102,7 +119,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(c.Legal) b, err := os.ReadFile(getPath(c.Legal))
if err != nil { if err != nil {
return "", err return "", err
} }
@ -127,7 +144,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(cp) b, err := os.ReadFile(getPath(cp))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -196,7 +213,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(p.Content) b, err := os.ReadFile(getPath(p.Content))
if err != nil { if err != nil {
return "", err return "", err
} }

12
main.go
View file

@ -24,10 +24,10 @@ var (
var ( var (
domain string domain string
configPath string configPath string
dev bool = false dev = false
generateToml bool generateToml bool
generateJson bool generateJson bool
port int = 80 port = 80
) )
func init() { func init() {
@ -82,11 +82,15 @@ func main() {
var g *golatt.Golatt var g *golatt.Golatt
if dev { if dev {
g = golatt.New(golatt.UsableEmbedFS("templates", templates), os.DirFS("public"), os.DirFS("dist")) g = golatt.New(
golatt.UsableEmbedFS("templates", templates),
os.DirFS(getPath("public")),
os.DirFS("dist"),
)
} else { } else {
g = golatt.New( g = golatt.New(
golatt.UsableEmbedFS("templates", templates), golatt.UsableEmbedFS("templates", templates),
os.DirFS("public"), os.DirFS(getPath("public")),
golatt.UsableEmbedFS("dist", assets), golatt.UsableEmbedFS("dist", assets),
) )
} }