refactoring code/error handling
This commit is contained in:
parent
64c9ea0158
commit
1313fa6949
28
api.go
28
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
|
||||
|
14
data.go
14
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 {
|
||||
|
32
file.go
32
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user