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)
signal.Notify(sigs, syscall.SIGTERM)
//get environmental variables
var minram, maxram, args, stop, jar, regex string
for _, e := range os.Environ() {
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)
//check if there is a server jar
if _, err := os.Stat("/server/"+jar); os.IsNotExist(err) {
log.Println("\033[31mJar \"" + jar + "\" not found\033[0m")
os.Exit(1)
}
//create command
var cmd *exec.Cmd
if args == "" {
cmd = exec.Command("java", minram, maxram, "-jar", jar)
@ -58,16 +58,16 @@ func main() {
log.Printf("Args: %s \n\r", args)
}
//start server
cmd.Dir = "/server"
tty, err := pty.Start(cmd)
ln, _ := net.Listen("tcp", ":8081")
if err != nil {
panic(err)
}
//cleanup
defer func() {
cmd.Process.Kill()
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() {
//accept connections from clients
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() {
filter := regexp.MustCompile(regex)
if(regex != "") {
@ -106,20 +108,22 @@ func main() {
fmt.Println(text)
}
}
} else {
} else { //don't filter
io.Copy(os.Stdout, tty)
}
}()
//copy stdin
go func() {
io.Copy(tty, os.Stdin)
}()
cmd.Wait()
cmd.Wait() //wait for server to stop
tty.Write([]byte("\033[31mServer Stopped\033[0m"))
os.Exit(0)
}
//take commands from cmd.go and send it to sdtin of the server
func handleClient(conn net.Conn, tty *os.File) {
defer conn.Close()
cmd, _ := bufio.NewReader(conn).ReadString('\n')