Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Our go message forwarder
package main
import (
"net/http"
"fmt"
"strings"
"time"
"io"
)
func handler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
params := strings.SplitN(r.Form.Get("a"), ",", 2)
client := http.Client{Timeout:time.Second}
if params[0]!="" {
forwardIp := params[0]
var message = ""
if len(params) > 1 {
message = params[1]
}
resp, err := client.Get("http://192.168.21." + forwardIp + ":9001/?a=" + message)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
io.Copy(w, resp.Body)
resp.Body.Close()
reader := strings.NewReader(",fish")
reader.WriteTo(w)
fmt.Printf("Forwarded message %s to: %s\n", message, forwardIp)
} else {
fmt.Println("Ended")
fmt.Fprint(w, "end")
}
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":9001", nil);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment