-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
79 lines (59 loc) · 1.7 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/go-co-op/gocron"
"github.com/joho/godotenv"
"github.com/julienschmidt/httprouter"
_ "github.com/lib/pq"
)
func SetupRouter() *httprouter.Router {
router := httprouter.New()
// V2 Routes
router.GET("/v2/", CapacityAndNonCapacitySailingsEndpoint)
router.GET("/v2/capacity/", CapacitySailingsEndpoint)
router.GET("/v2/noncapacity/", NonCapacitySailingsEndpoint)
// V1 Routes
router.GET("/api/", AllSailingsEndpoint)
router.GET("/api/:departureTerminal/", SailingsByDepartureTerminal)
router.GET("/api/:departureTerminal/:destinationTerminal/", SailingsByDepartureAndDestinationTerminals)
router.GET("/healthcheck/", HealthCheck)
router.NotFound = http.FileServer(http.Dir("./static"))
return router
}
func SetupCron() {
s := gocron.NewScheduler(time.UTC)
s.Every(1).Minute().Do(func() {
ScrapeCapacityRoutes()
})
s.Every(1).Hour().Do(func() {
ScrapeNonCapacityRoutes()
})
s.StartAsync()
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file: File not found")
} else {
fmt.Println("INFO: .env file loaded")
}
if os.Getenv("DB_USER") == "" || os.Getenv("DB_PASS") == "" || os.Getenv("DB_NAME") == "" || os.Getenv("DB_HOST") == "" || os.Getenv("DB_PORT") == "" || os.Getenv("DB_SSL") == "" {
log.Fatal("Error loading .env file: Missing variables")
} else {
fmt.Println("INFO: .env file valid")
}
db := GetPostgresInstance()
defer db.Close()
SetupCron()
var port = os.Getenv("PORT")
if port == "" {
port = "8080"
fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port)
}
router := SetupRouter()
http.ListenAndServe(":"+port, router)
}