This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
api.go
177 lines (154 loc) · 3.67 KB
/
api.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"sort"
"time"
"github.com/flynn/strowger/types"
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
)
func apiHandler(router *Router) http.Handler {
r := martini.NewRouter()
m := martini.New()
m.Use(martini.Logger())
m.Use(martini.Recovery())
m.Use(render.Renderer())
m.Action(r.Handle)
m.Map(router)
r.Post("/routes", binding.Bind(strowger.Route{}), createRoute)
r.Put("/routes", binding.Bind(strowger.Route{}), createOrReplaceRoute)
r.Get("/routes", getRoutes)
r.Get("/routes/:route_type/:route_id", getRoute)
r.Delete("/routes/:route_type/:route_id", deleteRoute)
return m
}
func createRoute(req *http.Request, route strowger.Route, router *Router, r render.Render) {
now := time.Now()
route.CreatedAt = &now
route.UpdatedAt = &now
l := listenerFor(router, route.Type)
if l == nil {
r.JSON(400, "Invalid route type")
return
}
if err := l.AddRoute(&route); err != nil {
log.Println(err)
r.JSON(500, struct{}{})
return
}
res := formatRoute(&route)
r.JSON(200, res)
}
func createOrReplaceRoute(req *http.Request, route strowger.Route, router *Router, r render.Render) {
now := time.Now()
route.CreatedAt = &now
route.UpdatedAt = &now
l := listenerFor(router, route.Type)
if l == nil {
r.JSON(400, "Invalid route type")
return
}
if err := l.SetRoute(&route); err != nil {
log.Println(err)
r.JSON(500, struct{}{})
return
}
res := formatRoute(&route)
r.JSON(200, res)
}
func listenerFor(router *Router, typ string) Listener {
switch typ {
case "http":
return router.HTTP
case "tcp":
return router.TCP
default:
return nil
}
}
func formatRoute(r *strowger.Route) *strowger.Route {
r.ID = fmt.Sprintf("%s/%s", r.Type, r.ID)
switch r.Type {
case "http":
httpRoute := r.HTTPRoute()
httpRoute.TLSKey = ""
httpRoute.Route = nil
conf, _ := json.Marshal(httpRoute)
jsonConf := json.RawMessage(conf)
r.Config = &jsonConf
}
return r
}
type sortedRoutes []*strowger.Route
func (p sortedRoutes) Len() int { return len(p) }
func (p sortedRoutes) Less(i, j int) bool { return p[i].CreatedAt.After(*p[j].CreatedAt) }
func (p sortedRoutes) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func getRoutes(req *http.Request, router *Router, r render.Render) {
routes, err := router.HTTP.List()
if err != nil {
log.Println(err)
r.JSON(500, struct{}{})
return
}
tcpRoutes, err := router.TCP.List()
if err != nil {
log.Println(err)
r.JSON(500, struct{}{})
return
}
routes = append(routes, tcpRoutes...)
if ref := req.URL.Query().Get("parent_ref"); ref != "" {
filtered := make([]*strowger.Route, 0)
for _, route := range routes {
if route.ParentRef == ref {
filtered = append(filtered, route)
}
}
routes = filtered
}
for i, route := range routes {
routes[i] = formatRoute(route)
}
sort.Sort(sortedRoutes(routes))
r.JSON(200, routes)
}
func getRoute(params martini.Params, router *Router, r render.Render) {
l := listenerFor(router, params["route_type"])
if l == nil {
r.JSON(404, struct{}{})
return
}
route, err := l.Get(params["route_id"])
if err == ErrNotFound {
r.JSON(404, struct{}{})
return
}
if err != nil {
log.Println(err)
r.JSON(500, struct{}{})
return
}
r.JSON(200, formatRoute(route))
}
func deleteRoute(params martini.Params, router *Router, r render.Render) {
l := listenerFor(router, params["route_type"])
if l == nil {
r.JSON(404, struct{}{})
return
}
err := l.RemoveRoute(params["route_id"])
if err == ErrNotFound {
r.JSON(404, struct{}{})
return
}
if err != nil {
log.Println(err)
r.JSON(500, struct{}{})
return
}
r.JSON(200, struct{}{})
}