-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (72 loc) · 1.83 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
package main
import (
"fmt"
"time"
"os"
"strconv"
"log"
"github.com/alexcarol/bicing-oracle/db"
"github.com/alexcarol/bicing-oracle/station-state/datasource"
"github.com/alexcarol/bicing-oracle/station-state/parser"
"github.com/alexcarol/bicing-oracle/station-state/repository"
weatherDatasource "github.com/alexcarol/bicing-oracle/weather/datasource"
weatherRepository "github.com/alexcarol/bicing-oracle/weather/repository"
)
func main() {
db, err := db.GetRawDataDBFromEnv()
if err != nil {
panic(err)
}
storage := repository.NewSQLStorage(db)
weatherStorage := weatherRepository.NewSQLStorage(db)
pollingTime, err := strconv.Atoi(getEnv("BICING_API_POLLING_TIME", "45"))
if err != nil {
panic("Error converting ascii to integer " + err.Error())
}
ticker := time.NewTicker(time.Duration(pollingTime) * time.Second)
var dataProvider datasource.BicingDataProvider
if getEnv("BICING_API_FETCH_REAL_DATA", "1") == "1" {
dataProvider = datasource.ProvideAPIData
} else {
dataProvider = datasource.ProvideFakeData
}
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
weather, err := weatherDatasource.GetWeatherData()
if err != nil {
log.Print("Error getting weather data")
log.Println(err)
}
err = weatherStorage.PersistWeather(weather)
if err != nil {
log.Println("Error persisting weather", err)
}
apiData, err := dataProvider()
if err != nil {
fmt.Println(err)
break
}
data, err := parser.ParseXML(apiData)
if err != nil {
fmt.Println("Error parsing xml")
break
}
storage.PersistCollection(data)
case <-quit:
ticker.Stop()
return
}
}
}()
<-quit
}
func getEnv(key string, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}