mirror of https://github.com/curusarn/resh
parent
f1cb7bfc75
commit
d77b99ba42
@ -0,0 +1,37 @@ |
||||
package common |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/json" |
||||
"net/http" |
||||
"log" |
||||
) |
||||
|
||||
type Record struct { |
||||
CmdLine string `json: cmdLine` |
||||
Pwd string `json: pwd` |
||||
GitWorkTree string `json: gitWorkTree` |
||||
Shell string `json: shell` |
||||
ExitCode int `json: exitCode` |
||||
Logs []string `json: logs` |
||||
} |
||||
|
||||
func (r Record) Send() { |
||||
recJson, err := json.Marshal(r) |
||||
if err != nil { |
||||
log.Fatal("1", err) |
||||
} |
||||
|
||||
req, err := http.NewRequest("POST", "http://localhost:8888", |
||||
bytes.NewBuffer(recJson)) |
||||
if err != nil { |
||||
log.Fatal("2", err) |
||||
} |
||||
req.Header.Set("Content-Type", "application/json") |
||||
|
||||
client := &http.Client{} |
||||
_, err = client.Do(req) |
||||
if err != nil { |
||||
log.Fatal("3", err) |
||||
} |
||||
} |
||||
@ -0,0 +1,41 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
"log" |
||||
"io/ioutil" |
||||
"net/http" |
||||
common "github.com/curusarn/resh/common" |
||||
) |
||||
|
||||
func main() { |
||||
server() |
||||
} |
||||
|
||||
func recordHandler(w http.ResponseWriter, r *http.Request) { |
||||
record := common.Record{} |
||||
|
||||
jsn, err := ioutil.ReadAll(r.Body) |
||||
if err != nil { |
||||
log.Println("Error reading the body", err) |
||||
return |
||||
} |
||||
|
||||
err = json.Unmarshal(jsn, &record) |
||||
if err != nil { |
||||
log.Println("Decoding error: ", err) |
||||
return |
||||
} |
||||
|
||||
log.Printf("Received: %v\n", record) |
||||
// fmt.Println("cmd:", r.CmdLine)
|
||||
// fmt.Println("pwd:", r.Pwd)
|
||||
// fmt.Println("git:", r.GitWorkTree)
|
||||
// fmt.Println("exit_code:", r.ExitCode)
|
||||
w.Write([]byte("OK\n")) |
||||
} |
||||
|
||||
func server() { |
||||
http.HandleFunc("/", recordHandler) |
||||
http.ListenAndServe(":8888", nil) |
||||
} |
||||
Loading…
Reference in new issue