-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (116 loc) · 2.88 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
// TODO implement peer-to-peer leader election somehow
import (
"context"
"flag"
"github.com/google/uuid"
"github.com/himanshub16/upnext-backend/cluster"
"log"
"math/rand"
"net/url"
"os"
"os/signal"
"sync"
"time"
)
var (
runDs bool
apiUrl string
discoUrl string
clusterUrl string
nodeID string
authToken string
electionOnly bool
wg sync.WaitGroup
)
func parseFlags() {
flag.BoolVar(&runDs, "runds", false, "Run discovery service")
flag.StringVar(&apiUrl, "apiurl", "127.0.0.1:3000", "URL for ReST API")
flag.StringVar(&discoUrl, "discourl", "127.0.0.1:4000", "URL for cluster discovery")
flag.StringVar(&clusterUrl, "clusterurl", "ws://127.0.0.1:5000", "URL for cluster service to start")
flag.StringVar(&authToken, "authtoken", "secrettoken", "Auth token for cluster nodes")
flag.BoolVar(&electionOnly, "electiononly", false, "Demo election process")
u, _ := uuid.NewUUID()
nodeID = u.String()
flag.Parse()
}
func prepareWebService() *ServiceImpl {
var (
userRepo UserRepository
linkRepo LinkRepository
voteRepo VoteRepository
testRepo TestRepository
pgdb *PostgresRepository
sqlitedb *SQLiteRepository
)
var dbUrl string = os.Getenv("DB_URL")
log.Println("database url", dbUrl)
if u, err := url.Parse(dbUrl); err == nil {
switch u.Scheme {
case "sqlite":
sqlitedb = NewSQLiteRepository(u.Hostname())
userRepo = sqlitedb
linkRepo = sqlitedb
voteRepo = sqlitedb
testRepo = sqlitedb
case "postgres":
pgdb = NewPostgresRepository(dbUrl)
userRepo = pgdb
linkRepo = pgdb
voteRepo = pgdb
testRepo = pgdb
}
}
service := &ServiceImpl{
userRepo: userRepo,
linkRepo: linkRepo,
voteRepo: voteRepo,
testRepo: testRepo,
}
return service
}
func main() {
parseFlags()
rand.Seed(time.Now().UnixNano())
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
me := cluster.NodeInfoT{
NodeID: nodeID,
URL: clusterUrl,
Priority: rand.Intn(100),
}
log.Println("starting cluster at ", clusterUrl)
log.Println("This is me : ", me)
service := prepareWebService()
c := cluster.NewClusterService(clusterUrl, discoUrl, me, authToken)
r := NewRadio(service, c.Shm)
log.Println(r.shm, r.nowPlaying)
apiRouter := NewHTTPRouter(service, r)
go c.Start()
go apiRouter.Start(apiUrl)
for {
select {
case <-interrupt:
c.Shutdown()
if !electionOnly {
r.Shutdown()
apiRouter.Shutdown(context.Background())
log.Println("stopping api router")
}
case isLeader := <-c.SwitchMode:
log.Println("isLeader > ", isLeader)
if !electionOnly {
if isLeader {
r.SwitchMode(masterRadio)
// apiRouter.Shutdown(context.Background())
log.Println("stopping api router")
} else {
r.SwitchMode(peerRadio)
// go apiRouter.Start(apiUrl)
log.Println("starting http router")
}
}
}
}
log.Println("time ends now")
}