print a warn message when the configuration file is not found

This commit is contained in:
aler9 2020-12-06 22:29:56 +01:00
parent a6c06d3853
commit 18fe2d057c
3 changed files with 37 additions and 26 deletions

View file

@ -145,45 +145,45 @@ func (conf *Conf) fillAndCheck() error {
}
// Load loads a Conf.
func Load(fpath string) (*Conf, error) {
func Load(fpath string) (*Conf, bool, error) {
conf := &Conf{}
// read from file
err := func() error {
found, err := func() (bool, error) {
// rtsp-simple-server.yml is optional
if fpath == "rtsp-simple-server.yml" {
if _, err := os.Stat(fpath); err != nil {
return nil
return false, nil
}
}
f, err := os.Open(fpath)
if err != nil {
return err
return false, err
}
defer f.Close()
err = yaml.NewDecoder(f).Decode(conf)
if err != nil {
return err
return false, err
}
return nil
return true, nil
}()
if err != nil {
return nil, err
return nil, false, err
}
// read from environment
err = confenv.Load("RTSP", conf)
if err != nil {
return nil, err
return nil, false, err
}
err = conf.fillAndCheck()
if err != nil {
return nil, err
return nil, false, err
}
return conf, nil
return conf, found, nil
}

View file

@ -1,7 +1,6 @@
package confwatcher
import (
"os"
"path/filepath"
"time"
@ -29,16 +28,14 @@ func New(confPath string) (*ConfWatcher, error) {
return nil, err
}
if _, err := os.Stat(confPath); err == nil {
// use absolute path to support Darwin
absolutePath, _ := filepath.Abs(confPath)
err := inner.Add(absolutePath)
err = inner.Add(absolutePath)
if err != nil {
inner.Close()
return nil, err
}
}
w := &ConfWatcher{
inner: inner,

20
main.go
View file

@ -27,6 +27,7 @@ var version = "v0.0.0"
type program struct {
confPath string
conf *conf.Conf
confFound bool
stats *stats.Stats
logHandler *loghandler.LogHandler
metrics *metrics.Metrics
@ -63,7 +64,7 @@ func newProgram(args []string) (*program, error) {
}
var err error
p.conf, err = conf.Load(p.confPath)
p.conf, p.confFound, err = conf.Load(p.confPath)
if err != nil {
return nil, err
}
@ -74,11 +75,13 @@ func newProgram(args []string) (*program, error) {
return nil, err
}
if p.confFound {
p.confWatcher, err = confwatcher.New(p.confPath)
if err != nil {
p.closeAllResources()
return nil, err
}
}
go p.run()
@ -102,10 +105,17 @@ func (p *program) Log(format string, args ...interface{}) {
func (p *program) run() {
defer close(p.done)
confChanged := func() chan struct{} {
if p.confWatcher != nil {
return p.confWatcher.Watch()
}
return make(chan struct{})
}()
outer:
for {
select {
case <-p.confWatcher.Watch():
case <-confChanged:
err := p.reloadConf()
if err != nil {
p.Log("ERR: %s", err)
@ -136,6 +146,10 @@ func (p *program) createDynamicResources(initial bool) error {
if initial {
p.Log("rtsp-simple-server %s", version)
if !p.confFound {
p.Log("configuration file not found, using the default one")
}
}
if p.conf.Metrics {
@ -239,7 +253,7 @@ func (p *program) closeAllResources() {
func (p *program) reloadConf() error {
p.Log("reloading configuration")
conf, err := conf.Load(p.confPath)
conf, _, err := conf.Load(p.confPath)
if err != nil {
return err
}