-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
132 lines (97 loc) · 2.85 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
import (
"net/http"
"os"
"github.com/bigroom/vision/models"
"github.com/bigroom/vision/tunnel"
"github.com/facebookgo/grace/gracehttp"
"github.com/getsentry/raven-go"
"github.com/gorilla/mux"
"github.com/koding/kite"
"github.com/paked/configure"
"github.com/paked/restrict"
log "github.com/sirupsen/logrus"
)
var (
messages chan tunnel.MessageArgs
clients map[string][]*conn
conf = configure.New()
dbName = conf.String("db-name", "postgres", "DB_NAME")
dbUser = conf.String("db-user", "postgres", "DB_USER")
dbPass = conf.String("db-pass", "postgres", "DB_PASS")
dbService = conf.String("db-service", "jarvis", "DB_SERVICE")
dbPort = conf.String("db-port", "5432", "DB_PORT")
defaultIRCServer = conf.String("default-irc", "chat.freenode.net", "default IRC host")
httpAddr = conf.String("http-address", "0.0.0.0", "Which address you want http to bind on")
rpcAddr = conf.String("rpc-address", "0.0.0.0", "Which address you want rpc to bind on")
httpPort = conf.String("http-port", "6060", "Which port you want http to bind on")
rpcPort = conf.String("rpc-port", "8080", "Which port you want rpc to bind on")
sentryDSN = conf.String("sentry-dsn", "", "The sentry DSN you want to use")
crypto = conf.String("crypto", "/crypto/app.rsa", "Your crypto")
sentry *raven.Client
pool *kite.Client
)
func init() {
conf.Use(configure.NewEnvironment())
conf.Use(configure.NewFlag())
conf.Parse()
messages = make(chan tunnel.MessageArgs)
clients = make(map[string][]*conn)
}
func main() {
var err error
sentry, err = raven.NewClient(*sentryDSN, nil)
if err != nil {
log.Println("No sentry:", err)
}
restrict.ReadCryptoKey(*crypto)
models.Init(
*dbUser,
*dbPass,
*dbService,
*dbPort,
*dbName,
)
go tunnel.NewRPCServer(messages, *rpcAddr, *rpcPort)
go messageLoop()
r := setupRouting()
pool = setupZombies()
log.Println(gracehttp.Serve(
&http.Server{
Addr: *httpAddr + ":" + *httpPort,
Handler: r,
},
))
}
func setupRouting() *mux.Router {
r := mux.NewRouter().
PathPrefix("/api").
Subrouter().
StrictSlash(true)
r.HandleFunc("/users", registerHandler).
Methods("POST")
r.HandleFunc("/users", loginHandler).
Methods("GET")
r.HandleFunc("/users/me", restrict.R(secretHandler)).
Methods("GET")
r.HandleFunc("/servers/{host}/{channel}/scrollback", restrict.R(messagesHandler)).
Methods("GET")
r.HandleFunc("/servers/default", defaultServerHandler).
Methods("GET")
r.HandleFunc("/ws", restrict.R(dispatchHandler))
return r
}
func setupZombies() *kite.Client {
k := kite.New("vision", "1.0.0")
url := "http://" + os.Getenv("ZOMBIES_PORT_3001_TCP_ADDR") + ":3001/kite"
pool := k.NewClient(url)
go func() {
connected, err := pool.DialForever()
if err != nil {
log.Warn("Got error connecting to zombies", err)
}
<-connected
log.Println("Connected!")
}()
return pool
}