forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
90 lines (77 loc) · 2.12 KB
/
routes.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
package main
import (
"net/http"
"github.com/flynn/flynn/controller/schema"
"github.com/flynn/flynn/pkg/httphelper"
"github.com/flynn/flynn/pkg/postgres"
routerc "github.com/flynn/flynn/router/client"
"github.com/flynn/flynn/router/types"
"golang.org/x/net/context"
)
func createRoute(db *postgres.DB, rc routerc.Client, appID string, route *router.Route) error {
route.ParentRef = routeParentRef(appID)
if err := schema.Validate(route); err != nil {
return err
}
return rc.CreateRoute(route)
}
func (c *controllerAPI) CreateRoute(ctx context.Context, w http.ResponseWriter, req *http.Request) {
var route router.Route
if err := httphelper.DecodeJSON(req, &route); err != nil {
respondWithError(w, err)
return
}
if err := createRoute(c.appRepo.db, c.routerc, c.getApp(ctx).ID, &route); err != nil {
respondWithError(w, err)
return
}
httphelper.JSON(w, 200, &route)
}
func (c *controllerAPI) GetRoute(ctx context.Context, w http.ResponseWriter, req *http.Request) {
route, err := c.getRoute(ctx)
if err != nil {
respondWithError(w, err)
return
}
httphelper.JSON(w, 200, route)
}
func (c *controllerAPI) GetRouteList(ctx context.Context, w http.ResponseWriter, req *http.Request) {
routes, err := c.routerc.ListRoutes(routeParentRef(c.getApp(ctx).ID))
if err != nil {
respondWithError(w, err)
return
}
httphelper.JSON(w, 200, routes)
}
func (c *controllerAPI) UpdateRoute(ctx context.Context, w http.ResponseWriter, req *http.Request) {
var route *router.Route
if err := httphelper.DecodeJSON(req, &route); err != nil {
respondWithError(w, err)
return
}
err := c.routerc.UpdateRoute(route)
if err == routerc.ErrNotFound {
err = ErrNotFound
}
if err != nil {
respondWithError(w, err)
return
}
httphelper.JSON(w, 200, route)
}
func (c *controllerAPI) DeleteRoute(ctx context.Context, w http.ResponseWriter, req *http.Request) {
route, err := c.getRoute(ctx)
if err != nil {
respondWithError(w, err)
return
}
err = c.routerc.DeleteRoute(route.Type, route.ID)
if err == routerc.ErrNotFound {
err = ErrNotFound
}
if err != nil {
respondWithError(w, err)
return
}
w.WriteHeader(200)
}