From fabbf68ced20d59aaeccce4c047ba725ef3492e7 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Sun, 25 Nov 2012 22:41:27 +0100 Subject: [PATCH] Add JSON output --- ifconfig.go | 18 ++++++++++++++++-- index.html | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ifconfig.go b/ifconfig.go index 6aa7eec..2f5e446 100644 --- a/ifconfig.go +++ b/ifconfig.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "html/template" "io" @@ -13,6 +14,7 @@ import ( type Client struct { IP net.IP + JSON string Header http.Header } @@ -52,7 +54,18 @@ func handler(w http.ResponseWriter, req *http.Request) { ip := parseRealIP(req) header := pathToKey(req.URL.Path) - if isCli(req.UserAgent()) { + 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 { @@ -67,7 +80,8 @@ func handler(w http.ResponseWriter, req *http.Request) { New("index.html"). Funcs(funcMap). ParseFiles("index.html") - client := &Client{IP: ip, Header: req.Header} + b, _ := json.MarshalIndent(req.Header, "", " ") + client := &Client{IP: ip, JSON: string(b), Header: req.Header} t.Execute(w, client) } } diff --git a/index.html b/index.html index 418d412..b89f5cd 100644 --- a/index.html +++ b/index.html @@ -58,6 +58,8 @@ {{end}} {{end}} + curl ifconfig/all.json +
{{ .JSON }}