func createRoute(req *http.Request, route router.Route, router *Router, r render.Render) { l := listenerFor(router, route.Type) if l == nil { r.JSON(400, "Invalid route type") return } err := l.AddRoute(&route) if err != nil { if err == ErrConflict { rjson, err := json.Marshal(&route) if err != nil { log.Println(err) r.JSON(500, "unknown error") return } r.JSON(409, httphelper.JSONError{ Code: httphelper.ConflictErrorCode, Message: "Duplicate route", Detail: rjson, }) return } log.Println(err) r.JSON(500, "unknown error") return } r.JSON(200, route) }
func getRoutes(req *http.Request, rtr *Router, r render.Render) { routes, err := rtr.HTTP.List() if err != nil { log.Println(err) r.JSON(500, struct{}{}) return } tcpRoutes, err := rtr.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([]*router.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 createDatabase(db *postgres.DB, r render.Render) { username, password, database := random.Hex(16), random.Hex(16), random.Hex(16) if _, err := db.Exec(fmt.Sprintf(`CREATE USER "%s" WITH PASSWORD '%s'`, username, password)); err != nil { log.Println(err) r.JSON(500, struct{}{}) return } if _, err := db.Exec(fmt.Sprintf(`CREATE DATABASE "%s" WITH OWNER = "%s"`, database, username)); err != nil { db.Exec(fmt.Sprintf(`DROP USER "%s"`, username)) log.Println(err) r.JSON(500, struct{}{}) return } r.JSON(200, &resource{ ID: fmt.Sprintf("/databases/%s:%s", username, database), Env: map[string]string{ "FLYNN_POSTGRES": serviceName, "PGUSER": username, "PGPASSWORD": password, "PGDATABASE": database, }, }) }
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{}{}) }
func getRoute(params martini.Params, router *Router, r render.Render) { l := listenerFor(router, params["route_type"]) if l == nil { r.JSON(404, "not found") return } route, err := l.Get(params["id"]) if err == ErrNotFound { r.JSON(404, "not found") return } if err != nil { log.Println(err) r.JSON(500, "unknown error") return } r.JSON(200, route) }
func updateRoute(params martini.Params, route router.Route, router *Router, r render.Render) { route.Type = params["route_type"] route.ID = params["id"] l := listenerFor(router, route.Type) if l == nil { r.JSON(400, "Invalid route type") return } if err := l.UpdateRoute(&route); err != nil { if err == ErrNotFound { r.JSON(404, "not found") return } log.Println(err) r.JSON(500, "unknown error") return } r.JSON(200, route) }
func createRoute(req *http.Request, route router.Route, router *Router, r render.Render) { 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, "unknown error") return } r.JSON(200, route) }
func createOrReplaceRoute(req *http.Request, route router.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) }