daemonize
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Jake Hillion 2021-03-25 13:50:48 +00:00
parent 9326593844
commit 5587f1f52e
4 changed files with 33 additions and 13 deletions

View File

@ -6,8 +6,8 @@ import (
)
type Options struct {
Config string `short:"c" long:"config" description:"Configuration file location" value-name:"FILE"`
PidFile string `short:"p" long:"pid" description:"PID file location"`
ConfigFile string `short:"c" long:"config" description:"Configuration file location" value-name:"FILE"`
PidFile string `short:"p" long:"pid" description:"PID file location"`
InterfaceName string
}
@ -21,8 +21,8 @@ func ParseFlags() (*Options, error) {
if len(args) > 0 {
o.InterfaceName = args[0]
if o.Config == "" {
o.Config = fmt.Sprintf(DefaultConfigFile, o.InterfaceName)
if o.ConfigFile == "" {
o.ConfigFile = fmt.Sprintf(DefaultConfigFile, o.InterfaceName)
}
if o.PidFile == "" {
o.PidFile = fmt.Sprintf(DefaultPidFile, o.InterfaceName)

2
go.mod
View File

@ -6,6 +6,8 @@ require (
github.com/JakeHillion/taptun v0.0.0-20210320133200-cf0ef75b1bff
github.com/go-playground/validator/v10 v10.4.1
github.com/jessevdk/go-flags v1.5.0
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/sevlyar/go-daemon v0.1.5
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9

4
go.sum
View File

@ -16,10 +16,14 @@ github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LF
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sevlyar/go-daemon v0.1.5 h1:Zy/6jLbM8CfqJ4x4RPr7MJlSKt90f00kNM1D401C+Qk=
github.com/sevlyar/go-daemon v0.1.5/go.mod h1:6dJpPatBT9eUwM5VCw9Bt6CdX9Tk6UWvhW3MebLDRKE=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=

32
main.go
View File

@ -1,8 +1,10 @@
package main
import (
"github.com/sevlyar/go-daemon"
"log"
"mpbl3p/config"
"mpbl3p/flags"
"os"
"os/signal"
"syscall"
@ -11,16 +13,14 @@ import (
func main() {
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
log.Println("loading config...")
var configLoc string
if v, ok := os.LookupEnv("CONFIG_LOC"); ok {
configLoc = v
} else {
configLoc = "config.ini"
o, err := flags.ParseFlags()
if err != nil {
panic(err)
}
c, err := config.LoadConfig(configLoc)
log.Println("loading config...")
c, err := config.LoadConfig(o.ConfigFile)
if err != nil {
panic(err)
}
@ -34,7 +34,21 @@ func main() {
log.Println("starting...")
p.Start()
log.Println("running")
log.Println("forking...")
ctx := &daemon.Context{
PidFileName: o.PidFile,
PidFilePerm: 0644,
}
d, err := ctx.Reborn()
if err != nil {
panic(err)
}
if d != nil {
return
}
signals := make(chan os.Signal)
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)