58 lines
897 B
Go
58 lines
897 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"io/ioutil"
|
||
|
"os"
|
||
|
"regexp"
|
||
|
|
||
|
"gopkg.in/yaml.v3"
|
||
|
)
|
||
|
|
||
|
type serveConfig struct {
|
||
|
Dir string
|
||
|
Regexp string
|
||
|
Root string
|
||
|
|
||
|
r *regexp.Regexp
|
||
|
}
|
||
|
|
||
|
type serverConfig struct {
|
||
|
Cert string
|
||
|
Key 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.Dir != "" && serve.Dir[len(serve.Dir)-1] == '/' {
|
||
|
serve.Dir = serve.Dir[:len(serve.Dir)-1]
|
||
|
}
|
||
|
if serve.Regexp != "" {
|
||
|
serve.r = regexp.MustCompile(serve.Regexp)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|