diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 52 |
1 files changed, 19 insertions, 33 deletions
@@ -5,16 +5,18 @@ package main import ( "flag" - "log" "os" + "os/signal" "path/filepath" "syscall" - "time" + + "git.thomasvoss.com/euro-cash.eu/pkg/atexit" + . "git.thomasvoss.com/euro-cash.eu/pkg/try" + "git.thomasvoss.com/euro-cash.eu/pkg/watch" "git.thomasvoss.com/euro-cash.eu/src" "git.thomasvoss.com/euro-cash.eu/src/dbx" "git.thomasvoss.com/euro-cash.eu/src/email" - . "git.thomasvoss.com/euro-cash.eu/src/try" ) func main() { @@ -38,39 +40,23 @@ func main() { "database name or ‘:memory:’ for an in-memory database") flag.Parse() + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + go func() { + <-sigs + atexit.Exec() + os.Exit(0) + }() + if *debugp { - go watch() + path := Try2(os.Executable()) + go watch.File(path, func() { + atexit.Exec() + Try(syscall.Exec(path, os.Args, os.Environ())) + }) } dbx.Init(Try2(os.OpenRoot("src/dbx/sql")).FS()) - app.BuildTemplates(Try2(os.OpenRoot("src/templates")).FS()) + app.BuildTemplates(Try2(os.OpenRoot("src/templates")).FS(), *debugp) app.Run(*port) } - -func watch() { - path, err := os.Executable() - if err != nil { - log.Fatal(err) - } - - ostat, err := os.Stat(path) - if err != nil { - log.Fatal(err) - } - - for { - nstat, err := os.Stat(path) - if err != nil { - log.Fatal(err) - } - - if nstat.ModTime() != ostat.ModTime() { - dbx.Close() - if err := syscall.Exec(path, os.Args, os.Environ()); err != nil { - log.Fatal(err) - } - } - - time.Sleep(1 * time.Second) - } -} |