72 lines
1.4 KiB
Go
Raw Normal View History

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)
2020-11-24 20:36:47 -05:00
if err != nil {
fmt.Println(err)
return
2020-11-24 20:36:47 -05:00
}
defer r.Body.Close()
2020-11-24 20:36:47 -05:00
var followtarget interface{}
err = json.Unmarshal(body, &followtarget)
if err != nil {
fmt.Println(err)
return
}
2020-11-24 20:36:47 -05:00
myMap := followtarget.(map[string]interface{})
followInbox(myMap["follow"].(string))
2020-11-24 20:36:47 -05:00
fmt.Fprintf(w, "{}")
2020-11-24 20:36:47 -05:00
}
func cmdAdd(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
2020-11-13 20:13:13 -05:00
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))
}