1
0
Fork 0
forked from External/mediamtx

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. // Load loads a Conf.
func Load(fpath string) (*Conf, error) { func Load(fpath string) (*Conf, bool, error) {
conf := &Conf{} conf := &Conf{}
// read from file // read from file
err := func() error { found, err := func() (bool, error) {
// rtsp-simple-server.yml is optional // rtsp-simple-server.yml is optional
if fpath == "rtsp-simple-server.yml" { if fpath == "rtsp-simple-server.yml" {
if _, err := os.Stat(fpath); err != nil { if _, err := os.Stat(fpath); err != nil {
return nil return false, nil
} }
} }
f, err := os.Open(fpath) f, err := os.Open(fpath)
if err != nil { if err != nil {
return err return false, err
} }
defer f.Close() defer f.Close()
err = yaml.NewDecoder(f).Decode(conf) err = yaml.NewDecoder(f).Decode(conf)
if err != nil { if err != nil {
return err return false, err
} }
return nil return true, nil
}() }()
if err != nil { if err != nil {
return nil, err return nil, false, err
} }
// read from environment // read from environment
err = confenv.Load("RTSP", conf) err = confenv.Load("RTSP", conf)
if err != nil { if err != nil {
return nil, err return nil, false, err
} }
err = conf.fillAndCheck() err = conf.fillAndCheck()
if err != nil { 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 package confwatcher
import ( import (
"os"
"path/filepath" "path/filepath"
"time" "time"
@ -29,15 +28,13 @@ func New(confPath string) (*ConfWatcher, error) {
return nil, err return nil, err
} }
if _, err := os.Stat(confPath); err == nil { // use absolute path to support Darwin
// use absolute path to support Darwin absolutePath, _ := filepath.Abs(confPath)
absolutePath, _ := filepath.Abs(confPath)
err := inner.Add(absolutePath) err = inner.Add(absolutePath)
if err != nil { if err != nil {
inner.Close() inner.Close()
return nil, err return nil, err
}
} }
w := &ConfWatcher{ w := &ConfWatcher{

28
main.go
View file

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