From 1313fa6949293a82234ea972f47c674c34a46e52 Mon Sep 17 00:00:00 2001 From: michaellindman Date: Wed, 26 Jun 2019 02:56:55 +0100 Subject: [PATCH] refactoring code/error handling --- api.go | 28 +++++++++++----------------- data.go | 14 ++++++++++++++ file.go | 32 +++++++++++++++++++++----------- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/api.go b/api.go index 33e4198..7755866 100755 --- a/api.go +++ b/api.go @@ -2,32 +2,26 @@ package request import ( "encoding/json" - "fmt" "html" "io/ioutil" - "log" "net/http" "strings" "time" + "0cd.xyz-go/logger" "github.com/fatih/structs" ) -type HTTPError struct { - Status string - StatusCode int -} - func url(path string) string { return string("https://forum.0cd.xyz/" + path) } // Request sends GET to path -func Request(path string) ([]byte, *HTTPError) { +func Request(path string) ([]byte, *logger.HTTPError) { req, err := http.NewRequest("GET", url(path), nil) if err != nil { - log.Println("Error reading request. ", err) - return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500} + logger.ErrorLog("Error reading request. ", err) + return nil, &logger.HTTPError{Status: "500 Internal Server Error", StatusCode: http.StatusInternalServerError} } head, er := Header() @@ -41,21 +35,21 @@ func Request(path string) ([]byte, *HTTPError) { client := &http.Client{Timeout: time.Second * 10} resp, err := client.Do(req) if err != nil { - log.Println("Error reading request. ", err) - return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500} + logger.ErrorLog("Error reading request. ", err) + return nil, &logger.HTTPError{Status: "500 Internal Server Error", StatusCode: http.StatusInternalServerError} } body, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Println("Error reading request. ", err) - return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500} + logger.ErrorLog("Error reading request. ", err) + return nil, &logger.HTTPError{Status: "500 Internal Server Error", StatusCode: http.StatusInternalServerError} } defer resp.Body.Close() if resp.StatusCode == 200 { return body, nil } - fmt.Printf("Request Error: GET %d %s\n", resp.StatusCode, html.EscapeString(url(path))) - return nil, &HTTPError{Status: resp.Status, StatusCode: resp.StatusCode} + logger.GetLog("GET %d %s\n", resp.StatusCode, html.EscapeString(url(path))) + return nil, &logger.HTTPError{Status: resp.Status, StatusCode: resp.StatusCode} } // Category returns category json data @@ -114,7 +108,7 @@ func Tag() map[string]interface{} { } // GetTopics gets topic list from tag -func GetTopics(path string) (topics TagTopics, err *HTTPError) { +func GetTopics(path string) (topics TagTopics, err *logger.HTTPError) { resp, err := Request("tags/" + path) if err != nil { return diff --git a/data.go b/data.go index 15e8761..3be2f33 100644 --- a/data.go +++ b/data.go @@ -11,6 +11,20 @@ type Headers struct { } `json:"headers"` } +type Options struct { + Title string `json:"title"` + Description string `json:"description"` + Site string `json:"site"` + Options struct { + Address string `json:"address"` + Port int `json:"port"` + Ace struct { + DynamicReload bool `json:"DynamicReload"` + BaseDir string `json:"BaseDir"` + } `json:"ace"` + } `json:"options"` +} + // Contacts list type Contacts struct { Contacts []struct { diff --git a/file.go b/file.go index 64576e8..52625ac 100644 --- a/file.go +++ b/file.go @@ -6,21 +6,40 @@ import ( "log" "os" + "0cd.xyz-go/logger" "github.com/fatih/structs" ) // File reads request json file -func File(path string) ([]byte, *HTTPError) { +func File(path string) ([]byte, *logger.HTTPError) { jsonFile, err := os.Open(path) if err != nil { log.Println("Error reading request. ", err) - return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500} + return nil, &logger.HTTPError{Status: "500 Internal Server Error", StatusCode: 500} } defer jsonFile.Close() byteValue, _ := ioutil.ReadAll(jsonFile) return byteValue, nil } +func Header() (headers *Headers, err *logger.HTTPError) { + resp, err := File("./assets/json/headers.json") + if err != nil { + return + } + json.Unmarshal(resp, &headers) + return +} + +func Option() (options *Options) { + resp, err := File("./assets/json/options.json") + if err != nil { + return + } + json.Unmarshal(resp, &options) + return +} + func Contact() map[string]interface{} { var contact Contacts resp, err := File("./assets/json/contacts.json") @@ -32,12 +51,3 @@ func Contact() map[string]interface{} { m := structs.Map(contact) return m } - -func Header() (headers *Headers, err *HTTPError) { - resp, err := File("./assets/json/headers.json") - if err != nil { - return - } - json.Unmarshal(resp, &headers) - return -}