Skip to content

Commit

Permalink
Merge pull request #6 from creztfallen/feat/endpoints
Browse files Browse the repository at this point in the history
feat: add endpoint for all rates
  • Loading branch information
creztfallen committed Sep 25, 2023
2 parents 52b8268 + 23b1f6e commit 1d69d65
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
25 changes: 23 additions & 2 deletions api/handlers/latestExchangeRate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"encoding/json"
"fmt"
"go-rich/models"
"go-rich/pubsub/utils"
"log"
"net/http"
"os"
"time"
"go-rich/pubsub/utils"

"github.com/joho/godotenv"
)
Expand Down Expand Up @@ -43,7 +43,28 @@ func LatestExchangeRateHandler(w http.ResponseWriter, r *http.Request) {
}
}()

time.Sleep(3 * time.Second)
time.Sleep(2 * time.Second)

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)
}

func LatestExchangeRatesHandler(w http.ResponseWriter, r *http.Request) {
if err := godotenv.Load(); err != nil {
log.Fatalf("Error loading .env file %v", err)
}

apiKey := os.Getenv("API_KEY")
apiEndpoint := fmt.Sprintf("https://api.currencyfreaks.com/latest?apikey=%s", apiKey)

response, err := http.Get(apiEndpoint)
if err != nil {
panic(err)
}

var result models.ExchangeRateResponse

json.NewDecoder(response.Body).Decode(&result)

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)
Expand Down
1 change: 1 addition & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

func main() {
http.HandleFunc("/latest", handlers.LatestExchangeRateHandler)
http.HandleFunc("/latests", handlers.LatestExchangeRatesHandler)

port := "8080"

Expand Down

0 comments on commit 1d69d65

Please sign in to comment.