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.
57 lines
897 B
57 lines
897 B
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 |
|
}
|
|
|