Skip to content

Commit 3819e3b

Browse files
committed
switching from local file to dwnload from
github in startup
1 parent 5f8470f commit 3819e3b

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,3 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16-
17-
# TibiaData related files that should be ignored
18-
houses_mapping.json

src/HousesMapping.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package main
22

33
import (
44
"encoding/json"
5-
"io/ioutil"
65
"log"
6+
"net/http"
7+
"time"
8+
9+
"github.com/go-resty/resty/v2"
710
)
811

912
var (
@@ -21,22 +24,44 @@ type HousesMapping struct {
2124

2225
// TibiaDataHousesMappingInitiator func - used to load data from local JSON file
2326
func TibiaDataHousesMappingInitiator() {
24-
// load content from file into variable file
25-
file, err := ioutil.ReadFile("houses_mapping.json")
2627

27-
if err != nil {
28-
log.Println("[error] TibiaData API failed to load content from houses_mapping.json")
29-
} else {
30-
// loading json and mapping it into the data variable
28+
// Setting up resty client
29+
client := resty.New()
30+
31+
// Set client timeout and retry
32+
client.SetTimeout(5 * time.Second)
33+
client.SetRetryCount(2)
34+
35+
// Set headers for all requests
36+
client.SetHeaders(map[string]string{
37+
"Content-Type": "application/json",
38+
"User-Agent": TibiadataUserAgent,
39+
})
40+
41+
// Enabling Content length value for all request
42+
client.SetContentLength(true)
43+
44+
// Disable redirection of client (so we skip parsing maintenance page)
45+
client.SetRedirectPolicy(resty.NoRedirectPolicy())
46+
47+
TibiadataAssetsURL := "https://raw.githubusercontent.com/TibiaData/tibiadata-api-assets/main/data/houses_mapping.json"
48+
res, err := client.R().Get(TibiadataAssetsURL)
49+
50+
switch res.StatusCode() {
51+
case http.StatusOK:
52+
// adding response into the data field
3153
data := HousesMapping{}
32-
err = json.Unmarshal([]byte(file), &data)
54+
err = json.Unmarshal([]byte(res.Body()), &data)
3355

3456
if err != nil {
3557
log.Println("[error] TibiaData API failed to parse content from houses_mapping.json")
3658
} else {
3759
// storing data so it's accessible from other places
3860
TibiadataHousesMapping = data
3961
}
62+
63+
default:
64+
log.Printf("[error] TibiaData API failed to load houses mapping. %s", err)
4065
}
4166
}
4267

src/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ func main() {
4545
log.Printf("[debug] TIbiaData API User-Agent: %s", TibiadataUserAgent)
4646
}
4747

48-
// initializing houses mappings
49-
TibiaDataHousesMappingInitiator()
50-
5148
// starting webserver.go stuff
5249
runWebServer()
5350
}
@@ -75,4 +72,7 @@ func TibiaDataInitializer() {
7572

7673
log.Printf("[info] TibiaData API proxy: %s", TibiadataProxyDomain)
7774

75+
// initializing houses mappings
76+
TibiaDataHousesMappingInitiator()
77+
7878
}

0 commit comments

Comments
 (0)