diff --git a/api.go b/api.go index aca6777..c38dbfa 100755 --- a/api.go +++ b/api.go @@ -2,10 +2,10 @@ package request import ( "encoding/json" - "fmt" "html" "io/ioutil" "net/http" + "os" "strconv" "strings" "time" @@ -28,22 +28,25 @@ func Request(path string) ([]byte, *logger.HTTPError) { req.Header.Set("Accept", "application/json") req.Header.Set("Content-Type", "application/json") - ap := Option() - for _, api := range ap.API { - req.Header.Set(api.Name, api.Value) + if os.Getenv("GOENV") != "production" { + resp := API() + for _, api := range resp.API { + req.Header.Set(api.Name, api.Value) + } + } else { + req.Header.Set("Api-Key", os.Getenv("GOAPIKEY")) + req.Header.Set("Api-Username", os.Getenv("APIUSERNAME")) } client := &http.Client{Timeout: time.Second * 10} resp, err := client.Do(req) if err != nil { logger.ErrorLog("Error reading request. ", err) - fmt.Println(err) return nil, logger.HTTPErr(http.StatusInternalServerError) } body, err := ioutil.ReadAll(resp.Body) if err != nil { logger.ErrorLog("Error reading request. ", err) - fmt.Println(err) return nil, logger.HTTPErr(http.StatusInternalServerError) } defer resp.Body.Close() diff --git a/data.go b/data.go index 717e92d..ea75b1e 100644 --- a/data.go +++ b/data.go @@ -4,6 +4,14 @@ import ( "time" ) +// APIOpt API options +type APIOpt struct { + API []struct { + Name string `json:"name"` + Value string `json:"value"` + } `json:"api"` +} + // Options struct for application options type Options struct { Title string `json:"title"` @@ -18,10 +26,6 @@ type Options struct { DynamicReload bool `json:"DynamicReload"` BaseDir string `json:"BaseDir"` } `json:"ace"` - API []struct { - Name string `json:"name"` - Value string `json:"value"` - } `json:"api"` Database struct { Server string `json:"server"` Db string `json:"db"` diff --git a/file.go b/file.go index f928113..a4e2f7f 100644 --- a/file.go +++ b/file.go @@ -32,6 +32,16 @@ func Option() (options *Options) { return } +// API gets api options data from json file +func API() (api *APIOpt) { + resp, err := File("./assets/json/api.json") + if err != nil { + return + } + json.Unmarshal(resp, &api) + return +} + // Contact get contact information from json file func Contact() map[string]interface{} { var contact Contacts