-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (99 loc) · 2.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"encoding/json"
"errors"
"log"
"net/http"
"os"
"sync"
"time"
"github.com/go-resty/resty/v2"
)
var (
client = resty.New().SetTimeout(10 * time.Second)
cacheLock sync.RWMutex
exchangeCache interface{}
currenciesCache interface{}
lastExchangeFetch time.Time
lastCurrenciesFetch time.Time
appID string
exchangeRateURL = "https://openexchangerates.org/api/latest.json"
currenciesListURL = "https://openexchangerates.org/api/currencies.json"
cacheMinute = 5
)
func fetchDataFromAPI(url string) (interface{}, error) {
response, err := client.R().Get(url)
if err != nil || response.StatusCode() != http.StatusOK {
return nil, errors.New("failed to fetch data from external API")
}
var jsonResponse interface{}
if err := json.Unmarshal(response.Body(), &jsonResponse); err != nil {
return nil, errors.New("failed to parse API response")
}
return jsonResponse, nil
}
func nextCacheExpirationTime() time.Time {
now := time.Now()
nextHour := now.Truncate(time.Hour).Add(time.Hour)
return nextHour.Add(time.Minute * time.Duration(cacheMinute))
}
func isCacheExpired(lastFetch time.Time) bool {
now := time.Now()
expiration := nextCacheExpirationTime()
if now.Before(expiration) && now.Sub(lastFetch) < time.Hour {
return false
}
return true
}
func getCachedData(url string, cache *interface{}, lastFetch *time.Time) (interface{}, error) {
cacheLock.RLock()
if !isCacheExpired(*lastFetch) {
defer cacheLock.RUnlock()
return *cache, nil
}
cacheLock.RUnlock()
data, err := fetchDataFromAPI(url)
if err != nil {
return nil, err
}
cacheLock.Lock()
*cache = data
*lastFetch = time.Now()
cacheLock.Unlock()
return data, nil
}
func getLatestExchangeRate(w http.ResponseWriter, r *http.Request) {
data, err := getCachedData(exchangeRateURL+"?app_id="+appID, &exchangeCache, &lastExchangeFetch)
if err != nil {
sendErrorResponse(w, "Failed to fetch exchange rates")
return
}
sendDataResponse(w, data)
}
func getCurrencyList(w http.ResponseWriter, r *http.Request) {
data, err := getCachedData(currenciesListURL, ¤ciesCache, &lastCurrenciesFetch)
if err != nil {
sendErrorResponse(w, "Failed to fetch currency list")
return
}
sendDataResponse(w, data)
}
func sendDataResponse(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
func sendErrorResponse(w http.ResponseWriter, errMessage string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"error": errMessage})
}
func main() {
appID = os.Getenv("APP_ID")
if appID == "" {
log.Fatal("APP_ID environment variable is not set")
}
http.HandleFunc("/api/latest", getLatestExchangeRate)
http.HandleFunc("/api/currencies", getCurrencyList)
log.Println("Server is running on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}