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 createRoute(app *ct.App, router routerc.Client, route router.Route, r ResponseHelper) { route.ParentRef = routeParentRef(app) if err := router.CreateRoute(&route); err != nil { r.Error(err) return } r.JSON(200, &route) }
func getRouteList(app *ct.App, router routerc.Client, r ResponseHelper) { routes, err := router.ListRoutes(routeParentRef(app)) if err != nil { r.Error(err) return } r.JSON(200, routes) }
func streamRouterEvents(rc routerc.Client, db *postgres.DB, doneCh chan struct{}) error { // wait for router to come up { events := make(chan *discoverd.Event) stream, err := discoverd.NewService("router-api").Watch(events) if err != nil { return err } for e := range events { if e.Kind == discoverd.EventKindUp { break } } stream.Close() } events := make(chan *router.StreamEvent) s, err := rc.StreamEvents(events) if err != nil { return err } go func() { for { e, ok := <-events if !ok { return } route := e.Route route.ID = postgres.CleanUUID(route.ID) var appID string if strings.HasPrefix(route.ParentRef, routeParentRefPrefix) { appID = strings.TrimPrefix(route.ParentRef, routeParentRefPrefix) } eventType := ct.EventTypeRoute if e.Event == "remove" { eventType = ct.EventTypeRouteDeletion } hash := md5.New() io.WriteString(hash, appID) io.WriteString(hash, string(eventType)) io.WriteString(hash, route.ID) io.WriteString(hash, route.CreatedAt.String()) io.WriteString(hash, route.UpdatedAt.String()) uniqueID := fmt.Sprintf("%x", hash.Sum(nil)) if err := createEvent(wrapDBExec(db.Exec), &ct.Event{ AppID: appID, ObjectID: route.ID, ObjectType: eventType, UniqueID: uniqueID, }, route); err != nil { log.Println(err) } } }() _, _ = <-doneCh return s.Close() }
func deleteRoute(route *router.Route, router routerc.Client, r ResponseHelper) { err := router.DeleteRoute(route.ID) if err == routerc.ErrNotFound { err = ErrNotFound } if err != nil { r.Error(err) return } r.WriteHeader(200) }
func getRouteMiddleware(app *ct.App, c martini.Context, params martini.Params, router routerc.Client, r ResponseHelper) { route, err := router.GetRoute(routeID(params)) if err == routerc.ErrNotFound || err == nil && route.ParentRef != routeParentRef(app) { err = ErrNotFound } if err != nil { r.Error(err) return } c.Map(route) }