Initial commit

This commit is contained in:
emile
2015-09-22 18:40:30 +02:00
commit c422047cf9
5 changed files with 69 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/whoamI

5
Dockerfile Normal file
View File

@ -0,0 +1,5 @@
# Create a minimal container to run a Golang static binary
FROM scratch
COPY whoamI /
ENTRYPOINT ["/whoamI"]
EXPOSE 80

19
README.md Normal file
View File

@ -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: */*
```

41
app.go Normal file
View File

@ -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)
}

3
build.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
CGO_ENABLED=0 go build -a --installsuffix cgo --ldflags="-s" -o whoamI
docker build -t emilevauge/whoami .