-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (44 loc) · 1.68 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
package main
import (
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/go-co-op/gocron"
"rpallas/oil/prices/gasstations"
"rpallas/oil/prices/positions"
"rpallas/oil/prices/positions/geocoding"
"rpallas/oil/prices/routes"
"rpallas/oil/prices/routes/directions"
"time"
)
func main() {
gasStationsRepository := gasstations.GasStationsRepository{Datasource: &gasstations.GasStationsDatasource{}}
directionsRepository := directions.DirectionsRepository{Datasource: &directions.DirectionsDatasource{}}
positionsRepository := geocoding.PositionsRepository{Datasource: &geocoding.PositionsDatasource{}}
routesController := routes.RoutesController{
DirectionsRepository: &directionsRepository,
GasStationsRepository: &gasStationsRepository,
PositionsRepository: &positionsRepository,
}
warmup(&gasStationsRepository)
scheduleWarmUp(&gasStationsRepository)
r := gin.Default()
r.Use(cors.Default())
r.GET("/findGasStations/coordinates/:origin/:destination", routesController.FindGasStationsByCoordinates)
r.GET("/findGasStations/cities/:origin/:destination", routesController.FindGasStationsByCities)
r.Run()
}
func scheduleWarmUp(gasStationsRepository *gasstations.GasStationsRepository) {
s := gocron.NewScheduler(time.UTC)
_, _ = s.Cron("0 */4 * * *").Do(func() {
fmt.Println("Executing scheduled warmup")
warmup(gasStationsRepository)
}) // every 4 hours
s.StartAsync()
}
func warmup(gasStationsRepository *gasstations.GasStationsRepository) {
fmt.Println("Warming up gas stations")
somePosition := positions.Position{Lat: 42.241915, Lon: 0.412275}
gasStationsRepository.GetGasStationsByDistance(somePosition, 100)
fmt.Println("Warm up finished")
}