twins/config.go

66 lines
1.1 KiB
Go

package main
import (
"errors"
"io/ioutil"
"log"
"os"
"regexp"
"gopkg.in/yaml.v3"
)
type serveConfig struct {
Path string
Root string
Proxy string
r *regexp.Regexp
}
type serverConfig struct {
Cert string
Key string
Address string
Serve []*serveConfig
}
var config = &serverConfig{}
func readconfig(configPath string) error {
if configPath == "" {
return errors.New("file unspecified")
}
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return errors.New("file not found")
}
configData, err := ioutil.ReadFile(configPath)
if err != nil {
return err
}
err = yaml.Unmarshal(configData, &config)
if err != nil {
return err
}
for _, serve := range config.Serve {
if serve.Path == "" {
log.Fatal("path must be specified in serve entry")
} else if serve.Root != "" && serve.Proxy != "" {
log.Fatal("only one root or reverse proxy may defined for a serve entry")
}
if serve.Path[0] == '^' {
serve.r = regexp.MustCompile(serve.Path)
} else if serve.Path[len(serve.Path)-1] == '/' {
serve.Path = serve.Path[:len(serve.Path)-1]
}
}
return nil
}