api now has to be set in ENV for production

This commit is contained in:
Michael 2019-08-20 08:16:05 +01:00
parent 1a2d9265e0
commit 82c31cb3cc
3 changed files with 27 additions and 10 deletions

15
api.go
View File

@ -2,10 +2,10 @@ package request
import ( import (
"encoding/json" "encoding/json"
"fmt"
"html" "html"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -28,22 +28,25 @@ func Request(path string) ([]byte, *logger.HTTPError) {
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
ap := Option() if os.Getenv("GOENV") != "production" {
for _, api := range ap.API { resp := API()
req.Header.Set(api.Name, api.Value) 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} client := &http.Client{Timeout: time.Second * 10}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
logger.ErrorLog("Error reading request. ", err) logger.ErrorLog("Error reading request. ", err)
fmt.Println(err)
return nil, logger.HTTPErr(http.StatusInternalServerError) return nil, logger.HTTPErr(http.StatusInternalServerError)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
logger.ErrorLog("Error reading request. ", err) logger.ErrorLog("Error reading request. ", err)
fmt.Println(err)
return nil, logger.HTTPErr(http.StatusInternalServerError) return nil, logger.HTTPErr(http.StatusInternalServerError)
} }
defer resp.Body.Close() defer resp.Body.Close()

12
data.go
View File

@ -4,6 +4,14 @@ import (
"time" "time"
) )
// APIOpt API options
type APIOpt struct {
API []struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"api"`
}
// Options struct for application options // Options struct for application options
type Options struct { type Options struct {
Title string `json:"title"` Title string `json:"title"`
@ -18,10 +26,6 @@ type Options struct {
DynamicReload bool `json:"DynamicReload"` DynamicReload bool `json:"DynamicReload"`
BaseDir string `json:"BaseDir"` BaseDir string `json:"BaseDir"`
} `json:"ace"` } `json:"ace"`
API []struct {
Name string `json:"name"`
Value string `json:"value"`
} `json:"api"`
Database struct { Database struct {
Server string `json:"server"` Server string `json:"server"`
Db string `json:"db"` Db string `json:"db"`

10
file.go
View File

@ -32,6 +32,16 @@ func Option() (options *Options) {
return 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 // Contact get contact information from json file
func Contact() map[string]interface{} { func Contact() map[string]interface{} {
var contact Contacts var contact Contacts