mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-10-30 01:13:08 +00:00
config: Use toml for configuration
This commit is contained in:
parent
fd187da71d
commit
c7adb5fef1
53
config.go
53
config.go
|
@ -3,52 +3,41 @@ package main
|
||||||
import (
|
import (
|
||||||
"mime"
|
"mime"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"git.sr.ht/~adnano/go-ini"
|
"github.com/pelletier/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config contains site configuration.
|
// Config contains site configuration.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Title string // site title
|
Title string `toml:"title"` // site title
|
||||||
URLs []string // site URLs
|
URLs []string `toml:"urls"` // site URLs
|
||||||
Feeds map[string]string // site feeds
|
Feeds map[string]string `toml:"feeds"` // site feeds
|
||||||
Templates *Templates // site templates
|
Mediatypes map[string][]string `toml:"mediatypes"` // mediatypes
|
||||||
|
Templates *Templates `toml:"-"` // site templates
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConfig returns a new configuration.
|
// LoadConfig loads the configuration from the provided path.
|
||||||
func NewConfig() *Config {
|
func LoadConfig(path string) (*Config, error) {
|
||||||
return &Config{
|
c := new(Config)
|
||||||
Feeds: map[string]string{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load loads the configuration from the provided path.
|
|
||||||
func (c *Config) Load(path string) error {
|
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
if err := toml.NewDecoder(f).Decode(c); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load config
|
// Register media types
|
||||||
return ini.Parse(f, func(section, key, value string) {
|
for typ, exts := range c.Mediatypes {
|
||||||
switch section {
|
for _, ext := range exts {
|
||||||
case "":
|
mime.AddExtensionType(typ, ext)
|
||||||
switch key {
|
|
||||||
case "title":
|
|
||||||
c.Title = value
|
|
||||||
case "url":
|
|
||||||
c.URLs = strings.Fields(value)
|
|
||||||
}
|
|
||||||
case "feeds":
|
|
||||||
c.Feeds[key] = value
|
|
||||||
case "mediatypes":
|
|
||||||
for _, field := range strings.Fields(value) {
|
|
||||||
mime.AddExtensionType(key, field)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadTemplates loads templates from the provided path.
|
// LoadTemplates loads templates from the provided path.
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
title=Example Site
|
|
||||||
url=gemini://example.com https://example.com
|
|
||||||
|
|
||||||
[feeds]
|
|
||||||
/blog/=Example Feed
|
|
5
example/config.toml
Normal file
5
example/config.toml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
title = "Example Site"
|
||||||
|
urls = ["gemini://example.com", "https://example.com"]
|
||||||
|
|
||||||
|
[feeds]
|
||||||
|
"/blog/" = "Example Feed"
|
2
go.mod
2
go.mod
|
@ -4,5 +4,5 @@ go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.sr.ht/~adnano/go-gemini v0.1.8
|
git.sr.ht/~adnano/go-gemini v0.1.8
|
||||||
git.sr.ht/~adnano/go-ini v0.0.0-20201014024959-cc89f6531f0e
|
github.com/pelletier/go-toml v1.8.1
|
||||||
)
|
)
|
||||||
|
|
6
go.sum
6
go.sum
|
@ -1,4 +1,6 @@
|
||||||
git.sr.ht/~adnano/go-gemini v0.1.8 h1:93DxDNXB0bjnfDhZewf+QsEopfuOMh/I4v7ujoJ6WIs=
|
git.sr.ht/~adnano/go-gemini v0.1.8 h1:93DxDNXB0bjnfDhZewf+QsEopfuOMh/I4v7ujoJ6WIs=
|
||||||
git.sr.ht/~adnano/go-gemini v0.1.8/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0=
|
git.sr.ht/~adnano/go-gemini v0.1.8/go.mod h1:If1VxEWcZDrRt5FeAFnGTcM2Ud1E3BXs3VJ5rnZWKq0=
|
||||||
git.sr.ht/~adnano/go-ini v0.0.0-20201014024959-cc89f6531f0e h1:NRVtAaE3jvBKkwX/x7/yGeQKe8Agj13VDoKKOEE04aA=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
git.sr.ht/~adnano/go-ini v0.0.0-20201014024959-cc89f6531f0e/go.mod h1:eysIkA6b8oivxACZfyG75+wlq6BW6t5qPVnx8IcFYlE=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM=
|
||||||
|
github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc=
|
||||||
|
|
4
main.go
4
main.go
|
@ -17,8 +17,8 @@ func run() error {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Load config
|
// Load config
|
||||||
cfg := NewConfig()
|
cfg, err := LoadConfig("config.toml")
|
||||||
if err := cfg.Load("config.ini"); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := cfg.LoadTemplates("layouts"); err != nil {
|
if err := cfg.LoadTemplates("layouts"); err != nil {
|
||||||
|
|
Loading…
Reference in a new issue