diff options
author | Thomas Voss <mail@thomasvoss.com> | 2025-06-06 02:22:37 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2025-06-06 02:22:37 +0200 |
commit | 2e75e396845deb84a079561c85bd3bb2a443afb6 (patch) | |
tree | 8001964d8376572e02536baed8d7a8cb67c90005 /main.go | |
parent | d1fc67e0592249219a5b2010c535966f48f27df3 (diff) |
Implement live-reloading
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -5,6 +5,10 @@ package main import ( "flag" + "log" + "os" + "syscall" + "time" "git.thomasvoss.com/euro-cash.eu/src" "git.thomasvoss.com/euro-cash.eu/src/email" @@ -12,6 +16,7 @@ import ( func main() { port := flag.Int("port", 8080, "port number") + debugp := flag.Bool("debug", false, "run in debug mode") flag.BoolVar(&email.Config.Disabled, "no-email", false, "disables email support") flag.StringVar(&email.Config.Host, "smtp-host", "smtp.migadu.com", @@ -26,5 +31,40 @@ func main() { "password to authenticate the email client") flag.Parse() + if *debugp { + go watch() + } src.Run(*port) } + +func watch() { + path, err := os.Executable() + if err != nil { + die(err) + } + + ostat, err := os.Stat(path) + if err != nil { + die(err) + } + + for { + nstat, err := os.Stat(path) + if err != nil { + die(err) + } + + if nstat.ModTime() != ostat.ModTime() { + if err := syscall.Exec(path, os.Args, os.Environ()); err != nil { + die(err) + } + } + + time.Sleep(1 * time.Second) + } +} + +func die(err error) { + log.Fatal(err) + os.Exit(1) +} |