From 82d029b357e806749b489be808950a39ad919dcc Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Mon, 24 Sep 2018 20:00:18 +0000 Subject: [PATCH] Generate proper JSON --- src/handlers.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ src/static.go | 58 --------------------------------------- 2 files changed, 73 insertions(+), 58 deletions(-) diff --git a/src/handlers.go b/src/handlers.go index 3b0ec91..8eec570 100644 --- a/src/handlers.go +++ b/src/handlers.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "encoding/json" "fmt" "io" "log" @@ -11,6 +12,78 @@ import ( "strings" ) +type JSend struct { + Status string `json:"status"` + Data JSendData `json:"data"` +} +type JSendData struct { + Short string `json:"short"` + Description string `json:"description"` +} + +// ShowJSend renders a JSend response to w +func ShowJSend(w http.ResponseWriter, status Status, short string, description string) { + + resp := JSend{ + Status: "success", + Data: JSendData{ + Short: short, + Description: description, + }, + } + switch status { + case Success: + resp.Status = "success" + case Fail: + resp.Status = "fail" + default: + resp.Status = "error" + } + + respBytes, err := json.Marshal(resp) + if (err != nil) { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) // RFC2616 makes it pretty clear that 4xx codes are for the user-agent + w.Write(respBytes) +} + +// ShowHtml delevers an HTML response to w +func ShowHtml(w http.ResponseWriter, status Status, title string, body string) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusOK) + + statusStr := "" + switch status { + case Success: + statusStr = "Success" + case Fail: + statusStr = "Fail" + default: + statusStr = "Error" + } + + fmt.Fprintf(w, "") + fmt.Fprintf(w, "") + fmt.Fprintf(w, "%s", title) + fmt.Fprintf(w, "") + fmt.Fprintf(w, "") + fmt.Fprintf(w, "") + fmt.Fprintf(w, "") + fmt.Fprintf(w, "

%s

", statusStr, title) + fmt.Fprintf(w, "
%s
", body) + fmt.Fprintf(w, "") + fmt.Fprintf(w, "") +} + func respond(w http.ResponseWriter, req *http.Request, status Status, short string, format string, a ...interface{}) { long := fmt.Sprintf(format, a...) // This is a kludge. Do proper parsing when this causes problems. diff --git a/src/static.go b/src/static.go index 1630c50..41a60bc 100644 --- a/src/static.go +++ b/src/static.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "fmt" "net/http" "os" @@ -17,63 +16,6 @@ const ( Error ) -// ShowJSend renders a JSend response to w -func ShowJSend(w http.ResponseWriter, status Status, short string, description string) { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) // RFC2616 makes it pretty clear that 4xx codes are for the user-agent - - statusStr := "" - switch status { - case Success: - statusStr = "success" - case Fail: - statusStr = "fail" - default: - statusStr = "error" - } - - jshort, _ := json.Marshal(short) - jdesc, _ := json.Marshal(description) - fmt.Fprintf( - w, - `{"status":"%s","data":{"short":%s,"description":%s}}"`, - statusStr, jshort, jdesc, - ) -} - -// ShowHtml delevers an HTML response to w -func ShowHtml(w http.ResponseWriter, status Status, title string, body string) { - w.Header().Set("Content-Type", "text/html; charset=utf-8") - w.WriteHeader(http.StatusOK) - - statusStr := "" - switch status { - case Success: - statusStr = "Success" - case Fail: - statusStr = "Fail" - default: - statusStr = "Error" - } - - fmt.Fprintf(w, "") - fmt.Fprintf(w, "") - fmt.Fprintf(w, "%s", title) - fmt.Fprintf(w, "") - fmt.Fprintf(w, "") - fmt.Fprintf(w, "") - fmt.Fprintf(w, "") - fmt.Fprintf(w, "

%s

", statusStr, title) - fmt.Fprintf(w, "
%s
", body) - fmt.Fprintf(w, "") - fmt.Fprintf(w, "") -} - // staticStylesheet serves up a basic stylesheet. // This is designed to be usable on small touchscreens (like mobile phones) func staticStylesheet(w http.ResponseWriter) {