run without service #407

This commit is contained in:
ffdfgdfg 2020-02-18 22:53:37 +08:00
parent 876d3ccc6f
commit 3ec790d98d
2 changed files with 40 additions and 20 deletions

View File

@ -15,6 +15,7 @@ import (
"os" "os"
"runtime" "runtime"
"strings" "strings"
"sync"
"time" "time"
) )
@ -107,7 +108,12 @@ func main() {
} }
s, err := service.New(prg, svcConfig) s, err := service.New(prg, svcConfig)
if err != nil { if err != nil {
logs.Error(err) logs.Error(err, "service function disabled")
run()
// run without service
wg := sync.WaitGroup{}
wg.Add(1)
wg.Wait()
return return
} }
if len(os.Args) >= 2 { if len(os.Args) >= 2 {
@ -172,6 +178,15 @@ func (p *npc) run() error {
logs.Warning("npc: panic serving %v: %v\n%s", err, string(buf)) logs.Warning("npc: panic serving %v: %v\n%s", err, string(buf))
} }
}() }()
run()
select {
case <-p.exit:
logs.Warning("stop...")
}
return nil
}
func run() {
common.InitPProfFromArg(*pprofAddr) common.InitPProfFromArg(*pprofAddr)
//p2p or secret command //p2p or secret command
if *password != "" { if *password != "" {
@ -187,7 +202,7 @@ func (p *npc) run() error {
commonConfig.Client = new(file.Client) commonConfig.Client = new(file.Client)
commonConfig.Client.Cnf = new(file.Config) commonConfig.Client.Cnf = new(file.Config)
go client.StartLocalServer(localServer, commonConfig) go client.StartLocalServer(localServer, commonConfig)
return nil return
} }
env := common.GetEnvMap() env := common.GetEnvMap()
if *serverAddr == "" { if *serverAddr == "" {
@ -211,9 +226,4 @@ func (p *npc) run() error {
} }
go client.StartFromFile(*configPath) go client.StartFromFile(*configPath)
} }
select {
case <-p.exit:
logs.Warning("stop...")
}
return nil
} }

View File

@ -1,26 +1,27 @@
package main package main
import ( import (
"ehang.io/nps/lib/crypt"
"ehang.io/nps/lib/file"
"ehang.io/nps/lib/install" "ehang.io/nps/lib/install"
"ehang.io/nps/lib/version"
"ehang.io/nps/server"
"ehang.io/nps/server/connection"
"ehang.io/nps/server/tool"
"ehang.io/nps/web/routers"
"flag" "flag"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"sync"
"ehang.io/nps/lib/common" "ehang.io/nps/lib/common"
"ehang.io/nps/lib/crypt"
"ehang.io/nps/lib/daemon" "ehang.io/nps/lib/daemon"
"ehang.io/nps/lib/file"
"ehang.io/nps/lib/version"
"ehang.io/nps/server"
"ehang.io/nps/server/connection"
"ehang.io/nps/server/tool"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
"ehang.io/nps/web/routers"
"github.com/kardianos/service" "github.com/kardianos/service"
) )
@ -97,7 +98,12 @@ func main() {
prg.exit = make(chan struct{}) prg.exit = make(chan struct{})
s, err := service.New(prg, svcConfig) s, err := service.New(prg, svcConfig)
if err != nil { if err != nil {
logs.Error(err) logs.Error(err, "service function disabled")
run()
// run without service
wg := sync.WaitGroup{}
wg.Add(1)
wg.Wait()
return return
} }
if len(os.Args) > 1 && os.Args[1] != "service" { if len(os.Args) > 1 && os.Args[1] != "service" {
@ -166,6 +172,15 @@ func (p *nps) run() error {
logs.Warning("nps: panic serving %v: %v\n%s", err, string(buf)) logs.Warning("nps: panic serving %v: %v\n%s", err, string(buf))
} }
}() }()
run()
select {
case <-p.exit:
logs.Warning("stop...")
}
return nil
}
func run() {
routers.Init() routers.Init()
task := &file.Tunnel{ task := &file.Tunnel{
Mode: "webServer", Mode: "webServer",
@ -181,9 +196,4 @@ func (p *nps) run() error {
tool.InitAllowPort() tool.InitAllowPort()
tool.StartSystemInfo() tool.StartSystemInfo()
go server.StartNewServer(bridgePort, task, beego.AppConfig.String("bridge_type")) go server.StartNewServer(bridgePort, task, beego.AppConfig.String("bridge_type"))
select {
case <-p.exit:
logs.Warning("stop...")
}
return nil
} }