mirror of
https://github.com/bluenviron/mediamtx.git
synced 2025-12-20 02:00:05 -08:00
print a warn message when the configuration file is not found
This commit is contained in:
parent
a6c06d3853
commit
18fe2d057c
3 changed files with 37 additions and 26 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package confwatcher
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
|
|
@ -29,15 +28,13 @@ 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)
|
||||
// use absolute path to support Darwin
|
||||
absolutePath, _ := filepath.Abs(confPath)
|
||||
|
||||
err := inner.Add(absolutePath)
|
||||
if err != nil {
|
||||
inner.Close()
|
||||
return nil, err
|
||||
}
|
||||
err = inner.Add(absolutePath)
|
||||
if err != nil {
|
||||
inner.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
w := &ConfWatcher{
|
||||
|
|
|
|||
28
main.go
28
main.go
|
|
@ -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,10 +75,12 @@ func newProgram(args []string) (*program, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
p.confWatcher, err = confwatcher.New(p.confPath)
|
||||
if err != nil {
|
||||
p.closeAllResources()
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue