Farhan Khan 2d7c09a905 Relay option added
Restructured ctl to http protocol
Minor modifications throughout
2022-01-13 17:09:12 -05:00

72 lines
1.4 KiB
Go

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func cmdFollow(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println(err)
return
}
defer r.Body.Close()
var followtarget interface{}
err = json.Unmarshal(body, &followtarget)
if err != nil {
fmt.Println(err)
return
}
myMap := followtarget.(map[string]interface{})
followInbox(myMap["follow"].(string))
fmt.Fprintf(w, "{}")
}
func cmdAdd(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println(err)
return
}
defer r.Body.Close()
var addtarget interface{}
err = json.Unmarshal(body, &addtarget)
if err != nil {
fmt.Println(err)
return
}
myMap := addtarget.(map[string]interface{})
go StartInstance(myMap["host"].(string))
fmt.Fprintf(w, "{}")
}
func cmdStatus(w http.ResponseWriter, r *http.Request) {
ri_mutex.Lock()
bytes, err := json.Marshal(runninginstances)
if err != nil {
fmt.Println("Error happened", err)
}
ri_mutex.Unlock()
fmt.Fprintf(w, (string(bytes)))
}
func startctl() {
ctlweb := http.NewServeMux()
ctlweb.HandleFunc("/follow", cmdFollow)
ctlweb.HandleFunc("/status", cmdStatus)
ctlweb.HandleFunc("/add", cmdAdd)
log.Print("Starting HTTP inbox on port 127.0.0.1:5555")
log.Fatal(http.ListenAndServe("127.0.0.1:5555", ctlweb))
}