-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
rest_ApiTerminal_realtime.go
executable file
·95 lines (73 loc) · 1.83 KB
/
rest_ApiTerminal_realtime.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
// File generated by Gopher Sauce
// DO NOT EDIT!!
package handlers
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/adlane/exec"
"github.com/gorilla/sessions"
"github.com/thestrukture/IDE/api/globals"
methods "github.com/thestrukture/IDE/api/methods"
)
//
func ApiTerminal_realtime(w http.ResponseWriter, r *http.Request, session *sessions.Session) (response string, callmet bool) {
c, err := globals.Upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
cm1, cm2 := "bash", "-i"
if globals.Windows {
cm1 = "start"
cm2 = "cmd"
}
var ctx exec.ProcessContext
if globals.Windows {
tc := strings.Replace("set PATH=%PATH%;"+os.ExpandEnv("$GOPATH")+"\\bin", "/", "\\", -1)
ctx = exec.InteractiveExec("cmd", "/k", tc)
c.WriteMessage(1, []byte("HELLO globals.Windows user."))
} else {
ctx = exec.InteractiveExec(cm1, cm2)
}
reader := methods.Reader{Conn: c}
go ctx.Receive(&reader, 5*time.Hour)
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
if ctx != nil {
ctx.Cancel()
ctx.Stop()
}
break
}
if len(message) != 0 {
msg := string(message)
if msg == "killnow\n" {
fmt.Println("Restarting")
ctx.Cancel()
ctx.Stop()
if globals.Windows {
tc := strings.Replace("set PATH=%PATH%;"+os.ExpandEnv("$GOPATH")+"\\bin", "/", "\\", -1)
ctx = exec.InteractiveExec("cmd", "/k", tc)
c.WriteMessage(1, []byte("HELLO globals.Windows user."))
} else {
ctx = exec.InteractiveExec(cm1, cm2)
}
reader = methods.Reader{Conn: c}
go ctx.Receive(&reader, 5*time.Hour)
continue
} else if strings.Contains(msg, "$GOPATH") && globals.Windows {
msg = strings.ReplaceAll(msg, "$GOPATH", globals.Dfd)
}
ctx.Send(msg)
}
}
callmet = true
return
}