first commit
This commit is contained in:
commit
581d3e6da5
11 changed files with 987 additions and 0 deletions
46
config.go
Normal file
46
config.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Listen string `toml:"listen"`
|
||||
Root string `toml:"root"`
|
||||
Hidden bool `toml:"hidden"`
|
||||
Footer string `toml:"footer"`
|
||||
}
|
||||
|
||||
func loadConfig(path string) (*Config, error) {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg := &Config{}
|
||||
if err := toml.Unmarshal(b, cfg); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
if cfg.Root == "" {
|
||||
return nil, errors.New(`config: "root" is required`)
|
||||
}
|
||||
root, err := filepath.Abs(cfg.Root)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if info, err := os.Stat(root); err != nil || !info.IsDir() {
|
||||
return nil, fmt.Errorf("config: root %q is not a directory", root)
|
||||
}
|
||||
cfg.Root = root
|
||||
if cfg.Listen == "" {
|
||||
cfg.Listen = ":8080"
|
||||
}
|
||||
if cfg.Footer == "" {
|
||||
cfg.Footer = "kaidan 1.0"
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue