Ejemplo n.º 1
0
// get exchange rate from fixer.io
func GetBase() string {
	err := godotenv.Load()    // load the dotenv
	perror(err)               // print to std error
	url := os.Getenv("EXAPI") // get the endpoint value

	res, err := http.Get(url) // fetch the api
	perror(err)

	defer res.Body.Close()
	jsonResponse, err := ioutil.ReadAll(res.Body) // read the respond body
	perror(err)

	data := JsonStruct{}
	json.Unmarshal(jsonResponse, &data) // put the response body into data obj
	perror(err)

	return data.Base // return the base currency
}
Ejemplo n.º 2
0
// main entry to service worker
func main() {
	doterr := godotenv.Load()
	perror(doterr)

	r := mux.NewRouter() // new mux instance

	r.HandleFunc("/", root.Start) // root route */*

	r.HandleFunc("/api/pay", api.Create)                // :api pay [:post] @ /api/pay
	r.HandleFunc("/api/rate/base", api.GetExchangeBase) // :api get currency exchange base
	r.HandleFunc("/api/rate", api.GetRate)              // :api get rates

	http.Handle("/", r) // http instance attach with mux

	port := os.Getenv("PORT")
	log.Println("Listening on ", port)
	err := http.ListenAndServe(port, nil)
	perror(err)

}