Initial commit

This commit is contained in:
2020-10-13 21:21:37 +13:00
parent 0be5068d88
commit 4277fffcf5
12 changed files with 197 additions and 0 deletions

18
src/cmd.go Normal file
View File

@@ -0,0 +1,18 @@
package main
import (
"fmt"
"net"
"os"
)
func main() {
command := ""
// connect to this socket
conn, _ := net.Dial("tcp", "127.0.0.1:8081")
for _, s := range os.Args[1:] {
command += s + " "
}
// send to socket
fmt.Fprintf(conn, command+"\n")
}

89
src/main.go Normal file
View File

@@ -0,0 +1,89 @@
package main
import (
"bufio"
"fmt"
"io"
"net"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"github.com/creack/pty"
)
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
var minram, maxram, args, stop string
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
switch pair[0] {
case "MIN_MEM":
minram = "-Xms" + pair[1]
case "MAX_MEM":
maxram = "-Xmx" + pair[1]
case "ARGS":
args = pair[1]
case "STOP":
stop = pair[1]
}
}
var cmd *exec.Cmd
if args == "" {
cmd = exec.Command("java", minram, maxram, "-jar", "server.jar")
} else {
cmd = exec.Command("java", minram, maxram, args, "-jar", "server.jar")
}
cmd.Dir = "/server"
cmd.Stdout = os.Stdout
tty, err := pty.Start(cmd)
ln, _ := net.Listen("tcp", ":8081")
if err != nil {
panic(err)
}
defer func() {
cmd.Process.Kill()
cmd.Process.Wait()
tty.Close()
ln.Close()
}()
go func() {
sig := <-sigs
if sig == syscall.SIGTERM {
fmt.Println("Stopping server")
tty.WriteString(stop + "\n\r")
}
}()
go func() {
//accept connections from clients
for {
conn, err := ln.Accept()
if err != nil {
continue
}
go handleClient(conn, tty)
}
}()
go func() {
io.Copy(tty, os.Stdin)
}()
cmd.Wait()
os.Exit(0)
}
func handleClient(conn net.Conn, tty *os.File) {
defer conn.Close()
cmd, _ := bufio.NewReader(conn).ReadString('\n')
tty.Write([]byte(cmd))
}