forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
184 lines (159 loc) · 4.3 KB
/
route.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"bytes"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/flynn/go-docopt"
"github.com/flynn/flynn/controller/client"
"github.com/flynn/flynn/router/types"
)
func init() {
register("route", runRoute, `
usage: flynn route
flynn route add http [-s <service>] [-c <tls-cert> -k <tls-key>] [--sticky] <domain>
flynn route add tcp [-s <service>]
flynn route remove <id>
Manage routes for application.
Options:
-s, --service <service> service name to route domain to (defaults to APPNAME-web)
-c, --tls-cert <tls-cert> path to PEM encoded certificate for TLS, - for stdin (http only)
-k, --tls-key <tls-key> path to PEM encoded private key for TLS, - for stdin (http only)
--sticky enable cookie-based sticky routing (http only)
Commands:
With no arguments, shows a list of routes.
add adds a route to an app
remove removes a route
Examples:
$ flynn route add http example.com
$ flynn route add tcp
`)
}
func runRoute(args *docopt.Args, client *controller.Client) error {
if args.Bool["add"] {
switch {
case args.Bool["http"]:
return runRouteAddHTTP(args, client)
case args.Bool["tcp"]:
return runRouteAddTCP(args, client)
default:
return fmt.Errorf("Route type %s not supported.", args.String["-t"])
}
} else if args.Bool["remove"] {
return runRouteRemove(args, client)
}
routes, err := client.RouteList(mustApp())
if err != nil {
return err
}
w := tabWriter()
defer w.Flush()
var route, protocol, service string
listRec(w, "ROUTE", "SERVICE", "ID")
for _, k := range routes {
switch k.Type {
case "tcp":
protocol = "tcp"
route = strconv.Itoa(k.TCPRoute().Port)
service = k.TCPRoute().Service
case "http":
route = k.HTTPRoute().Domain
service = k.TCPRoute().Service
if k.HTTPRoute().TLSCert == "" {
protocol = "http"
} else {
protocol = "https"
}
}
listRec(w, protocol+":"+route, service, k.ID)
}
return nil
}
func runRouteAddTCP(args *docopt.Args, client *controller.Client) error {
service := args.String["--service"]
if service == "" {
service = mustApp() + "-web"
}
hr := &router.TCPRoute{Service: service}
r := hr.ToRoute()
if err := client.CreateRoute(mustApp(), r); err != nil {
return err
}
hr = r.TCPRoute()
fmt.Printf("%s listening on port %d\n", r.ID, hr.Port)
return nil
}
func runRouteAddHTTP(args *docopt.Args, client *controller.Client) error {
var tlsCert []byte
var tlsKey []byte
service := args.String["--service"]
if service == "" {
service = mustApp() + "-web"
}
tlsCertPath := args.String["--tls-cert"]
tlsKeyPath := args.String["--tls-key"]
if tlsCertPath != "" && tlsKeyPath != "" {
var stdin []byte
var err error
if tlsCertPath == "-" || tlsKeyPath == "-" {
stdin, err = ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("Failed to read from stdin: %s", err)
}
}
tlsCert, err = readPEM("CERTIFICATE", tlsCertPath, stdin)
if err != nil {
return fmt.Errorf("Failed to read TLS cert: %s", err)
}
tlsKey, err = readPEM("PRIVATE KEY", tlsKeyPath, stdin)
if err != nil {
return fmt.Errorf("Failed to read TLS key: %s", err)
}
} else if tlsCertPath != "" || tlsKeyPath != "" {
return errors.New("Both the TLS certificate AND private key need to be specified")
}
hr := &router.HTTPRoute{
Service: service,
Domain: args.String["<domain>"],
TLSCert: string(tlsCert),
TLSKey: string(tlsKey),
Sticky: args.Bool["sticky"],
}
route := hr.ToRoute()
if err := client.CreateRoute(mustApp(), route); err != nil {
return err
}
fmt.Println(route.ID)
return nil
}
func readPEM(typ string, path string, stdin []byte) ([]byte, error) {
if path == "-" {
var buf bytes.Buffer
var block *pem.Block
for {
block, stdin = pem.Decode(stdin)
if block == nil {
break
}
if block.Type == typ {
pem.Encode(&buf, block)
}
}
if buf.Len() > 0 {
return buf.Bytes(), nil
}
return nil, errors.New("No PEM blocks found in stdin")
}
return ioutil.ReadFile(path)
}
func runRouteRemove(args *docopt.Args, client *controller.Client) error {
routeID := args.String["<id>"]
if err := client.DeleteRoute(mustApp(), routeID); err != nil {
return err
}
fmt.Printf("Route %s removed.\n", routeID)
return nil
}