diff --git a/src/router/routes/responses.go b/src/router/routes/responses.go index 2a3e239c..9bd3989a 100644 --- a/src/router/routes/responses.go +++ b/src/router/routes/responses.go @@ -36,3 +36,10 @@ type ExchangeResponse struct { Amount float64 `json:"amount"` Result float64 `json:"result"` } + +type CurrenciesResponse struct { + responseBase + + Base currency.Currency `json:"base"` + Currencies currency.Currencies `json:"currencies"` +} diff --git a/src/router/routes/route_currencies.go b/src/router/routes/route_currencies.go new file mode 100644 index 00000000..6002e719 --- /dev/null +++ b/src/router/routes/route_currencies.go @@ -0,0 +1,57 @@ +package routes + +import ( + "fmt" + "net/http" + "time" + + "github.com/hearchco/agent/src/cache" + "github.com/hearchco/agent/src/config" + "github.com/hearchco/agent/src/exchange" + "github.com/rs/zerolog/log" +) + +func routeCurrencies(w http.ResponseWriter, ver string, conf config.Exchange, db cache.DB, ttl time.Duration) error { + // Capture start time. + startTime := time.Now() + + // Get the cached currencies. + currencies, err := db.GetCurrencies(conf.BaseCurrency, conf.Engines) + if err != nil { + log.Error(). + Err(err). + Str("base", conf.BaseCurrency.String()). + Str("engines", fmt.Sprintf("%v", conf.Engines)). + Msg("Error while getting currencies from cache") + } + + // Create the exchange. + var exch exchange.Exchange + if currencies == nil { + // Fetch the currencies from the enabled engines. + exch = exchange.NewExchange(conf) + // Cache the currencies if any have been fetched. + if len(exch.Currencies()) > 0 { + err := db.SetCurrencies(conf.BaseCurrency, conf.Engines, exch.Currencies(), ttl) + if err != nil { + log.Error(). + Err(err). + Str("base", conf.BaseCurrency.String()). + Str("engines", fmt.Sprintf("%v", conf.Engines)). + Msg("Error while setting currencies in cache") + } + } + } else { + // Use the cached currencies. + exch = exchange.NewExchange(conf, currencies) + } + + return writeResponseJSON(w, http.StatusOK, CurrenciesResponse{ + responseBase{ + ver, + time.Since(startTime).Milliseconds(), + }, + conf.BaseCurrency, + exch.Currencies(), + }) +} diff --git a/src/router/routes/setup.go b/src/router/routes/setup.go index 02edc03c..d2f7c86b 100644 --- a/src/router/routes/setup.go +++ b/src/router/routes/setup.go @@ -102,6 +102,28 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) { } }) + // /currencies + mux.Get("/currencies", func(w http.ResponseWriter, r *http.Request) { + err := routeCurrencies(w, ver, conf.Exchange, db, conf.Server.Cache.TTL.Currencies) + if err != nil { + log.Error(). + Err(err). + Str("path", r.URL.Path). + Str("method", r.Method). + Msg("Failed to send response") + } + }) + mux.Post("/currencies", func(w http.ResponseWriter, r *http.Request) { + err := routeCurrencies(w, ver, conf.Exchange, db, conf.Server.Cache.TTL.Currencies) + if err != nil { + log.Error(). + Err(err). + Str("path", r.URL.Path). + Str("method", r.Method). + Msg("Failed to send response") + } + }) + // /proxy mux.Get("/proxy", func(w http.ResponseWriter, r *http.Request) { err := routeProxy(w, r, conf.Server.ImageProxy.Salt, conf.Server.ImageProxy.Timeout)