Created
April 18, 2017 19:48
-
-
Save danielvaughan/51e2a1a13b0b944e33feae94a5b1808d to your computer and use it in GitHub Desktop.
Our go message forwarder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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