forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
160 lines (142 loc) · 3.76 KB
/
server.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"crypto/tls"
"encoding/base64"
"flag"
"fmt"
"net/http"
"os"
"strconv"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/jackc/pgx"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/kavu/go_reuseport"
"github.com/flynn/flynn/discoverd/client"
"github.com/flynn/flynn/pkg/postgres"
"github.com/flynn/flynn/pkg/shutdown"
"github.com/flynn/flynn/router/types"
)
type Listener interface {
Start() error
Close() error
AddRoute(*router.Route) error
UpdateRoute(*router.Route) error
RemoveRoute(id string) error
Watcher
DataStoreReader
}
type Router struct {
HTTP Listener
TCP Listener
}
func (s *Router) Start() error {
if err := s.HTTP.Start(); err != nil {
return err
}
if err := s.TCP.Start(); err != nil {
return err
}
return nil
}
var listenFunc = reuseport.NewReusablePortListener
func main() {
defer shutdown.Exit()
apiPort := os.Getenv("PORT")
if apiPort == "" {
apiPort = "5000"
}
var cookieKey *[32]byte
if key := os.Getenv("COOKIE_KEY"); key != "" {
res, err := base64.StdEncoding.DecodeString(key)
if err != nil {
shutdown.Fatal("error decoding COOKIE_KEY:", err)
}
var k [32]byte
copy(k[:], res)
cookieKey = &k
}
httpAddr := flag.String("httpaddr", ":8080", "http listen address")
httpsAddr := flag.String("httpsaddr", ":4433", "https listen address")
tcpIP := flag.String("tcpip", "", "tcp router listen ip")
tcpRangeStart := flag.Int("tcp-range-start", 3000, "tcp port range start")
tcpRangeEnd := flag.Int("tcp-range-end", 3500, "tcp port range end")
certFile := flag.String("tlscert", "", "TLS (SSL) cert file in pem format")
keyFile := flag.String("tlskey", "", "TLS (SSL) key file in pem format")
apiAddr := flag.String("apiaddr", ":"+apiPort, "api listen address")
flag.Parse()
keypair := tls.Certificate{}
var err error
if *certFile != "" {
if keypair, err = tls.LoadX509KeyPair(*certFile, *keyFile); err != nil {
shutdown.Fatal(err)
}
} else if tlsCert := os.Getenv("TLSCERT"); tlsCert != "" {
if tlsKey := os.Getenv("TLSKEY"); tlsKey != "" {
os.Setenv("TLSKEY", fmt.Sprintf("md5^(%s)", md5sum(tlsKey)))
if keypair, err = tls.X509KeyPair([]byte(tlsCert), []byte(tlsKey)); err != nil {
shutdown.Fatal(err)
}
}
}
db, err := postgres.Open("", "")
if err != nil {
shutdown.Fatal(err)
}
if err := migrateDB(db.DB); err != nil {
shutdown.Fatal(err)
}
var pgport int
if port := os.Getenv("PGPORT"); port != "" {
var err error
if pgport, err = strconv.Atoi(port); err != nil {
shutdown.Fatal(err)
}
}
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
Port: uint16(pgport),
Database: os.Getenv("PGDATABASE"),
User: os.Getenv("PGUSER"),
Password: os.Getenv("PGPASSWORD"),
},
})
if err != nil {
shutdown.Fatal(err)
}
shutdown.BeforeExit(func() { pgxpool.Close() })
r := Router{
TCP: &TCPListener{
IP: *tcpIP,
startPort: *tcpRangeStart,
endPort: *tcpRangeEnd,
ds: NewPostgresDataStore("tcp", pgxpool),
discoverd: discoverd.DefaultClient,
},
HTTP: &HTTPListener{
Addr: *httpAddr,
TLSAddr: *httpsAddr,
cookieKey: cookieKey,
keypair: keypair,
ds: NewPostgresDataStore("http", pgxpool),
discoverd: discoverd.DefaultClient,
},
}
if err := r.Start(); err != nil {
shutdown.Fatal(err)
}
listener, err := listenFunc("tcp4", *apiAddr)
if err != nil {
shutdown.Fatal(err)
}
services := map[string]string{
"router-api": *apiAddr,
"router-http": *httpAddr,
}
for service, addr := range services {
hb, err := discoverd.AddServiceAndRegister(service, addr)
if err != nil {
shutdown.Fatal(err)
}
shutdown.BeforeExit(func() { hb.Close() })
}
shutdown.Fatal(http.Serve(listener, apiHandler(&r)))
}