From cde2d9b6fcc8156ff1f3eb8276334100c2d9fced Mon Sep 17 00:00:00 2001 From: macbookpro Date: Sat, 17 Nov 2018 12:50:34 +0800 Subject: [PATCH] =?UTF-8?q?support=20setting=20external=20configure=20file?= =?UTF-8?q?=20path;=20=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE=E5=A4=96?= =?UTF-8?q?=E9=83=A8=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + easydarwin.ini | 5 +- main.go | 50 ++++++++++++++----- rtsp/rtsp-server.go | 17 ++++--- start.sh | 4 +- stop.sh | 4 +- .../penggy/EasyGoLib/utils/utils.go | 12 +++++ 7 files changed, 68 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 87cdc147..3bf5853f 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ node_modules /*.dev.ini /*.dev.db .idea +easydarwin_debug.ini diff --git a/easydarwin.ini b/easydarwin.ini index a22c4f75..3aa3c8b7 100644 --- a/easydarwin.ini +++ b/easydarwin.ini @@ -6,4 +6,7 @@ default_password=admin [rtsp] port=554 timeout=28800 -gop_cache_enable=1 \ No newline at end of file +gop_cache_enable=1 +save_stream_to_mp4=0 +ffmpeg_path= +mp4_dir_path= \ No newline at end of file diff --git a/main.go b/main.go index 8bc12db2..d9dfa665 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,15 @@ package main import ( "context" + "flag" "fmt" "log" "net/http" - "os" + "strings" "time" + "github.com/go-ini/ini" + "github.com/EasyDarwin/EasyDarwin/models" "github.com/EasyDarwin/EasyDarwin/routers" "github.com/EasyDarwin/EasyDarwin/rtsp" @@ -128,12 +131,29 @@ func (p *program) Stop(s service.Service) (err error) { } func main() { + configPath := flag.String("config", "", "configure file path") + flag.Parse() + tail := flag.Args() log.SetPrefix("[EasyDarwin] ") log.SetFlags(log.LstdFlags) if utils.Debug { log.SetFlags(log.Lshortfile | log.LstdFlags) } - sec := utils.Conf().Section("service") + var conf *ini.File + var err error + if len(*configPath) != 0 { + log.Printf("use config file[%s]", *configPath) + conf, err = utils.ConfByPath(*configPath) + } else { + conf = utils.Conf() + err = nil + } + if err != nil { + log.Print(err) + log.Printf("EasyDarwin terminate due to config not found.") + return + } + sec := conf.Section("service") svcConfig := &service.Config{ Name: sec.Key("name").MustString("EasyDarwin_Service"), DisplayName: sec.Key("display_name").MustString("EasyDarwin_Service"), @@ -147,22 +167,26 @@ func main() { rtspPort: rtspServer.TCPPort, rtspServer: rtspServer, } - var s, err = service.New(p, svcConfig) + s, err := service.New(p, svcConfig) if err != nil { log.Println(err) utils.PauseExit() } - if len(os.Args) > 1 { - if os.Args[1] == "install" || os.Args[1] == "stop" { - figure.NewFigure("EasyDarwin", "", false).Print() - } - log.Println(svcConfig.Name, os.Args[1], "...") - if err = service.Control(s, os.Args[1]); err != nil { - log.Println(err) - utils.PauseExit() + if len(tail) > 0 { + cmd := tail[0] + cmd = strings.ToLower(cmd) + if cmd == "install" || cmd == "stop" || cmd == "start" || cmd == "uninstall" { + if cmd == "install" || cmd == "stop" { + figure.NewFigure("EasyDarwin", "", false).Print() + } + log.Println(svcConfig.Name, cmd, "...") + if err = service.Control(s, cmd); err != nil { + log.Println(err) + utils.PauseExit() + } + log.Println(svcConfig.Name, cmd, "ok") + return } - log.Println(svcConfig.Name, os.Args[1], "ok") - return } figure.NewFigure("EasyDarwin", "", false).Print() if err = s.Run(); err != nil { diff --git a/rtsp/rtsp-server.go b/rtsp/rtsp-server.go index 892b5740..2b977f0d 100644 --- a/rtsp/rtsp-server.go +++ b/rtsp/rtsp-server.go @@ -24,15 +24,18 @@ type Server struct { removePusherCh chan *Pusher } -var Instance *Server = &Server{ - Stoped: true, - TCPPort: utils.Conf().Section("rtsp").Key("port").MustInt(554), - pushers: make(map[string]*Pusher), - addPusherCh: make(chan *Pusher), - removePusherCh: make(chan *Pusher), -} +var Instance *Server = nil func GetServer() *Server { + if Instance == nil { + Instance = &Server{ + Stoped: true, + TCPPort: utils.Conf().Section("rtsp").Key("port").MustInt(554), + pushers: make(map[string]*Pusher), + addPusherCh: make(chan *Pusher), + removePusherCh: make(chan *Pusher), + } + } return Instance } diff --git a/start.sh b/start.sh index da92c556..a46abf62 100644 --- a/start.sh +++ b/start.sh @@ -1,4 +1,4 @@ #!/bin/bash CWD=$(cd "$(dirname $0)";pwd) -$CWD/easydarwin install -$CWD/easydarwin start \ No newline at end of file +"$CWD"/easydarwin install +"$CWD"/easydarwin start \ No newline at end of file diff --git a/stop.sh b/stop.sh index 480814b3..f0e3e8ff 100644 --- a/stop.sh +++ b/stop.sh @@ -1,4 +1,4 @@ #!/bin/bash CWD=$(cd "$(dirname $0)";pwd) -$CWD/easydarwin stop -$CWD/easydarwin uninstall \ No newline at end of file +"$CWD"/easydarwin stop +"$CWD"/easydarwin uninstall \ No newline at end of file diff --git a/vendor/github.com/penggy/EasyGoLib/utils/utils.go b/vendor/github.com/penggy/EasyGoLib/utils/utils.go index e49a916f..213c9cd0 100644 --- a/vendor/github.com/penggy/EasyGoLib/utils/utils.go +++ b/vendor/github.com/penggy/EasyGoLib/utils/utils.go @@ -134,6 +134,18 @@ func Conf() *ini.File { return conf } +func ConfByPath(path string) (*ini.File, error) { + if conf != nil { + return conf, nil + } + if _conf, err := ini.InsensitiveLoad(path); err != nil { + return nil, err + } else { + conf = _conf + } + return conf, nil +} + func ReloadConf() *ini.File { if _conf, err := ini.InsensitiveLoad(ConfFile()); err != nil { _conf, _ = ini.LoadSources(ini.LoadOptions{Insensitive: true}, []byte(""))