Added hello-go example to make example image faster.

This commit is contained in:
Travis Reeder
2016-10-12 00:08:22 -07:00
parent df3d5b48ce
commit e85c7560c3
17 changed files with 80 additions and 11 deletions

View File

@@ -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
```
*<b>Note</b>: 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:

1
examples/hello-go/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/hello

View File

@@ -0,0 +1,6 @@
FROM iron/go
WORKDIR /function
ADD hello .
ENTRYPOINT ["./hello"]

View File

@@ -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 .

View File

@@ -0,0 +1 @@
0.0.1

8
examples/hello-go/build.sh Executable file
View File

@@ -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 .

View File

@@ -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)
}

17
examples/hello-go/release.sh Executable file
View File

@@ -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

View File

@@ -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

View File

@@ -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")