-
Notifications
You must be signed in to change notification settings - Fork 0
/
hydra.go
106 lines (92 loc) · 2.99 KB
/
hydra.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
package main
import (
"net"
"net/http"
"os"
"time"
"github.com/innotech/hydra/config"
"github.com/innotech/hydra/database/connector"
"github.com/innotech/hydra/etcd"
"github.com/innotech/hydra/load_balancer"
"github.com/innotech/hydra/log"
"github.com/innotech/hydra/server"
)
func main() {
// Load configuration.
var conf = config.New()
if err := conf.Load(os.Args[1:]); err != nil {
log.Fatal(err.Error() + "\n")
}
// Enable verbose option.
if conf.Verbose {
log.Verbose = true
}
if conf.DataDir == "" {
log.Fatal("Data dir does't exist")
}
// Create data directory if it doesn't already exist.
if err := os.MkdirAll(conf.DataDir, 0744); err != nil {
log.Fatalf("Unable to create path: %s", err)
}
// Load etcd configuration.
if err := conf.LoadEtcdConfig(); err != nil {
log.Fatalf("Unable to load etcd conf: %s", err)
}
// Launch services
var etcd = etcd.New(conf.EtcdConf)
etcd.Load()
hydraEnv := os.Getenv("HYDRA_ENV")
if hydraEnv == "ETCD_TEST" {
etcd.Start(true)
} else {
go func() {
var withEtcdServer bool = false
if conf.EtcdAddr != "" {
withEtcdServer = true
}
etcd.Start(withEtcdServer)
}()
connector.SetEtcdConnector(etcd)
// Private Server API
privateHydraListener, err := net.Listen("tcp", conf.PrivateAddr)
if err != nil {
log.Fatalf("Failed to create hydra private listener: ", err)
}
var privateServer = server.NewPrivateServer(privateHydraListener, conf.InstanceExpirationTime)
privateServer.RegisterHandlers()
go func() {
log.Infof("hydra private server [name %s, listen on %s, advertised url %s]", conf.Name, conf.PrivateAddr, "http://"+conf.PrivateAddr)
log.Fatal(http.Serve(privateServer.Listener, privateServer.Router))
}()
// Public Server API
var loadBalancerFrontendEndpoint string = "ipc://" + conf.Name + "-frontend.ipc"
publicHydraListener, err := net.Listen("tcp", conf.PublicAddr)
if err != nil {
log.Fatalf("Failed to create hydra public listener: ", err)
}
var publicServer = server.NewPublicServer(publicHydraListener, loadBalancerFrontendEndpoint, conf.BalanceTimeout)
publicServer.RegisterHandlers()
go func() {
log.Infof("hydra public server [name %s, listen on %s, advertised url %s]", conf.Name, conf.PublicAddr, "http://"+conf.PublicAddr)
log.Fatal(http.Serve(publicServer.Listener, publicServer.Router))
}()
// Load applications.
var appsConfig = config.NewApplicationsConfig()
if _, err := os.Stat(conf.AppsFile); os.IsNotExist(err) {
log.Warnf("Unable to find apps file: %s", err)
} else {
if err := appsConfig.Load(conf.AppsFile); err != nil {
log.Fatalf("Unable to load applications: %s", err)
}
}
time.Sleep(1 * time.Second)
// Persist Configured applications
if err := appsConfig.Persists(); err != nil {
log.Fatalf("Failed to save configured applications: ", err)
}
// Load Balancer
loadBalancer := load_balancer.NewLoadBalancer(loadBalancerFrontendEndpoint, "tcp://"+conf.LoadBalancerAddr)
defer loadBalancer.Close()
loadBalancer.Run()
}
}