I'm working on a web service application with endpoint/crypto/rates accepting as an input two “symbols” of currencies. The web service should search for currency exchange data between these characters in the database, and if there is no value in the database, or the timestamp value differs from the current by more than 1 minute, make a request to the service API: https://min-api.cryptocompare.com/documentation So, I created the struct and the go-chi router. But I don't know how to build a working handler to get parameters from URL, for example: https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD
package main
import (
"github.com/go-chi/chi"
"net/http"
)
type Crypto struct {
Cur1 string
Cur2 string
Rate float64
Timestamp int64
}
func main() {
port := ":3000"
r := chi.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Connected"))
})
http.ListenAndServe(port, r)
I think I can try to create Cur1 for first value on the handler body:
cur1 = r.FormValue("cur1")
Similarly for second value:
Cur2 = r.FormValue("cur2")
The final request will be: ~/get_rates?cur1=eth&cur2=btc
You can extract query params from a request by calling the getter r.URL.Query().Get("paramName")
Extracting cur1 and cur2 for your task will be something like that:
r.Get("/get_rates", func(w http.ResponseWriter, r *http.Request) {
cur1 := r.URL.Query().Get("cur1")
cur2 := r.URL.Query().Get("cur2")
w.Write([]byte("cur1=" + cur1 + "; cur2=" + cur2))
})