request/file.go

47 lines
969 B
Go
Raw Normal View History

package request
import (
"encoding/json"
"io/ioutil"
"log"
"os"
2019-06-26 01:56:55 +00:00
"0cd.xyz-go/logger"
"github.com/fatih/structs"
)
// File reads request json file
2019-06-26 01:56:55 +00:00
func File(path string) ([]byte, *logger.HTTPError) {
jsonFile, err := os.Open(path)
if err != nil {
2019-06-20 00:33:48 +00:00
log.Println("Error reading request. ", err)
2019-06-26 01:56:55 +00:00
return nil, &logger.HTTPError{Status: "500 Internal Server Error", StatusCode: 500}
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
2019-06-20 00:33:48 +00:00
return byteValue, nil
}
2019-06-27 00:41:18 +00:00
// Option get options data from json file
2019-06-26 01:56:55 +00:00
func Option() (options *Options) {
resp, err := File("./assets/json/options.json")
if err != nil {
return
}
json.Unmarshal(resp, &options)
return
}
2019-06-27 00:41:18 +00:00
// Contact get contact information from json file
func Contact() map[string]interface{} {
var contact Contacts
2019-06-20 00:39:24 +00:00
resp, err := File("./assets/json/contacts.json")
2019-06-20 00:33:48 +00:00
if err != nil {
m := structs.Map(err)
return m
}
json.Unmarshal(resp, &contact)
m := structs.Map(contact)
return m
}