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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"html"
|
"html"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"0cd.xyz-go/logger"
|
||||||
"github.com/fatih/structs"
|
"github.com/fatih/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPError struct {
|
|
||||||
Status string
|
|
||||||
StatusCode int
|
|
||||||
}
|
|
||||||
|
|
||||||
func url(path string) string {
|
func url(path string) string {
|
||||||
return string("https://forum.0cd.xyz/" + path)
|
return string("https://forum.0cd.xyz/" + path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request sends GET to 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)
|
req, err := http.NewRequest("GET", url(path), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error reading request. ", err)
|
logger.ErrorLog("Error reading request. ", err)
|
||||||
return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
|
return nil, &logger.HTTPError{Status: "500 Internal Server Error", StatusCode: http.StatusInternalServerError}
|
||||||
}
|
}
|
||||||
|
|
||||||
head, er := Header()
|
head, er := Header()
|
||||||
@ -41,21 +35,21 @@ func Request(path string) ([]byte, *HTTPError) {
|
|||||||
client := &http.Client{Timeout: time.Second * 10}
|
client := &http.Client{Timeout: time.Second * 10}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error reading request. ", err)
|
logger.ErrorLog("Error reading request. ", err)
|
||||||
return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
|
return nil, &logger.HTTPError{Status: "500 Internal Server Error", StatusCode: http.StatusInternalServerError}
|
||||||
}
|
}
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error reading request. ", err)
|
logger.ErrorLog("Error reading request. ", err)
|
||||||
return nil, &HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
|
return nil, &logger.HTTPError{Status: "500 Internal Server Error", StatusCode: http.StatusInternalServerError}
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode == 200 {
|
if resp.StatusCode == 200 {
|
||||||
return body, nil
|
return body, nil
|
||||||
}
|
}
|
||||||
fmt.Printf("Request Error: GET %d %s\n", resp.StatusCode, html.EscapeString(url(path)))
|
logger.GetLog("GET %d %s\n", resp.StatusCode, html.EscapeString(url(path)))
|
||||||
return nil, &HTTPError{Status: resp.Status, StatusCode: resp.StatusCode}
|
return nil, &logger.HTTPError{Status: resp.Status, StatusCode: resp.StatusCode}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Category returns category json data
|
// Category returns category json data
|
||||||
@ -114,7 +108,7 @@ func Tag() map[string]interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetTopics gets topic list from tag
|
// 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)
|
resp, err := Request("tags/" + path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
14
data.go
14
data.go
@ -11,6 +11,20 @@ type Headers struct {
|
|||||||
} `json:"headers"`
|
} `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
|
// Contacts list
|
||||||
type Contacts struct {
|
type Contacts struct {
|
||||||
Contacts []struct {
|
Contacts []struct {
|
||||||
|
32
file.go
32
file.go
@ -6,21 +6,40 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"0cd.xyz-go/logger"
|
||||||
"github.com/fatih/structs"
|
"github.com/fatih/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// File reads request json file
|
// File reads request json file
|
||||||
func File(path string) ([]byte, *HTTPError) {
|
func File(path string) ([]byte, *logger.HTTPError) {
|
||||||
jsonFile, err := os.Open(path)
|
jsonFile, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error reading request. ", err)
|
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()
|
defer jsonFile.Close()
|
||||||
byteValue, _ := ioutil.ReadAll(jsonFile)
|
byteValue, _ := ioutil.ReadAll(jsonFile)
|
||||||
return byteValue, nil
|
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{} {
|
func Contact() map[string]interface{} {
|
||||||
var contact Contacts
|
var contact Contacts
|
||||||
resp, err := File("./assets/json/contacts.json")
|
resp, err := File("./assets/json/contacts.json")
|
||||||
@ -32,12 +51,3 @@ func Contact() map[string]interface{} {
|
|||||||
m := structs.Map(contact)
|
m := structs.Map(contact)
|
||||||
return m
|
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