Add comments

This commit is contained in:
Jimmy 2021-11-03 18:59:52 +13:00
parent 88718390fa
commit 0d05c20961
1 changed files with 14 additions and 10 deletions

View File

@ -21,6 +21,7 @@ func main() {
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM) signal.Notify(sigs, syscall.SIGTERM)
//get environmental variables
var minram, maxram, args, stop, jar, regex string var minram, maxram, args, stop, jar, regex string
for _, e := range os.Environ() { for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2) pair := strings.SplitN(e, "=", 2)
@ -40,16 +41,15 @@ func main() {
} }
} }
log.Printf("Ram: Min: %s Max: %s Stop: %s Jar: %s\n\r", minram, maxram, stop, jar) log.Printf("Ram: Min: %s Max: %s Stop: %s Jar: %s\n\r", minram, maxram, stop, jar)
//check if there is a server jar
if _, err := os.Stat("/server/"+jar); os.IsNotExist(err) { if _, err := os.Stat("/server/"+jar); os.IsNotExist(err) {
log.Println("\033[31mJar \"" + jar + "\" not found\033[0m") log.Println("\033[31mJar \"" + jar + "\" not found\033[0m")
os.Exit(1) os.Exit(1)
} }
//create command
var cmd *exec.Cmd var cmd *exec.Cmd
if args == "" { if args == "" {
cmd = exec.Command("java", minram, maxram, "-jar", jar) cmd = exec.Command("java", minram, maxram, "-jar", jar)
@ -58,16 +58,16 @@ func main() {
log.Printf("Args: %s \n\r", args) log.Printf("Args: %s \n\r", args)
} }
//start server
cmd.Dir = "/server" cmd.Dir = "/server"
tty, err := pty.Start(cmd) tty, err := pty.Start(cmd)
ln, _ := net.Listen("tcp", ":8081")
if err != nil { if err != nil {
panic(err) panic(err)
} }
//cleanup
defer func() { defer func() {
cmd.Process.Kill() cmd.Process.Kill()
cmd.Process.Wait() cmd.Process.Wait()
@ -84,6 +84,8 @@ func main() {
} }
}() }()
//accepts command from cmd.go and sends them to the server
ln, _ := net.Listen("tcp", ":8081")
go func() { go func() {
//accept connections from clients //accept connections from clients
for { for {
@ -95,7 +97,7 @@ func main() {
} }
}() }()
//copy to standard out. If the the is a regex pattern, do output anything that matches
go func() { go func() {
filter := regexp.MustCompile(regex) filter := regexp.MustCompile(regex)
if(regex != "") { if(regex != "") {
@ -106,20 +108,22 @@ func main() {
fmt.Println(text) fmt.Println(text)
} }
} }
} else { } else { //don't filter
io.Copy(os.Stdout, tty) io.Copy(os.Stdout, tty)
} }
}() }()
//copy stdin
go func() { go func() {
io.Copy(tty, os.Stdin) io.Copy(tty, os.Stdin)
}() }()
cmd.Wait() cmd.Wait() //wait for server to stop
tty.Write([]byte("\033[31mServer Stopped\033[0m")) tty.Write([]byte("\033[31mServer Stopped\033[0m"))
os.Exit(0) os.Exit(0)
} }
//take commands from cmd.go and send it to sdtin of the server
func handleClient(conn net.Conn, tty *os.File) { func handleClient(conn net.Conn, tty *os.File) {
defer conn.Close() defer conn.Close()
cmd, _ := bufio.NewReader(conn).ReadString('\n') cmd, _ := bufio.NewReader(conn).ReadString('\n')