Brian Picciano
8c3e6a2845
In a world where the daemon can manage more than one network, the Daemon is really responsible only for knowing which networks are currently joined, creating/joining/leaving networks, and routing incoming RPC requests to the correct network handler as needed. The new network package, with its Network interface, inherits most of the logic that Daemon used to have, leaving Daemon only the parts needed for the functionality just described. There's a lot of cleanup done here in order to really nail down the separation of concerns between the two, especially around directory creation.
74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mctx"
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
|
)
|
|
|
|
func getAppDirPath() string {
|
|
appDirPath := os.Getenv("APPDIR")
|
|
if appDirPath == "" {
|
|
appDirPath = "."
|
|
}
|
|
return appDirPath
|
|
}
|
|
|
|
var (
|
|
envAppDirPath = getAppDirPath()
|
|
envBinDirPath = filepath.Join(envAppDirPath, "bin")
|
|
)
|
|
|
|
func binPath(name string) string {
|
|
return filepath.Join(envBinDirPath, name)
|
|
}
|
|
|
|
func main() {
|
|
|
|
logger := mlog.NewLogger(&mlog.LoggerOpts{
|
|
MessageHandler: newLogMsgHandler(),
|
|
MaxLevel: mlog.LevelInfo.Int(),
|
|
})
|
|
defer logger.Close()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
signalCh := make(chan os.Signal, 2)
|
|
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
sig := <-signalCh
|
|
cancel()
|
|
|
|
ctx := mctx.Annotate(ctx, "signal", sig.String())
|
|
logger.Info(ctx, "got signal, exiting gracefully")
|
|
|
|
sig = <-signalCh
|
|
|
|
ctx = mctx.Annotate(ctx, "signal", sig.String())
|
|
logger.FatalString(ctx, "second signal received, force quitting, there may be zombie children left behind, good luck!")
|
|
}()
|
|
|
|
err := subCmdCtx{
|
|
Context: ctx,
|
|
args: os.Args[1:],
|
|
logger: logger,
|
|
}.doSubCmd(
|
|
subCmdDaemon,
|
|
subCmdGarage,
|
|
subCmdHost,
|
|
subCmdNebula,
|
|
subCmdNetwork,
|
|
subCmdVersion,
|
|
)
|
|
|
|
if err != nil {
|
|
logger.Fatal(ctx, "error running command", err)
|
|
}
|
|
}
|