Skip to content

Commit 94c21f5

Browse files
Add .env file for port
1 parent c0aa016 commit 94c21f5

File tree

5 files changed

+34
-17
lines changed

5 files changed

+34
-17
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.idea/Currencies.iml
2+
/.idea/modules.xml
3+
/.idea/vcs.xml

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ go 1.16
55
require (
66
github.com/go-redis/redis v6.15.9+incompatible
77
github.com/gorilla/mux v1.8.0
8+
github.com/joho/godotenv v1.3.0 // indirect
89
github.com/onsi/gomega v1.13.0 // indirect
910
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
2222
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
2323
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
2424
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
25+
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
26+
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
2527
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
2628
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
2729
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=

main.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"os"
78
"strings"
8-
9+
910
"github.com/gorilla/mux"
11+
"github.com/joho/godotenv"
1012
)
1113

1214
type CurrencyConversion struct {
@@ -22,15 +24,16 @@ type ConversionsResponse struct {
2224
}
2325

2426
func main() {
25-
27+
godotenv.Load()
28+
2629
InitRedis()
27-
30+
2831
router := mux.NewRouter()
29-
32+
3033
router.HandleFunc("/latest", CORS(Latest))
31-
32-
fmt.Println("* Running on http://127.0.0.1:3000/")
33-
http.ListenAndServe(":3000", router)
34+
35+
fmt.Println("Running on http://127.0.0.1:" + os.Getenv("PORT"))
36+
http.ListenAndServe(":"+os.Getenv("PORT"), router)
3437
}
3538

3639
func CORS(fn http.HandlerFunc) http.HandlerFunc {
@@ -51,46 +54,46 @@ func CORS(fn http.HandlerFunc) http.HandlerFunc {
5154

5255
func Latest(w http.ResponseWriter, r *http.Request) {
5356
baseParam := r.URL.Query().Get("base")
54-
57+
5558
if baseParam == "" {
5659
baseParam = "EUR"
5760
}
5861
responseData := &ConversionsResponse{
5962
Base: baseParam,
6063
Currencies: make(map[string]CurrencyConversion),
6164
}
62-
65+
6366
currenciesMap, err := GetOrUpdateCurrencies()
64-
67+
6568
if err != nil {
6669
http.Error(w, http.StatusText(500), 500)
6770
return
6871
}
69-
72+
7073
currencies := *currenciesMap
71-
74+
7275
for _, currency := range currencies {
7376
baseCurrency := currencies[strings.ToUpper(baseParam)]
74-
77+
7578
if baseParam != "EUR" {
7679
currency.Rate = currency.Rate / baseCurrency.Rate
7780
}
78-
81+
7982
responseData.Currencies[currency.Currency] = CurrencyConversion{
8083
From: baseCurrency.Currency,
8184
To: currency.Currency,
8285
Rate: currency.Rate,
8386
Currency: currency.Currency,
8487
}
8588
}
86-
89+
8790
response, err := json.Marshal(responseData)
88-
91+
8992
if err != nil {
9093
http.Error(w, http.StatusText(500), 500)
9194
return
9295
}
93-
96+
9497
w.Header().Set("Content-Type", "application/json")
9598
w.Write(response)
9699
}

0 commit comments

Comments
 (0)