Initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/whoamI
|
5
Dockerfile
Normal file
5
Dockerfile
Normal 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
19
README.md
Normal 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
41
app.go
Normal 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)
|
||||||
|
}
|
Reference in New Issue
Block a user