From 3d94fc64c995a64d83b1dbb376aadcaf07a84598 Mon Sep 17 00:00:00 2001 From: Travis Reeder Date: Sun, 7 Aug 2016 14:58:05 -0400 Subject: [PATCH] Removed "name" from route, only need path. --- README.md | 7 +++---- api/datastore/bolt/bolt.go | 2 +- api/datastore/bolt_test.go | 15 +++++++-------- api/datastore/postgres/postgres.go | 20 +++++++++----------- api/ifaces/app.go | 16 ++++++++++++++++ api/models/route.go | 5 ----- api/server/routes_update.go | 3 ++- main.go | 8 +------- 8 files changed, 39 insertions(+), 37 deletions(-) create mode 100644 api/ifaces/app.go diff --git a/README.md b/README.md index cb7e6faae..b8573910c 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,6 @@ Now add routes to the app. First we'll add a route to the output of a docker con ```sh curl -H "Content-Type: application/json" -X POST -d '{ "route": { - "name": "hello", "path":"/hello", "image":"iron/hello" } @@ -46,7 +45,7 @@ curl http://localhost:8080/r/myapp/hello ### To pass in data to your function -Your function will get the body of the request as is, and the headers of the request will be passed in as env vars. +Your function will get the body of the request as is, and the headers of the request will be passed in as env vars. Try this: ```sh curl -H "Content-Type: application/json" -X POST -d '{ @@ -65,8 +64,8 @@ curl -H "Authorization: Bearer IRON_TOKEN" -H "Content-Type: application/json" - And you'll get an ironfunctions.com host for your app: ```sh -APP_NAME.USER_ID.ironfunctions.com/PATH -``` +myapp.USER_ID.ironfunctions.com/hello +``` ## Configuring your API diff --git a/api/datastore/bolt/bolt.go b/api/datastore/bolt/bolt.go index e2e5d2a7f..cab5f61fe 100644 --- a/api/datastore/bolt/bolt.go +++ b/api/datastore/bolt/bolt.go @@ -191,7 +191,7 @@ func (ds *BoltDatastore) StoreRoute(route *models.Route) (*models.Route, error) return err } - err = b.Put([]byte(route.Name), buf) + err = b.Put([]byte(route.Path), buf) if err != nil { return err } diff --git a/api/datastore/bolt_test.go b/api/datastore/bolt_test.go index 50d1830cc..3672b169d 100644 --- a/api/datastore/bolt_test.go +++ b/api/datastore/bolt_test.go @@ -28,7 +28,6 @@ func TestBolt(t *testing.T) { } testRoute := &models.Route{ - Name: "test", AppName: testApp.Name, Path: "/test", Image: "iron/hello", @@ -114,12 +113,12 @@ func TestBolt(t *testing.T) { t.Fatalf("Test GetRoute: expected error when using empty app name", err) } - route, err := ds.GetRoute(testApp.Name, testRoute.Name) + route, err := ds.GetRoute(testApp.Name, testRoute.Path) if err != nil { t.Fatalf("Test GetRoute: error: %s", err) } - if route.Name != testRoute.Name { - t.Fatalf("Test GetRoute: expected `route.Name` to be `%s` but it was `%s`", route.Name, testRoute.Name) + if route.Path != testRoute.Path { + t.Fatalf("Test GetRoute: expected `route.Name` to be `%s` but it was `%s`", route.Path, testRoute.Path) } // Testing list routes @@ -130,8 +129,8 @@ func TestBolt(t *testing.T) { if len(routes) == 0 { t.Fatal("Test GetRoutes: expected result count to be greater than 0") } - if routes[0].Name != testRoute.Name { - t.Fatalf("Test GetRoutes: expected `app.Name` to be `%s` but it was `%s`", testRoute.Name, routes[0].Name) + if routes[0].Path != testRoute.Path { + t.Fatalf("Test GetRoutes: expected `app.Name` to be `%s` but it was `%s`", testRoute.Path, routes[0].Path) } // Testing app delete @@ -145,12 +144,12 @@ func TestBolt(t *testing.T) { t.Fatalf("Test RemoveRoute: expected error when using empty route name", err) } - err = ds.RemoveRoute(testRoute.AppName, testRoute.Name) + err = ds.RemoveRoute(testRoute.AppName, testRoute.Path) if err != nil { t.Fatalf("Test RemoveApp: error: %s", err) } - route, err = ds.GetRoute(testRoute.AppName, testRoute.Name) + route, err = ds.GetRoute(testRoute.AppName, testRoute.Path) if err != nil { t.Fatalf("Test GetRoute: error: %s", err) } diff --git a/api/datastore/postgres/postgres.go b/api/datastore/postgres/postgres.go index c659c6194..9ac33cb0e 100644 --- a/api/datastore/postgres/postgres.go +++ b/api/datastore/postgres/postgres.go @@ -13,9 +13,8 @@ import ( const routesTableCreate = ` CREATE TABLE IF NOT EXISTS routes ( - name character varying(256) NOT NULL PRIMARY KEY, + app_name character varying(256) NOT NULL, path text NOT NULL, - app_name character varying(256) NOT NULL, image character varying(256) NOT NULL, headers text NOT NULL );` @@ -24,7 +23,7 @@ const appsTableCreate = `CREATE TABLE IF NOT EXISTS apps ( name character varying(256) NOT NULL PRIMARY KEY );` -const routeSelector = `SELECT name, path, app_name, image, headers FROM routes` +const routeSelector = `SELECT app_name, path, image, headers FROM routes` type rowScanner interface { Scan(dest ...interface{}) error @@ -159,16 +158,15 @@ func (ds *PostgresDatastore) StoreRoute(route *models.Route) (*models.Route, err _, err = ds.db.Exec(` INSERT INTO routes ( - name, app_name, path, image, + app_name, path, image, headers ) - VALUES ($1, $2, $3, $4, $5, $6, $7) + VALUES ($1, $2, $3, $4, $5) ON CONFLICT (name) DO UPDATE SET - path = $3, - image = $4, - headers = $5; + path = $1, + image = $2, + headers = $3; `, - route.Name, route.AppName, route.Path, route.Image, @@ -196,9 +194,9 @@ func (ds *PostgresDatastore) RemoveRoute(appName, routeName string) error { func scanRoute(scanner rowScanner, route *models.Route) error { var headerStr string err := scanner.Scan( - &route.Name, - &route.Path, + // &route.Name, &route.AppName, + &route.Path, &route.Image, &headerStr, ) diff --git a/api/ifaces/app.go b/api/ifaces/app.go new file mode 100644 index 000000000..d70c2b3f0 --- /dev/null +++ b/api/ifaces/app.go @@ -0,0 +1,16 @@ +package ifaces + +import "net/http" + +type App interface { + Name() string + Routes() Route + Validate() error +} + +type Route interface { + // AppName() string `json:"appname"` + Path() string + Image() string + Headers() http.Header +} diff --git a/api/models/route.go b/api/models/route.go index 72804eb37..627ac8b1f 100644 --- a/api/models/route.go +++ b/api/models/route.go @@ -21,7 +21,6 @@ var ( type Routes []*Route type Route struct { - Name string `json:"name"` AppName string `json:"appname"` Path string `json:"path"` Image string `json:"image"` @@ -39,10 +38,6 @@ var ( func (r *Route) Validate() error { var res []error - if r.Name == "" { - res = append(res, ErrRoutesValidationMissingName) - } - if r.Image == "" { res = append(res, ErrRoutesValidationMissingImage) } diff --git a/api/server/routes_update.go b/api/server/routes_update.go index 17d3b85c5..723bb8b48 100644 --- a/api/server/routes_update.go +++ b/api/server/routes_update.go @@ -27,7 +27,8 @@ func handleRouteUpdate(c *gin.Context) { } wroute.Route.AppName = c.Param("app") - wroute.Route.Name = c.Param("route") + log.Infoln("Route: ", c.Param("route")) + wroute.Route.Path = c.Param("route") if err := wroute.Validate(); err != nil { log.Error(err) diff --git a/main.go b/main.go index 086ff59a3..bb1e0d8c6 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,3 @@ -/* - -For keeping a minimum running, perhaps when doing a routing table update, if destination hosts are all - expired or about to expire we start more. - -*/ - package main import ( @@ -16,6 +9,7 @@ import ( "github.com/spf13/viper" ) +// See comments below for how to extend Functions func main() { c := &models.Config{}