From 0ad7b3f11093a82d54efac1e907346879b27d2b6 Mon Sep 17 00:00:00 2001 From: Merith-TK Date: Sat, 16 Mar 2024 18:53:16 -0700 Subject: [PATCH] update code to current --- .gitignore | 2 ++ cmd/client/main.go | 39 ++++++++++++++++++++++++++++++++------- cmd/server/runtime.go | 12 ++++++++---- makefile | 10 ++++++++++ source.txt | 4 ++-- workspace/main.py | 5 +---- 6 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 .gitignore create mode 100644 makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4954cc0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.exe +workspace \ No newline at end of file diff --git a/cmd/client/main.go b/cmd/client/main.go index 4e53f70..06c3498 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "fmt" "io/ioutil" "log" @@ -13,6 +14,7 @@ import ( ) func main() { + log.Println("Starting client...") interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt) @@ -59,19 +61,42 @@ func main() { // create execConn off of / endpoint + // BLAME COPILOT FOR THIS + // Send the contents over the lang websocket + sendSourceData := func() { + err = execConn.WriteMessage(websocket.TextMessage, sourceData) + if err != nil { + log.Fatal("Failed to send source data over lang websocket:", err) + } + } + + sendSourceData() + + // Listen for space key press in console + go func() { + reader := bufio.NewReader(os.Stdin) + for { + char, _, err := reader.ReadRune() + if err != nil { + log.Fatal("Failed to read from console:", err) + } + + if char == ' ' { + sendSourceData() + } + } + }() + // Send the contents over the lang websocket err = execConn.WriteMessage(websocket.TextMessage, sourceData) if err != nil { log.Fatal("Failed to send source data over lang websocket:", err) } - select { - case <-interrupt: - fmt.Println("Interrupt signal received, closing connections...") - close(done) - time.Sleep(1 * time.Second) - return - } + <-interrupt + fmt.Println("Interrupt signal received, closing connections...") + close(done) + time.Sleep(1 * time.Second) } func connectWebSocket(urlStr string) (*websocket.Conn, error) { diff --git a/cmd/server/runtime.go b/cmd/server/runtime.go index ce538b3..04c9226 100644 --- a/cmd/server/runtime.go +++ b/cmd/server/runtime.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "os/exec" "path/filepath" @@ -12,13 +13,18 @@ import ( ) func handleCase(caseName, data string) string { - // caseName = strings.ReplaceAll(caseName, "\n", "") + caseName = strings.ReplaceAll(caseName, "\n", "") + caseName = strings.ReplaceAll(caseName, "\r", "") caseName = strings.ReplaceAll(caseName, ":", "") log.Println("Handling case", caseName) switch caseName { case "python", "python3", "py": + log.Println(data) runPython(data) + default: + fmt.Printf("caseName: %q\n", caseName) + log.Println(data) } return "" } @@ -57,10 +63,8 @@ func runPython(data string) { cmd := exec.Command(fname, "./main.py") cmd.Dir = filepath.ToSlash("./workspace/") - // set the command's stdout to the pipe writer - cmd.Stdout = pyWriter - // set the command's stderr to the pipe writer + cmd.Stdout = pyWriter cmd.Stderr = pyWriter // start the command diff --git a/makefile b/makefile new file mode 100644 index 0000000..d21454e --- /dev/null +++ b/makefile @@ -0,0 +1,10 @@ +default: runserver + +setup: + -mkdir -p workspace +runserver: setup + go build -o workspace/server.exe ./cmd/server + ./workspace/server.exe +runclient: setup + go build -o workspace/client.exe ./cmd/client + ./workspace/client.exe diff --git a/source.txt b/source.txt index f36556a..3299e13 100644 --- a/source.txt +++ b/source.txt @@ -1,2 +1,2 @@ -:py -print("fuckme") \ No newline at end of file +:python +print("log test") \ No newline at end of file diff --git a/workspace/main.py b/workspace/main.py index 4d779d5..2f8e6be 100644 --- a/workspace/main.py +++ b/workspace/main.py @@ -1,4 +1 @@ -import time -for i in range(10): - print(i) - time.sleep(0.5) \ No newline at end of file +print("log test") \ No newline at end of file