api/handlers/rest_ApiTerminal_realtime.go
// File generated by Gopher Sauce
// DO NOT EDIT!!
package handlers
import (
"fmt"
"log"
"net/http"
"os"
"runtime"
"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"
isLinux := strings.Contains(runtime.GOOS, "linux")
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 if isLinux {
cm1 = "bash"
ctx = exec.InteractiveExec(cm1)
} 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 if isLinux {
ctx = exec.InteractiveExec(cm1)
} 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)
msg = strings.ReplaceAll(msg, "/", "\\")
}
ctx.Send(msg)
}
}
callmet = true
return
}