129 lines
2.8 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
2022-01-01 02:26:39 +00:00
"gitlab.com/khanzf/fedilogue/shared"
)
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 commandmap shared.CommandMap
err = json.Unmarshal(body, &commandmap)
if err != nil {
logWarn("Unable to unmarshal here, exiting...")
return
}
go followInbox(commandmap.Endpoint)
2020-11-24 20:36:47 -05:00
fmt.Fprintf(w, "{}")
}
2020-11-24 20:36:47 -05:00
func startctl() {
http.HandleFunc("/follow", cmdFollow)
log.Print("Starting HTTP inbox on port 127.0.0.1:5555")
log.Fatal(http.ListenAndServe("127.0.0.1:5555", nil))
2020-11-24 20:36:47 -05:00
}
/*
func handleClient(commandClient net.Conn) {
2021-01-30 14:30:49 +00:00
defer commandClient.Close()
sizebyte := make([]byte, 4)
2022-01-01 02:26:39 +00:00
var commandmap shared.CommandMap
var responseback shared.ResponseBack
n, err := io.ReadFull(commandClient, sizebyte)
2020-11-13 20:13:13 -05:00
if err != nil {
logFatal.Fatal("Read error: ", err)
return
2020-11-13 20:13:13 -05:00
}
if n != 4 {
logFatal.Fatal("Did not read 4 bytes, failure.")
return
}
jsonsize := int(binary.LittleEndian.Uint32(sizebyte))
jsonbyte := make([]byte, jsonsize)
n, err = io.ReadFull(commandClient, jsonbyte)
2020-11-13 20:13:13 -05:00
if err != nil {
logFatal.Fatal("Unable to unmarshal")
2020-11-13 20:13:13 -05:00
}
if n != jsonsize {
logFatal.Fatal("Failed to read json size of ", n)
}
err = json.Unmarshal(jsonbyte, &commandmap)
if err != nil {
logFatal.Fatal("Unable to unmarshal")
}
switch commandmap.Type {
case "status":
responseback.Message = "Ok"
case "add":
2021-02-13 02:45:24 +00:00
logConn("Manually added instance: " + commandmap.Endpoint)
//ri_mutex.Lock()
_, exists := GetRunner(commandmap.Endpoint)
if exists == true {
2021-02-13 02:45:24 +00:00
logConn("Already exists: " + commandmap.Endpoint)
2020-11-18 19:38:08 -05:00
responseback.Message = "Exists: " + commandmap.Endpoint
} else {
responseback.Message = "Adding: " + commandmap.Endpoint
ri_mutex.Lock()
go StartInstance(commandmap.Endpoint)
ri_mutex.Unlock()
}
//ri_mutex.Unlock()
case "follow":
logInfo("Follow handling...")
responseback.Message = "Trying to follow " + commandmap.Endpoint
go followInbox(commandmap.Endpoint)
case "suspend":
logFatal.Fatal("Suspend")
case "resume":
logFatal.Fatal("Resume")
default:
logFatal.Fatal("Something else")
}
responseback.Type = "status"
responseback.RunningInstances = runninginstances
ri_mutex.Lock()
responsebytes, err := json.Marshal(responseback)
ri_mutex.Unlock()
if err != nil {
2021-02-11 21:05:01 +00:00
logFatal.Fatal(err)
}
n = len(responsebytes)
binary.LittleEndian.PutUint32(sizebyte, uint32(n))
2020-11-13 20:13:13 -05:00
_, err = commandClient.Write(sizebyte)
if err != nil {
logFatal.Fatal("Error on write:", err)
2020-11-13 20:13:13 -05:00
}
ri_mutex.Lock()
responsebyte, err := json.Marshal(responseback)
ri_mutex.Unlock()
if err != nil {
logFatal.Fatal("Error response back: ", err)
}
2020-11-13 20:13:13 -05:00
_, err = commandClient.Write(responsebyte)
if err != nil {
logFatal.Fatal("Error on write:", err)
2020-11-13 20:13:13 -05:00
}
}
*/