LabCodeHub öffentliche Ansicht
Anmelden
Küpper / Quildrop öffentlich
Branch: main
Quildrop / internal / config / config.go
Verlauf Rohdaten
R Rüdiger Küpper fix: add sitemap and robots.txt
89f2d506 vor 14 Tagen
internal/config/config.go 143 Zeilen · 4.0 KB · Go
1
package config
2
3
import (
4
	"os"
5
	"path/filepath"
6
7
	"gopkg.in/yaml.v3"
8
)
9
10
type MenuItem struct {
11
	Label    string     `yaml:"label"`
12
	URL      string     `yaml:"url,omitempty"`
13
	Children []MenuItem `yaml:"children,omitempty"`
14
}
15
16
// LLMsConfig toggles generation/serving of the llms.txt convention files.
17
// Both default to true so behaviour without the block stays unchanged.
18
type LLMsConfig struct {
19
	Enabled *bool `yaml:"enabled,omitempty"` // llms.txt
20
	Full    *bool `yaml:"full,omitempty"`    // llms-full.txt
21
}
22
23
// EnabledOrDefault returns whether llms.txt should be produced (default: true).
24
func (l LLMsConfig) EnabledOrDefault() bool {
25
	if l.Enabled == nil {
26
		return true
27
	}
28
	return *l.Enabled
29
}
30
31
// FullOrDefault returns whether llms-full.txt should be produced (default: true).
32
func (l LLMsConfig) FullOrDefault() bool {
33
	if l.Full == nil {
34
		return true
35
	}
36
	return *l.Full
37
}
38
39
// SitemapConfig toggles generation/serving of sitemap.xml for search engines.
40
// Defaults to true so behaviour without the block stays unchanged.
41
type SitemapConfig struct {
42
	Enabled *bool `yaml:"enabled,omitempty"` // sitemap.xml
43
}
44
45
// EnabledOrDefault returns whether sitemap.xml should be produced (default: true).
46
func (s SitemapConfig) EnabledOrDefault() bool {
47
	if s.Enabled == nil {
48
		return true
49
	}
50
	return *s.Enabled
51
}
52
53
// RobotsConfig toggles generation/serving of robots.txt and lets the site
54
// define its own Allow/Disallow rules. Defaults to true so behaviour without
55
// the block stays unchanged.
56
type RobotsConfig struct {
57
	Enabled   *bool    `yaml:"enabled,omitempty"`   // robots.txt
58
	UserAgent string   `yaml:"userAgent,omitempty"` // default: "*"
59
	Allow     []string `yaml:"allow,omitempty"`
60
	Disallow  []string `yaml:"disallow,omitempty"`
61
}
62
63
// EnabledOrDefault returns whether robots.txt should be produced (default: true).
64
func (r RobotsConfig) EnabledOrDefault() bool {
65
	if r.Enabled == nil {
66
		return true
67
	}
68
	return *r.Enabled
69
}
70
71
// UserAgentOrDefault returns the User-agent line's value (default: "*").
72
func (r RobotsConfig) UserAgentOrDefault() string {
73
	if r.UserAgent == "" {
74
		return "*"
75
	}
76
	return r.UserAgent
77
}
78
79
type Config struct {
80
	Title        string        `yaml:"title"`
81
	Description  string        `yaml:"description"`
82
	Author       string        `yaml:"author"`
83
	BaseURL      string        `yaml:"baseURL"`
84
	Port         int           `yaml:"port"`
85
	PostsPerPage int           `yaml:"postsPerPage"`
86
	ContentDir   string        `yaml:"contentDir"`
87
	SitesDir     string        `yaml:"sitesDir"`
88
	StaticDir    string        `yaml:"staticDir"`
89
	OutputDir    string        `yaml:"outputDir"`
90
	ThemesDir    string        `yaml:"themesDir"`
91
	Theme        string        `yaml:"theme"`
92
	Menu         []MenuItem    `yaml:"menu"`
93
	LLMs         LLMsConfig    `yaml:"llms"`
94
	Sitemap      SitemapConfig `yaml:"sitemap"`
95
	Robots       RobotsConfig  `yaml:"robots"`
96
}
97
98
// ThemeDir returns the directory of the active theme, e.g. "themes/default".
99
func (c *Config) ThemeDir() string {
100
	return filepath.Join(c.ThemesDir, c.Theme)
101
}
102
103
// ThemeTemplatesDir returns the template directory of the active theme.
104
func (c *Config) ThemeTemplatesDir() string {
105
	return filepath.Join(c.ThemeDir(), "templates")
106
}
107
108
// ThemeStaticDir returns the static asset directory of the active theme.
109
// The directory is optional; a theme may ship without own assets.
110
func (c *Config) ThemeStaticDir() string {
111
	return filepath.Join(c.ThemeDir(), "static")
112
}
113
114
func Load(path string) (*Config, error) {
115
	data, err := os.ReadFile(path)
116
	if err != nil {
117
		return nil, err
118
	}
119
	cfg := &Config{
120
		Port:         8080,
121
		PostsPerPage: 5,
122
		ContentDir:   "content",
123
		SitesDir:     "sites",
124
		StaticDir:    "static",
125
		OutputDir:    "output",
126
		ThemesDir:    "themes",
127
		Theme:        "default",
128
	}
129
	if err := yaml.Unmarshal(data, cfg); err != nil {
130
		return nil, err
131
	}
132
	// Empty values in config.yaml must not override the defaults.
133
	if cfg.StaticDir == "" {
134
		cfg.StaticDir = "static"
135
	}
136
	if cfg.ThemesDir == "" {
137
		cfg.ThemesDir = "themes"
138
	}
139
	if cfg.Theme == "" {
140
		cfg.Theme = "default"
141
	}
142
	return cfg, nil
143
}