You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.9 KiB
107 lines
2.9 KiB
package config |
|
|
|
import ( |
|
"fmt" |
|
"os" |
|
|
|
"gopkg.in/yaml.v3" |
|
) |
|
|
|
// Config represents the application configuration |
|
type Config struct { |
|
WikiIndex string `yaml:"wiki_index"` |
|
BlogIndex string `yaml:"blog_index"` |
|
RepoAnnouncement string `yaml:"repo_announcement"` // naddr for kind 30617 repo announcement |
|
Relays struct { |
|
Primary string `yaml:"primary"` |
|
Fallback string `yaml:"fallback"` |
|
AdditionalFallback string `yaml:"additional_fallback"` |
|
} `yaml:"relays"` |
|
LinkBaseURL string `yaml:"link_base_url"` |
|
Cache struct { |
|
RefreshIntervalMinutes int `yaml:"refresh_interval_minutes"` |
|
} `yaml:"cache"` |
|
Feed struct { |
|
Relay string `yaml:"relay"` |
|
PollIntervalMinutes int `yaml:"poll_interval_minutes"` |
|
MaxEvents int `yaml:"max_events"` |
|
} `yaml:"feed"` |
|
Server struct { |
|
Port int `yaml:"port"` |
|
EnableCompression bool `yaml:"enable_compression"` |
|
} `yaml:"server"` |
|
SEO struct { |
|
SiteName string `yaml:"site_name"` |
|
SiteURL string `yaml:"site_url"` |
|
DefaultImage string `yaml:"default_image"` |
|
} `yaml:"seo"` |
|
} |
|
|
|
// LoadConfig loads configuration from a YAML file |
|
func LoadConfig(path string) (*Config, error) { |
|
data, err := os.ReadFile(path) |
|
if err != nil { |
|
return nil, fmt.Errorf("failed to read config file: %w", err) |
|
} |
|
|
|
var config Config |
|
if err := yaml.Unmarshal(data, &config); err != nil { |
|
return nil, fmt.Errorf("failed to parse config file: %w", err) |
|
} |
|
|
|
// Set defaults |
|
if config.Relays.Primary == "" { |
|
config.Relays.Primary = "wss://theforest.nostr1.com" |
|
} |
|
if config.Relays.Fallback == "" { |
|
config.Relays.Fallback = "wss://nostr.land" |
|
} |
|
if config.Relays.AdditionalFallback == "" { |
|
config.Relays.AdditionalFallback = "wss://thecitadel.nostr1.com" |
|
} |
|
if config.LinkBaseURL == "" { |
|
config.LinkBaseURL = "https://alexandria.gitcitadel.eu" |
|
} |
|
if config.Cache.RefreshIntervalMinutes == 0 { |
|
config.Cache.RefreshIntervalMinutes = 30 |
|
} |
|
if config.Feed.Relay == "" { |
|
config.Feed.Relay = "wss://theforest.nostr1.com" |
|
} |
|
if config.Feed.PollIntervalMinutes == 0 { |
|
config.Feed.PollIntervalMinutes = 5 |
|
} |
|
if config.Feed.MaxEvents == 0 { |
|
config.Feed.MaxEvents = 30 |
|
} |
|
if config.Server.Port == 0 { |
|
config.Server.Port = 8080 |
|
} |
|
if config.SEO.SiteName == "" { |
|
config.SEO.SiteName = "GitCitadel" |
|
} |
|
if config.SEO.SiteURL == "" { |
|
config.SEO.SiteURL = "https://gitcitadel.com" |
|
} |
|
if config.SEO.DefaultImage == "" { |
|
config.SEO.DefaultImage = "/static/GitCitadel_Graphic_Landscape.png" |
|
} |
|
|
|
// Validate required fields |
|
if config.WikiIndex == "" { |
|
return nil, fmt.Errorf("wiki_index is required") |
|
} |
|
|
|
return &config, nil |
|
} |
|
|
|
// Validate validates the configuration |
|
func (c *Config) Validate() error { |
|
if c.WikiIndex == "" { |
|
return fmt.Errorf("wiki_index is required") |
|
} |
|
if c.Relays.Primary == "" { |
|
return fmt.Errorf("relays.primary is required") |
|
} |
|
return nil |
|
}
|
|
|