woe.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package main
  2. // import "fmt"
  3. import "gitlab.com/beoran/woe/server"
  4. import "gitlab.com/beoran/woe/monolog"
  5. import "gitlab.com/beoran/woe/raku"
  6. import "os"
  7. import "os/exec"
  8. import "flag"
  9. import "fmt"
  10. type serverLogLevels []string
  11. var server_loglevels serverLogLevels = serverLogLevels{
  12. "FATAL", "ERROR", "WARNING", "INFO",
  13. }
  14. /* Command line flags. */
  15. var server_mode = flag.Bool("s", false, "Run in server mode")
  16. var raku_mode = flag.Bool("r", false, "Run in Raku interpreter mode")
  17. var server_tcpip = flag.String("l", ":7000", "TCP/IP Address where the server will listen")
  18. var enable_logs = flag.String("el", "FATAL,ERROR,WARNING,INFO", "Log levels to enable")
  19. var disable_logs = flag.String("dl", "", "Log levels to disable")
  20. func enableDisableLogs() {
  21. monolog.EnableLevels(*enable_logs)
  22. monolog.DisableLevels(*disable_logs)
  23. }
  24. /* Need to restart the server or not? */
  25. var server_restart = true
  26. func runServer() (status int) {
  27. monolog.Setup("woe.log", true, false)
  28. defer monolog.Close()
  29. enableDisableLogs()
  30. monolog.Info("Starting WOE server...")
  31. monolog.Info("Server will run at %s.", *server_tcpip)
  32. woe, err := server.NewServer(*server_tcpip)
  33. if err != nil {
  34. monolog.Error("Could not initialize server!")
  35. monolog.Error(err.Error())
  36. panic(err)
  37. }
  38. monolog.Info("Server at %s init ok.", *server_tcpip)
  39. defer woe.Close()
  40. status, err = woe.Serve()
  41. if err != nil {
  42. monolog.Error("Error while running WOE server!")
  43. monolog.Error(err.Error())
  44. panic(err)
  45. }
  46. monolog.Info("Server shut down without error indication.", *server_tcpip)
  47. return status
  48. }
  49. func runSupervisor() (status int) {
  50. monolog.Setup("woe.log", true, false)
  51. defer monolog.Close()
  52. enableDisableLogs()
  53. monolog.Info("Starting WOE supervisor.")
  54. for server_restart {
  55. // wd , _ := os.Getwd()
  56. exe := fmt.Sprintf("%s", os.Args[0])
  57. argp := fmt.Sprintf("-l=%s", *server_tcpip)
  58. argel := fmt.Sprintf("-el=%s", *enable_logs)
  59. argdl := fmt.Sprintf("-dl=%s", *disable_logs)
  60. cmd := exec.Command(exe, "-s=true", argp, argel, argdl)
  61. monolog.Info("Starting server %s at %s.", exe, *server_tcpip)
  62. cmd.Stderr = os.Stderr
  63. cmd.Stdout = os.Stdout
  64. monolog.Debug("Server command line: %s.", cmd.Args)
  65. err := cmd.Run()
  66. monolog.Info("Server at %s shut down.", *server_tcpip)
  67. if err != nil {
  68. monolog.Error("Server shut down with error %s!", err)
  69. server_restart = false
  70. return 1
  71. }
  72. }
  73. return 0
  74. }
  75. func runRaku() (status int) {
  76. lexer := raku.OpenLexer(os.Stdin)
  77. _ = lexer
  78. return 0
  79. }
  80. /* Woe can be run in supervisor mode (the default) or server mode (-s).
  81. * Server mode is the mode in which the real server is run. In supervisor mode,
  82. * woe runs a single woe server in server mode using os/exec. This is used to
  83. * be able to restart the server gracefully on recompile of the sources.
  84. */
  85. func main() {
  86. defer func() {
  87. pani := recover()
  88. if pani != nil {
  89. monolog.Fatal("Panic: %s", pani)
  90. os.Exit(255)
  91. }
  92. }()
  93. flag.Parse()
  94. if *server_mode {
  95. os.Exit(runServer())
  96. } else if *raku_mode {
  97. os.Exit(runRaku())
  98. } else {
  99. os.Exit(runSupervisor())
  100. }
  101. }