commit c422047cf93be8c798d414bb45e7b612b4abecee Author: emile Date: Tue Sep 22 18:40:30 2015 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d3a357e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/whoamI diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b23c0a0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +# Create a minimal container to run a Golang static binary +FROM scratch +COPY whoamI / +ENTRYPOINT ["/whoamI"] +EXPOSE 80 diff --git a/README.md b/README.md new file mode 100644 index 0000000..39a38da --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# whoamI + +Tiny Go webserver that prints os information and HTTP request to output + +```sh +$ docker run -d -P --name iamfoo emilevauge/whoami +$ docker inspect --format '{{ .NetworkSettings.Ports }}' iamfoo +map[80/tcp:[{0.0.0.0 32769}]] +$ curl "http://0.0.0.0:32769" +Hostname : 6e0030e67d6a +IP : 127.0.0.1 +IP : ::1 +IP : 172.17.0.27 +IP : fe80::42:acff:fe11:1b +GET / HTTP/1.1 +Host: 0.0.0.0:32769 +User-Agent: curl/7.35.0 +Accept: */* +``` diff --git a/app.go b/app.go new file mode 100644 index 0000000..bc837b2 --- /dev/null +++ b/app.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "log" + "net/http" + "github.com/gorilla/mux" + "os" + "net" +) + +func main() { + + r := mux.NewRouter() + r.HandleFunc("/", whoamI) + http.Handle("/", r) + fmt.Println("Starting up on 80") + log.Fatal(http.ListenAndServe(":80", nil)) +} + +func whoamI(w http.ResponseWriter, req *http.Request) { + hostname, _ := os.Hostname() + fmt.Fprintln(w, "Hostname : ", hostname) + ifaces, _ := net.Interfaces() + // handle err + for _, i := range ifaces { + addrs, _ := i.Addrs() + // handle err + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + fmt.Fprintln(w, "IP : ", ip) + } + } + req.Write(w) +} diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..9c6599a --- /dev/null +++ b/build.sh @@ -0,0 +1,3 @@ +#!/bin/sh +CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o whoamI +docker build -t emilevauge/whoami .