Skip to content

Instantly share code, notes, and snippets.

@danielvaughan
Created April 18, 2017 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielvaughan/51e2a1a13b0b944e33feae94a5b1808d to your computer and use it in GitHub Desktop.
Save danielvaughan/51e2a1a13b0b944e33feae94a5b1808d to your computer and use it in GitHub Desktop.
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