support setting external configure file path;

支持设置外部配置文件路径;
pull/132/head
macbookpro 6 years ago
parent 909ffa0f2b
commit cde2d9b6fc

1
.gitignore vendored

@ -11,3 +11,4 @@ node_modules
/*.dev.ini /*.dev.ini
/*.dev.db /*.dev.db
.idea .idea
easydarwin_debug.ini

@ -7,3 +7,6 @@ default_password=admin
port=554 port=554
timeout=28800 timeout=28800
gop_cache_enable=1 gop_cache_enable=1
save_stream_to_mp4=0
ffmpeg_path=
mp4_dir_path=

@ -2,12 +2,15 @@ package main
import ( import (
"context" "context"
"flag"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os" "strings"
"time" "time"
"github.com/go-ini/ini"
"github.com/EasyDarwin/EasyDarwin/models" "github.com/EasyDarwin/EasyDarwin/models"
"github.com/EasyDarwin/EasyDarwin/routers" "github.com/EasyDarwin/EasyDarwin/routers"
"github.com/EasyDarwin/EasyDarwin/rtsp" "github.com/EasyDarwin/EasyDarwin/rtsp"
@ -128,12 +131,29 @@ func (p *program) Stop(s service.Service) (err error) {
} }
func main() { func main() {
configPath := flag.String("config", "", "configure file path")
flag.Parse()
tail := flag.Args()
log.SetPrefix("[EasyDarwin] ") log.SetPrefix("[EasyDarwin] ")
log.SetFlags(log.LstdFlags) log.SetFlags(log.LstdFlags)
if utils.Debug { if utils.Debug {
log.SetFlags(log.Lshortfile | log.LstdFlags) 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{ svcConfig := &service.Config{
Name: sec.Key("name").MustString("EasyDarwin_Service"), Name: sec.Key("name").MustString("EasyDarwin_Service"),
DisplayName: sec.Key("display_name").MustString("EasyDarwin_Service"), DisplayName: sec.Key("display_name").MustString("EasyDarwin_Service"),
@ -147,23 +167,27 @@ func main() {
rtspPort: rtspServer.TCPPort, rtspPort: rtspServer.TCPPort,
rtspServer: rtspServer, rtspServer: rtspServer,
} }
var s, err = service.New(p, svcConfig) s, err := service.New(p, svcConfig)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
utils.PauseExit() utils.PauseExit()
} }
if len(os.Args) > 1 { if len(tail) > 0 {
if os.Args[1] == "install" || os.Args[1] == "stop" { 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() figure.NewFigure("EasyDarwin", "", false).Print()
} }
log.Println(svcConfig.Name, os.Args[1], "...") log.Println(svcConfig.Name, cmd, "...")
if err = service.Control(s, os.Args[1]); err != nil { if err = service.Control(s, cmd); err != nil {
log.Println(err) log.Println(err)
utils.PauseExit() utils.PauseExit()
} }
log.Println(svcConfig.Name, os.Args[1], "ok") log.Println(svcConfig.Name, cmd, "ok")
return return
} }
}
figure.NewFigure("EasyDarwin", "", false).Print() figure.NewFigure("EasyDarwin", "", false).Print()
if err = s.Run(); err != nil { if err = s.Run(); err != nil {
log.Println(err) log.Println(err)

@ -24,15 +24,18 @@ type Server struct {
removePusherCh chan *Pusher removePusherCh chan *Pusher
} }
var Instance *Server = &Server{ var Instance *Server = nil
func GetServer() *Server {
if Instance == nil {
Instance = &Server{
Stoped: true, Stoped: true,
TCPPort: utils.Conf().Section("rtsp").Key("port").MustInt(554), TCPPort: utils.Conf().Section("rtsp").Key("port").MustInt(554),
pushers: make(map[string]*Pusher), pushers: make(map[string]*Pusher),
addPusherCh: make(chan *Pusher), addPusherCh: make(chan *Pusher),
removePusherCh: make(chan *Pusher), removePusherCh: make(chan *Pusher),
} }
}
func GetServer() *Server {
return Instance return Instance
} }

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash
CWD=$(cd "$(dirname $0)";pwd) CWD=$(cd "$(dirname $0)";pwd)
$CWD/easydarwin install "$CWD"/easydarwin install
$CWD/easydarwin start "$CWD"/easydarwin start

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash
CWD=$(cd "$(dirname $0)";pwd) CWD=$(cd "$(dirname $0)";pwd)
$CWD/easydarwin stop "$CWD"/easydarwin stop
$CWD/easydarwin uninstall "$CWD"/easydarwin uninstall

@ -134,6 +134,18 @@ func Conf() *ini.File {
return conf 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 { func ReloadConf() *ini.File {
if _conf, err := ini.InsensitiveLoad(ConfFile()); err != nil { if _conf, err := ini.InsensitiveLoad(ConfFile()); err != nil {
_conf, _ = ini.LoadSources(ini.LoadOptions{Insensitive: true}, []byte("")) _conf, _ = ini.LoadSources(ini.LoadOptions{Insensitive: true}, []byte(""))

Loading…
Cancel
Save