From 26f53cea7ea7cc219e41cd162979d6e0f35cc681 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Sat, 1 Nov 2014 14:36:57 +0100 Subject: [PATCH] Fmt --- Makefile | 2 +- ifconfig.go | 140 +++++++++++++++++++++++------------------------ ifconfig_test.go | 26 ++++----- 3 files changed, 84 insertions(+), 84 deletions(-) diff --git a/Makefile b/Makefile index 4b2cfe9..29cfb1e 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ clean: rm -f -- $(TARGET) fmt: - gofmt -tabs=false -tabwidth=4 -w=true *.go + gofmt -w=true *.go install: go build $(TARGET).go diff --git a/ifconfig.go b/ifconfig.go index 2f5e446..949d99f 100644 --- a/ifconfig.go +++ b/ifconfig.go @@ -1,97 +1,97 @@ package main import ( - "encoding/json" - "fmt" - "html/template" - "io" - "log" - "net" - "net/http" - "regexp" - "strings" + "encoding/json" + "fmt" + "html/template" + "io" + "log" + "net" + "net/http" + "regexp" + "strings" ) type Client struct { - IP net.IP - JSON string - Header http.Header + IP net.IP + JSON string + Header http.Header } func isCli(userAgent string) bool { - match, _ := regexp.MatchString("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$", - userAgent) - return match + match, _ := regexp.MatchString("^(?i)(curl|wget|fetch\\slibfetch)\\/.*$", + userAgent) + return match } func parseRealIP(req *http.Request) net.IP { - var host string - realIP := req.Header.Get("X-Real-IP") - if realIP != "" { - host = realIP - } else { - host, _, _ = net.SplitHostPort(req.RemoteAddr) - } - return net.ParseIP(host) + var host string + realIP := req.Header.Get("X-Real-IP") + if realIP != "" { + host = realIP + } else { + host, _, _ = net.SplitHostPort(req.RemoteAddr) + } + return net.ParseIP(host) } func pathToKey(path string) string { - re := regexp.MustCompile("^\\/|\\.json$") - return re.ReplaceAllLiteralString(strings.ToLower(path), "") + re := regexp.MustCompile("^\\/|\\.json$") + return re.ReplaceAllLiteralString(strings.ToLower(path), "") } func isJson(req *http.Request) bool { - return strings.HasSuffix(req.URL.Path, ".json") || - strings.Contains(req.Header.Get("Accept"), "application/json") + return strings.HasSuffix(req.URL.Path, ".json") || + strings.Contains(req.Header.Get("Accept"), "application/json") } func handler(w http.ResponseWriter, req *http.Request) { - if req.Method != "GET" { - http.Error(w, "Invalid request method", 405) - return - } + if req.Method != "GET" { + http.Error(w, "Invalid request method", 405) + return + } - ip := parseRealIP(req) - header := pathToKey(req.URL.Path) + ip := parseRealIP(req) + header := pathToKey(req.URL.Path) - if isJson(req) { - if header == "all" { - b, _ := json.MarshalIndent(req.Header, "", " ") - io.WriteString(w, fmt.Sprintf("%s\n", b)) - } else { - m := map[string]string{ - header: req.Header.Get(header), - } - b, _ := json.MarshalIndent(m, "", " ") - io.WriteString(w, fmt.Sprintf("%s\n", b)) - } - } else if isCli(req.UserAgent()) { - if header == "" || header == "ip" { - io.WriteString(w, fmt.Sprintf("%s\n", ip)) - } else { - value := req.Header.Get(header) - io.WriteString(w, fmt.Sprintf("%s\n", value)) - } - } else { - funcMap := template.FuncMap{ - "ToLower": strings.ToLower, - } - t, _ := template. - New("index.html"). - Funcs(funcMap). - ParseFiles("index.html") - b, _ := json.MarshalIndent(req.Header, "", " ") - client := &Client{IP: ip, JSON: string(b), Header: req.Header} - t.Execute(w, client) - } + if isJson(req) { + if header == "all" { + b, _ := json.MarshalIndent(req.Header, "", " ") + io.WriteString(w, fmt.Sprintf("%s\n", b)) + } else { + m := map[string]string{ + header: req.Header.Get(header), + } + b, _ := json.MarshalIndent(m, "", " ") + io.WriteString(w, fmt.Sprintf("%s\n", b)) + } + } else if isCli(req.UserAgent()) { + if header == "" || header == "ip" { + io.WriteString(w, fmt.Sprintf("%s\n", ip)) + } else { + value := req.Header.Get(header) + io.WriteString(w, fmt.Sprintf("%s\n", value)) + } + } else { + funcMap := template.FuncMap{ + "ToLower": strings.ToLower, + } + t, _ := template. + New("index.html"). + Funcs(funcMap). + ParseFiles("index.html") + b, _ := json.MarshalIndent(req.Header, "", " ") + client := &Client{IP: ip, JSON: string(b), Header: req.Header} + t.Execute(w, client) + } } func main() { - http.Handle("/assets/", http.StripPrefix("/assets/", - http.FileServer(http.Dir("assets/")))) - http.HandleFunc("/", handler) - err := http.ListenAndServe(":8080", nil) - if err != nil { - log.Fatal("ListenAndServe: ", err) - } + http.Handle("/assets/", http.StripPrefix("/assets/", + http.FileServer(http.Dir("assets/")))) + http.HandleFunc("/", handler) + err := http.ListenAndServe(":8080", nil) + if err != nil { + log.Fatal("ListenAndServe: ", err) + } } diff --git a/ifconfig_test.go b/ifconfig_test.go index d0ec1c1..2423fd5 100644 --- a/ifconfig_test.go +++ b/ifconfig_test.go @@ -3,19 +3,19 @@ package main import "testing" func TestIsCLi(t *testing.T) { - userAgents := []string{"curl/7.26.0", "Wget/1.13.4 (linux-gnu)", - "fetch libfetch/2.0"} + userAgents := []string{"curl/7.26.0", "Wget/1.13.4 (linux-gnu)", + "fetch libfetch/2.0"} - for _, userAgent := range userAgents { - if !isCli(userAgent) { - t.Errorf("Expected true for %s", userAgent) - } - } + for _, userAgent := range userAgents { + if !isCli(userAgent) { + t.Errorf("Expected true for %s", userAgent) + } + } - browserUserAgent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " + - "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " + - "Safari/537.36" - if isCli(browserUserAgent) { - t.Errorf("Expected false for %s", browserUserAgent) - } + browserUserAgent := "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " + + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " + + "Safari/537.36" + if isCli(browserUserAgent) { + t.Errorf("Expected false for %s", browserUserAgent) + } }