diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 68 |
1 files changed, 41 insertions, 27 deletions
@@ -5,18 +5,31 @@ package main import ( "flag" + "fmt" "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/i18n" + "git.thomasvoss.com/euro-cash.eu/src/wikipedia" ) func main() { + Try(os.Chdir(filepath.Dir(os.Args[0]))) + port := flag.Int("port", 8080, "port number") - debugp := flag.Bool("debug", false, "run in debug mode") + flag.BoolVar(&app.Debugp, "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", @@ -29,37 +42,38 @@ func main() { "address to send error messages from") flag.StringVar(&email.Config.Password, "email-password", "", "password to authenticate the email client") + flag.StringVar(&dbx.DBName, "db-name", "eurocash.db", + "database name or ‘:memory:’ for an in-memory database") flag.Parse() - if *debugp { - go watch() - } - src.Run(*port) -} + defer func() { + if p := recover(); p != nil { + if app.Debugp { + log.Println(p) + time.Sleep(1 * time.Second) + app.Restart() + } + email.Send("URGENT: Server Panicked", fmt.Sprint(p)) + } + }() -func watch() { - path, err := os.Executable() - if err != nil { - log.Fatal(err) - } + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + go func() { + <-sigs + atexit.Exec() + os.Exit(0) + }() - ostat, err := os.Stat(path) - if err != nil { - log.Fatal(err) + if app.Debugp { + go watch.File(Try2(os.Executable()), app.Restart) } - for { - nstat, err := os.Stat(path) - if err != nil { - log.Fatal(err) - } - - if nstat.ModTime() != ostat.ModTime() { - if err := syscall.Exec(path, os.Args, os.Environ()); err != nil { - log.Fatal(err) - } - } - - time.Sleep(1 * time.Second) + i18n.Init(Try2(os.OpenRoot("po")).FS(), app.Debugp) + if err := wikipedia.Init(i18n.DefaultPrinter.Bcp); err != nil { + log.Println(err) } + dbx.Init(Try2(os.OpenRoot("src/dbx/sql")).FS()) + app.BuildTemplates(Try2(os.OpenRoot("src/templates")).FS()) + app.Run(*port) } |