This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (62 loc) · 1.37 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
package main
import (
"encoding/json"
"log"
"os"
"path"
"time"
"github.com/celrenheit/spider/schedulers"
"github.com/celrenheit/trending-machine/hubspider"
"github.com/celrenheit/trending-machine/web"
"gopkg.in/mgo.v2"
)
var (
PORT = ":3000"
MONGODB_URL = ":27017"
DBNAME = "ghtrending"
)
type Settings struct {
Languages []string `json:"languages"`
}
func init() {
if p := os.Getenv("PORT"); p != "" {
PORT = ":" + p
}
if m := os.Getenv("MONGODB_URL"); m != "" {
MONGODB_URL = m
_, name := path.Split(MONGODB_URL)
DBNAME = name
}
}
func main() {
dbSession := DBConnect(MONGODB_URL)
err := DBEnsureIndices(dbSession, DBNAME)
if err != nil {
log.Println("Error settings up indices")
log.Fatal(err)
}
settings, err := readConfig()
if err != nil {
log.Fatal(err)
}
go launchScheduler(dbSession.DB(DBNAME), settings)
server.Launch(dbSession, DBNAME, PORT)
}
func launchScheduler(db *mgo.Database, settings *Settings) {
scheduler := schedulers.NewBasicScheduler()
scheduler.Handle(hubspider.New(db, settings.Languages)).Every(12 * time.Hour)
log.Fatal(scheduler.Start())
}
func readConfig() (*Settings, error) {
file, err := os.Open("settings.json")
if err != nil {
return nil, err
}
defer file.Close()
var settings Settings
err = json.NewDecoder(file).Decode(&settings)
if err != nil {
return nil, err
}
return &settings, nil
}