-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 1.61 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
package main
import (
"flag"
"log"
"os"
"strconv"
"time"
"github.com/Bolado/ai-tracker/database"
router "github.com/Bolado/ai-tracker/router"
"github.com/Bolado/ai-tracker/watcher"
"github.com/joho/godotenv"
)
var (
disableWatcher bool
)
func init() {
// parse command line flags
flag.BoolVar(&disableWatcher, "disable-watcher", false, "flag to disable the watcher, intended for web server only")
flag.Parse()
}
func main() {
// change logger so it prints more information when logging
log.SetFlags(log.LstdFlags | log.Lshortfile)
//get env variables
godotenv.Load()
interval, _ := strconv.Atoi(os.Getenv("WATCHER_INTERVAL"))
// initialize the database
err := database.StartDatabase()
if err != nil {
log.Fatalf("Failed to start database: %v\n", err)
}
log.Println("Database started")
defer database.CloseDatabase()
// load articles from the database
if err := watcher.LoadArticles(); err != nil {
log.Fatalf("Failed to load articles: %v\n", err)
}
log.Println("Articles loaded")
if !disableWatcher {
// start the watcher
go func() {
for {
if err := watcher.StartWatcher(); err != nil {
log.Fatalf("Failed to start watcher: %v\n", err)
}
if interval == 0 {
log.Println("Watcher will sleep for 15 minutes")
time.Sleep(15 * time.Minute)
} else {
log.Printf("Watcher will sleep for %d minutes\n", interval)
time.Sleep(time.Duration(interval) * time.Minute)
}
}
}()
log.Println("Watcher started")
}
// start the router and listen for requests
if err := router.StartRouter(); err != nil {
log.Fatalf("Failed to start router: %v\n", err)
}
}