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