forked from jwilder/whoami
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from olljanat/more-env-info
Merged with more details logic from https://github.com/Nilsty/whoami
- Loading branch information
Showing
6 changed files
with
126 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"runtime" | ||
"time" | ||
|
||
"gopkg.in/sirupsen/logrus.v1" | ||
"gopkg.in/spf13/viper.v1" | ||
) | ||
|
||
type DataResponse struct { | ||
Hostname string `json:"hostname,omitempty"` | ||
Platform string `json:"platform,omitempty"` | ||
IP []string `json:"ip,omitempty"` | ||
Headers http.Header `json:"header,omitempty"` | ||
Environment []string `json:"env,omitempty"` | ||
} | ||
|
||
var port string | ||
|
||
func init() { | ||
flag.StringVar(&port, "port", "8080", "give me a port number") | ||
|
||
lvl, err := logrus.ParseLevel(viper.GetString("loglevel")) | ||
if err != nil { | ||
lvl = logrus.WarnLevel | ||
} | ||
logrus.SetLevel(lvl) | ||
} | ||
|
||
func main() { | ||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8080" | ||
flag.Parse() | ||
|
||
http.HandleFunc("/", index) | ||
http.HandleFunc("/api", api) | ||
|
||
log.Println("Starting up on port " + port) | ||
if err := http.ListenAndServe(":"+port, nil); err != nil { | ||
http.ListenAndServe(":"+port, nil) | ||
} | ||
} | ||
|
||
func index(w http.ResponseWriter, req *http.Request) { | ||
u, _ := url.Parse(req.URL.String()) | ||
queryParams := u.Query() | ||
|
||
fmt.Fprintf(os.Stdout, "Listening on :%s\n", port) | ||
wait := queryParams.Get("wait") | ||
if len(wait) > 0 { | ||
duration, err := time.ParseDuration(wait) | ||
if err == nil { | ||
time.Sleep(duration) | ||
} | ||
} | ||
|
||
data := fetchData(req) | ||
fmt.Fprintf(os.Stdout, "I'm %s\n", data.Hostname) | ||
fmt.Fprintf(w, "I'm %s running on %s\n\n", data.Hostname, data.Platform) | ||
|
||
for _, ip := range data.IP { | ||
fmt.Fprintln(w, "IP:", ip) | ||
} | ||
|
||
for _, env := range data.Environment { | ||
fmt.Fprintln(w, "ENV:", env) | ||
} | ||
req.Write(w) | ||
} | ||
|
||
func api(w http.ResponseWriter, req *http.Request) { | ||
data := fetchData(req) | ||
json.NewEncoder(w).Encode(data) | ||
} | ||
|
||
func fetchData(req *http.Request) DataResponse { | ||
hostname, _ := os.Hostname() | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(os.Stdout, "I'm %s\n", hostname) | ||
fmt.Fprintf(w, "I'm %s running on %s/%s\n", hostname, runtime.GOOS, runtime.GOARCH) | ||
}) | ||
data := DataResponse{ | ||
hostname, | ||
fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), | ||
[]string{}, | ||
req.Header, | ||
os.Environ(), | ||
} | ||
|
||
ifaces, _ := net.Interfaces() | ||
for _, i := range ifaces { | ||
addrs, _ := i.Addrs() | ||
for _, addr := range addrs { | ||
var ip net.IP | ||
switch v := addr.(type) { | ||
case *net.IPNet: | ||
ip = v.IP | ||
case *net.IPAddr: | ||
ip = v.IP | ||
} | ||
data.IP = append(data.IP, ip.String()) | ||
} | ||
} | ||
|
||
log.Fatal(http.ListenAndServe(":"+port, nil)) | ||
return data | ||
} |
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