From e85c7560c30a37c4a585fa990edc72c24fe3f54b Mon Sep 17 00:00:00 2001 From: Travis Reeder Date: Wed, 12 Oct 2016 00:08:22 -0700 Subject: [PATCH] Added hello-go example to make example image faster. --- README.md | 21 +++++++++++------ examples/hello-go/.gitignore | 1 + examples/hello-go/Dockerfile | 6 +++++ examples/hello-go/README.md | 4 ++++ examples/hello-go/VERSION | 1 + examples/hello-go/build.sh | 8 +++++++ examples/hello-go/hello.go | 26 +++++++++++++++++++++ examples/hello-go/release.sh | 17 ++++++++++++++ examples/{hello => hello-ruby}/.gitignore | 0 examples/{hello => hello-ruby}/Dockerfile | 0 examples/{hello => hello-ruby}/Gemfile | 0 examples/{hello => hello-ruby}/Gemfile.lock | 0 examples/{hello => hello-ruby}/README.md | 0 examples/{hello => hello-ruby}/VERSION | 0 examples/{hello => hello-ruby}/hello.rb | 0 examples/{hello => hello-ruby}/release.sh | 5 ++-- main.go | 2 +- 17 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 examples/hello-go/.gitignore create mode 100644 examples/hello-go/Dockerfile create mode 100644 examples/hello-go/README.md create mode 100644 examples/hello-go/VERSION create mode 100755 examples/hello-go/build.sh create mode 100644 examples/hello-go/hello.go create mode 100755 examples/hello-go/release.sh rename examples/{hello => hello-ruby}/.gitignore (100%) rename examples/{hello => hello-ruby}/Dockerfile (100%) rename examples/{hello => hello-ruby}/Gemfile (100%) rename examples/{hello => hello-ruby}/Gemfile.lock (100%) rename examples/{hello => hello-ruby}/README.md (100%) rename examples/{hello => hello-ruby}/VERSION (100%) rename examples/{hello => hello-ruby}/hello.rb (100%) rename examples/{hello => hello-ruby}/release.sh (56%) diff --git a/README.md b/README.md index 5625eafa6..69272a807 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,17 @@ ## Run functions +To get started quickly with IronFunctions, you can just fire up an `iron/functions` container: + ```sh docker run --rm --name functions --privileged -it -v $PWD/data:/app/data -p 8080:8080 iron/functions ``` -*Note: A list of configurations via env variables can be found [here](docs/api.md).* +** Note**: A list of configurations via env variables can be found [here](docs/api.md).* ## Using Functions -#### Create an Application +### Create an Application An application is essentially a grouping of functions, that put together, form an API. Here's how to create an app. @@ -22,7 +24,10 @@ curl -H "Content-Type: application/json" -X POST -d '{ Now that we have an app, we can map routes to functions. -#### Add a route to a Function +### Add a Route + +A route is a way to define a path in your application that maps to a function. In this example, we'll map +`/path` to a simple `Hello World!` image called `iron/hello`. ```sh curl -H "Content-Type: application/json" -X POST -d '{ @@ -33,15 +38,16 @@ curl -H "Content-Type: application/json" -X POST -d '{ }' http://localhost:8080/v1/apps/myapp/routes ``` -#### Calling your Function +### Calling your Function -Just hit the URL you got back from adding a route above: +Calling your function is as simple as requesting a URL. Each app has it's own namespace and each route mapped to the app. +The app `myapp` that we created above along with the `/hello` route we added would be called via the following URL. ``` curl http://localhost:8080/r/myapp/hello ``` -#### To pass in data to your function +### 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. Try this: @@ -51,8 +57,9 @@ curl -H "Content-Type: application/json" -X POST -d '{ }' http://localhost:8080/r/myapp/hello ``` +### Add an asynchronous route -**Adding a route with URL params** +### Adding a route with URL params You can create a route with dynamic URL parameters that will be available inside your function by prefixing path segments with a `:`, for example: diff --git a/examples/hello-go/.gitignore b/examples/hello-go/.gitignore new file mode 100644 index 000000000..e92569d01 --- /dev/null +++ b/examples/hello-go/.gitignore @@ -0,0 +1 @@ +/hello diff --git a/examples/hello-go/Dockerfile b/examples/hello-go/Dockerfile new file mode 100644 index 000000000..b1c5ce864 --- /dev/null +++ b/examples/hello-go/Dockerfile @@ -0,0 +1,6 @@ +FROM iron/go + +WORKDIR /function +ADD hello . + +ENTRYPOINT ["./hello"] diff --git a/examples/hello-go/README.md b/examples/hello-go/README.md new file mode 100644 index 000000000..8baccb13c --- /dev/null +++ b/examples/hello-go/README.md @@ -0,0 +1,4 @@ +set -ex + +docker run --rm -v "$PWD":/go/src/github.com/treeder/hello -w /go/src/github.com/treeder/hello iron/go:dev go build -o hello +docker build -t iron/hello . diff --git a/examples/hello-go/VERSION b/examples/hello-go/VERSION new file mode 100644 index 000000000..8a9ecc2ea --- /dev/null +++ b/examples/hello-go/VERSION @@ -0,0 +1 @@ +0.0.1 \ No newline at end of file diff --git a/examples/hello-go/build.sh b/examples/hello-go/build.sh new file mode 100755 index 000000000..3a09a6fd4 --- /dev/null +++ b/examples/hello-go/build.sh @@ -0,0 +1,8 @@ +set -ex + +USERNAME=iron +IMAGE=hello + +# build it +docker run --rm -v "$PWD":/go/src/github.com/iron/hello -w /go/src/github.com/iron/hello iron/go:dev go build -o hello +docker build -t $USERNAME/$IMAGE . diff --git a/examples/hello-go/hello.go b/examples/hello-go/hello.go new file mode 100644 index 000000000..15b70bee8 --- /dev/null +++ b/examples/hello-go/hello.go @@ -0,0 +1,26 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + "os" +) + +type Input struct { + Name string `json:"name"` +} + +func main() { + for _, e := range os.Environ() { + fmt.Println(e) + } + input := &Input{} + if err := json.NewDecoder(os.Stdin).Decode(input); err != nil { + log.Fatalln("Error! Bad input. ", err) + } + if input.Name == "" { + input.Name = "World" + } + fmt.Printf("Hello %v!\n", input.Name) +} diff --git a/examples/hello-go/release.sh b/examples/hello-go/release.sh new file mode 100755 index 000000000..1cef83f1c --- /dev/null +++ b/examples/hello-go/release.sh @@ -0,0 +1,17 @@ +set -ex + +USERNAME=iron +IMAGE=hello + +# build it +./build.sh +# test it +echo '{"name":"Johnny"}' | docker run --rm -i $USERNAME/hello +# tag it +docker run --rm -v "$PWD":/app treeder/bump patch +version=`cat VERSION` +echo "version: $version" +docker tag $USERNAME/$IMAGE:latest $USERNAME/$IMAGE:$version +# push it +docker push $USERNAME/$IMAGE:latest +docker push $USERNAME/$IMAGE:$version diff --git a/examples/hello/.gitignore b/examples/hello-ruby/.gitignore similarity index 100% rename from examples/hello/.gitignore rename to examples/hello-ruby/.gitignore diff --git a/examples/hello/Dockerfile b/examples/hello-ruby/Dockerfile similarity index 100% rename from examples/hello/Dockerfile rename to examples/hello-ruby/Dockerfile diff --git a/examples/hello/Gemfile b/examples/hello-ruby/Gemfile similarity index 100% rename from examples/hello/Gemfile rename to examples/hello-ruby/Gemfile diff --git a/examples/hello/Gemfile.lock b/examples/hello-ruby/Gemfile.lock similarity index 100% rename from examples/hello/Gemfile.lock rename to examples/hello-ruby/Gemfile.lock diff --git a/examples/hello/README.md b/examples/hello-ruby/README.md similarity index 100% rename from examples/hello/README.md rename to examples/hello-ruby/README.md diff --git a/examples/hello/VERSION b/examples/hello-ruby/VERSION similarity index 100% rename from examples/hello/VERSION rename to examples/hello-ruby/VERSION diff --git a/examples/hello/hello.rb b/examples/hello-ruby/hello.rb similarity index 100% rename from examples/hello/hello.rb rename to examples/hello-ruby/hello.rb diff --git a/examples/hello/release.sh b/examples/hello-ruby/release.sh similarity index 56% rename from examples/hello/release.sh rename to examples/hello-ruby/release.sh index 771b4380d..5a5b4415c 100755 --- a/examples/hello/release.sh +++ b/examples/hello-ruby/release.sh @@ -1,10 +1,9 @@ USERNAME=iron # build it -docker build -t $USERNAME/hello . +docker build -t $USERNAME/hello:ruby . # test it echo '{"name":"Johnny"}' | docker run --rm -i $USERNAME/hello # tag it docker run --rm -v "$PWD":/app treeder/bump patch -docker tag $USERNAME/hello:latest $USERNAME/hello:`cat VERSION` # push it -docker push $USERNAME/hello +docker push $USERNAME/hello:ruby diff --git a/main.go b/main.go index 856a8688c..d73934cb2 100644 --- a/main.go +++ b/main.go @@ -29,7 +29,7 @@ const ( func init() { cwd, err := os.Getwd() if err != nil { - log.WithError(err) + log.WithError(err).Fatalln("") } viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.SetDefault(envLogLevel, "info")