Add proof of concept WebApp with basic functionality

TODO:
 • Add arguments to app for port and address
 • Add x-forwarded-for header support for use behind proxies
 • Be able to give response in text, json or html
master
Buddy Sandidge 8 years ago
parent d199e866c8
commit c3a54a1b85

1
.gitignore vendored

@ -0,0 +1 @@
what-is-my-ip

@ -0,0 +1,31 @@
package main
import (
"fmt"
"io"
"log"
"net"
"net/http"
)
func main() {
http.HandleFunc("/", getIP)
fmt.Println("listening on port 3000")
http.ListenAndServe(":3000", nil)
}
func getIP(w http.ResponseWriter, req *http.Request) {
ip, _, err := net.SplitHostPort(req.RemoteAddr)
if err != nil {
handleError(w, err, "Could not get IP address from request")
return
}
io.WriteString(w, ip+"\n")
log.Printf("Give response IP %s\n", ip)
}
func handleError(resp http.ResponseWriter, err error, message string) {
resp.WriteHeader(http.StatusBadRequest)
io.WriteString(resp, message)
log.Printf("Error handling request: %s (%s)", message, err)
}
Loading…
Cancel
Save