-
Notifications
You must be signed in to change notification settings - Fork 1
/
configurations.go
83 lines (65 loc) · 2.04 KB
/
configurations.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
package main
import (
"flag"
"log"
"time"
"github.com/gaocegege/the-big-brother-is-watching-you/api"
"github.com/gaocegege/the-big-brother-is-watching-you/source"
"github.com/gaocegege/the-big-brother-is-watching-you/storage"
"github.com/gaocegege/the-big-brother-is-watching-you/timer"
"github.com/gaocegege/the-big-brother-is-watching-you/worker"
"github.com/emicklei/go-restful"
"gopkg.in/mgo.v2"
)
var (
mongoIP = flag.String("mongo-db-ip", "localhost", "the location of the mongodb")
period = flag.Int64("poll-period", 60, "poll period of the ticker")
mockMode = flag.Bool("mock-mode", false, "add mock as a source for test")
githubUsername = flag.String("github-username", "", "the username that you wanna watch")
)
// Init the main process
func Init(session *mgo.Session) {
if !flag.Parsed() {
flag.Parse()
}
// alloc the collection managers
vendorCM := storage.NewVendorCollectionManager(session)
recordCM := storage.NewRecordCollectionManager(session)
// alloc source manager
sourceM, err := source.NewManager(*mockMode, *githubUsername)
if err != nil {
log.Fatal(err)
}
// alloc the worker
worker := worker.NewWorker(sourceM, vendorCM, recordCM)
// alloc timer
timer, err := timer.NewTimer(time.Duration(*period), worker)
if err != nil {
log.Fatal(err)
}
timer.Run()
// run rest service
runRESTService()
}
// runRESTService runs the rest service
func runRESTService() {
ws := &restful.WebService{}
ws.Consumes(restful.MIME_JSON, "text/plain", "text/event-stream").
Produces(restful.MIME_JSON, "text/plain", "text/event-stream")
log.Print("Register API to server...")
// register healthz to rest service
registerHealthzCheckAPI(ws)
restful.Add(ws)
}
// getMongoSession returns a mongo session
func getMongoSession() *mgo.Session {
session, err := mgo.Dial(*mongoIP)
if err != nil {
log.Fatal("Error dailing mongodb")
}
session.SetMode(mgo.Strong, true)
return session
}
func registerHealthzCheckAPI(ws *restful.WebService) {
ws.Route(ws.GET("/healthz").To(api.Healthz).Doc("Health Check"))
}