-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
107 lines (92 loc) · 2.07 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
)
var output = make(chan string)
func main() {
xmx := flag.String("xmx", "1024M", "The Maximum Memory Allocation Pool for the JVM")
xms := flag.String("xms", "1024M", "The Initial Memory Allocation Pool")
dir := flag.String("dir", ".", "Path to the MC server directory")
jar := flag.String("jar", "minecraft_server.jar", "Path to the MC server JAR relative from -dir")
flag.Parse()
// The minecraft server command
cmd := exec.Command("java", "-Xmx"+*xmx, "-Xms"+*xms, "-d64", "-jar", *jar, "nogui")
cmd.Dir = *dir
// Control StdIn
stdin, err := cmd.StdinPipe()
if err != nil {
log.Fatal(err)
}
// Pipe the process' stdout to stdout for monitoring
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(err)
}
// Kick off the process
if err := cmd.Start(); err != nil {
log.Fatal(err)
}
// A routine to keep the channel updated
go func() {
var hasStarted bool
buf := make([]byte, 1024)
for {
n, err := stdout.Read(buf)
if n != 0 {
// Something there throw it into the channel
output <- string(buf[:n])
}
if err != nil {
fmt.Print("Caught: ", err, "\n")
// Error, break out of loop
break
}
hasStarted = true
}
fmt.Println("mcman stopping")
if !hasStarted {
fmt.Println("Couldn't get things running. Is minecraft available?")
}
DoStopServer()
close(output)
}()
// Load the Config
mm := NewManager(stdin)
LoadConfig(&mm, *dir)
go func() {
for s := range output {
m := NewMessage(s)
fmt.Printf("\x1b[34;1m%s\x1b[0m", m.Output())
mm.ProcessMessage(m)
}
}()
// Web Server Routine
go func() {
StartServer(true)
}()
// Catch interrupt signals and gracefully stop the servers
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
fmt.Println("Caught signal", sig)
DoStopServer()
}()
// The forever loop to monitor everything
for {
time.Sleep(time.Second)
mu.Lock()
if StopServer {
mu.Unlock()
break
}
mu.Unlock()
}
}