working on websockets and process spawning
This commit is contained in:
parent
993d38f304
commit
447bc4650c
3 changed files with 66 additions and 0 deletions
2
go.mod
2
go.mod
|
|
@ -7,3 +7,5 @@ require (
|
||||||
github.com/umpc/go-sortedmap v0.0.0-20180422175548-64ab94c482f4
|
github.com/umpc/go-sortedmap v0.0.0-20180422175548-64ab94c482f4
|
||||||
golang.org/x/crypto v0.29.0
|
golang.org/x/crypto v0.29.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -1,5 +1,7 @@
|
||||||
github.com/elliotchance/orderedmap/v2 v2.4.0 h1:6tUmMwD9F998FNpwFxA5E6NQvSpk2PVw7RKsVq3+2Cw=
|
github.com/elliotchance/orderedmap/v2 v2.4.0 h1:6tUmMwD9F998FNpwFxA5E6NQvSpk2PVw7RKsVq3+2Cw=
|
||||||
github.com/elliotchance/orderedmap/v2 v2.4.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
|
github.com/elliotchance/orderedmap/v2 v2.4.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
|
||||||
|
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||||
|
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
|
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
|
||||||
|
|
|
||||||
62
main.go
62
main.go
|
|
@ -4,14 +4,17 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
"wargh/db"
|
"wargh/db"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -41,6 +44,20 @@ var ERROR_TEXT = []string{
|
||||||
"Unathorized",
|
"Unathorized",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var wsUpgrader = websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
}
|
||||||
|
|
||||||
|
type Job struct {
|
||||||
|
Command *exec.Cmd
|
||||||
|
StdoutPipe io.ReadCloser
|
||||||
|
StderrPipe io.ReadCloser
|
||||||
|
Socket []*websocket.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
var jobs map[string]Job = make(map[string]Job)
|
||||||
|
|
||||||
func errorHandler(w http.ResponseWriter, r *http.Request) {
|
func errorHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
errorParam := r.URL.Query().Get("error")
|
errorParam := r.URL.Query().Get("error")
|
||||||
var errorCode int
|
var errorCode int
|
||||||
|
|
@ -226,6 +243,50 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createJob() (string, error) {
|
||||||
|
cmd := exec.Command("/usr/bin/sh", "-c", "while true; do echo $(date) \"test\"")
|
||||||
|
|
||||||
|
stdout, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
stderr, err := cmd.StderrPipe()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = cmd.Start()
|
||||||
|
|
||||||
|
jobId := randString(64)
|
||||||
|
jobs[jobId] = Job{
|
||||||
|
Command: cmd,
|
||||||
|
StdoutPipe: stdout,
|
||||||
|
StderrPipe: stderr,
|
||||||
|
Socket: make([]*websocket.Conn, 0),
|
||||||
|
}
|
||||||
|
return jobId, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !checkSession(w, r) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// get job id and attach
|
||||||
|
|
||||||
|
ws, err := wsUpgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err)
|
||||||
|
|
||||||
|
redirectError(w, r, ERROR_UNKNOWN)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.WriteMessage(websocket.TextMessage, ([]byte)("test"))
|
||||||
|
ws.Close()
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
db.Init(&db.DBConfig{
|
db.Init(&db.DBConfig{
|
||||||
DBPath: "wargh.db",
|
DBPath: "wargh.db",
|
||||||
|
|
@ -252,6 +313,7 @@ func main() {
|
||||||
http.HandleFunc("/", indexHandler)
|
http.HandleFunc("/", indexHandler)
|
||||||
http.HandleFunc("/error", errorHandler)
|
http.HandleFunc("/error", errorHandler)
|
||||||
http.HandleFunc("/login", loginHandler)
|
http.HandleFunc("/login", loginHandler)
|
||||||
|
http.HandleFunc("/ws", wsHandler)
|
||||||
|
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue