forked from nanopack/mist
/
main.go
86 lines (71 loc) · 2.31 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
// Copyright (c) 2015 Pagoda Box Inc
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v.
// 2.0. If a copy of the MPL was not distributed with this file, You can obtain one
// at http://mozilla.org/MPL/2.0/.
//
package main
import (
"github.com/jcelliott/lumber"
"github.com/nanobox-io/golang-discovery"
"github.com/nanobox-io/nanobox-api"
"github.com/nanobox-io/nanobox-config"
"github.com/nanopack/mist/authenticate"
"github.com/nanopack/mist/core"
"github.com/nanopack/mist/handlers"
"os"
"strings"
"time"
)
func main() {
configFile := ""
if len(os.Args) > 1 && !strings.HasPrefix(os.Args[1], "-") {
configFile = os.Args[1]
}
defaults := map[string]string{
"tcp_listen_address": "127.0.0.1:1445",
"http_listen_address": "127.0.0.1:8080",
"log_level": "INFO",
"multicast_interface": "eth1",
"pg_user": "postgres",
"pg_database": "postgres",
"pg_address": "127.0.0.1:5432",
}
config.Load(defaults, configFile)
level := lumber.LvlInt(config.Config["log_level"])
mist := mist.New()
api.Name = "MIST"
api.Logger = lumber.NewConsoleLogger(level)
api.User = mist
user := config.Config["pg_user"]
database := config.Config["pg_database"]
address := config.Config["pg_address"]
pgAuth, err := authenticate.NewPostgresqlAuthenticator(user, database, address)
if err != nil {
api.Logger.Fatal("unable to start postgresql authenticator %v", err)
os.Exit(1)
}
authCommands := handlers.GenerateAdditionalCommands(pgAuth)
listen := config.Config["tcp_listen_address"]
server, err := mist.Listen(listen, authCommands)
if err != nil {
api.Logger.Fatal("unable to start mist tcp listener %v", err)
os.Exit(1)
}
defer server.Close()
// start discovering other mist nodes on the network
discover, err := discovery.NewDiscovery(config.Config["multicast_interface"], "mist", time.Second*2)
if err != nil {
panic(err)
}
defer discover.Close()
// advertise this nodes listen address
discover.Add("mist", listen)
// enable replication between mist nodes
replicate := handlers.EnableReplication(mist, discover)
go replicate.Monitor()
// start up the authenticated websocket connection
authenticator := authenticate.NewNoopAuthenticator()
handlers.LoadWebsocketRoute(authenticator)
api.Start(config.Config["http_listen_address"])
}