woe.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package main
  2. // import "fmt"
  3. import "github.com/beoran/woe/server"
  4. import "github.com/beoran/woe/monolog"
  5. import "os"
  6. import "os/exec"
  7. import "flag"
  8. import "fmt"
  9. /* Command line flags. */
  10. var server_mode = flag.Bool("s", false, "Run in server mode");
  11. var server_tcpip = flag.String("l", ":7000", "TCP/IP Address where the server will listen");
  12. /* Need to restart the server or not? */
  13. var server_restart = true
  14. func runServer() (status int) {
  15. monolog.Setup("woe.log", true, false)
  16. defer monolog.Close()
  17. monolog.Info("Starting WOE!")
  18. monolog.Info("Server runs at %s!", *server_tcpip)
  19. woe, err := server.NewServer(*server_tcpip)
  20. if err != nil {
  21. monolog.Error(err.Error())
  22. panic(err)
  23. }
  24. defer woe.Close()
  25. status, err = woe.Serve()
  26. if err != nil {
  27. monolog.Error(err.Error())
  28. panic(err)
  29. }
  30. return status
  31. }
  32. func runSupervisor() (status int) {
  33. monolog.Setup("woe.log", true, false)
  34. defer monolog.Close()
  35. monolog.Info("Starting WOE supervisor!")
  36. for (server_restart) {
  37. // wd , _ := os.Getwd()
  38. exe := fmt.Sprintf("%s", os.Args[0])
  39. argp := fmt.Sprintf("-l=%s", *server_tcpip)
  40. cmd := exec.Command(exe, "-s=true", argp)
  41. monolog.Info("Starting server %s at %s!", exe, *server_tcpip)
  42. cmd.Stderr = os.Stderr
  43. cmd.Stdout = os.Stdout
  44. err := cmd.Run()
  45. monolog.Info("Server at %s shut down!", *server_tcpip)
  46. // monolog.Info("Server output: %s!", out);
  47. if (err != nil ) {
  48. monolog.Error("Server shut down with error %s!", err)
  49. server_restart = false;
  50. return 1
  51. }
  52. }
  53. return 0
  54. }
  55. /* Woe can be run in supervisor mode (the default) or server mode (-s).
  56. * Server mode is the mode in which the real server is run. In supervisor mode,
  57. * woe runs a single woe server in server mode using os/exec. This is used to
  58. * be able to restart the server gracefully on recompile of the sources.
  59. */
  60. func main() {
  61. flag.Parse()
  62. if *server_mode {
  63. os.Exit(runServer())
  64. } else {
  65. os.Exit(runSupervisor())
  66. }
  67. }