mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Added hello-go example to make example image faster.
This commit is contained in:
21
README.md
21
README.md
@@ -2,15 +2,17 @@
|
|||||||
|
|
||||||
## Run functions
|
## Run functions
|
||||||
|
|
||||||
|
To get started quickly with IronFunctions, you can just fire up an `iron/functions` container:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
docker run --rm --name functions --privileged -it -v $PWD/data:/app/data -p 8080:8080 iron/functions
|
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
|
## 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.
|
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.
|
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
|
```sh
|
||||||
curl -H "Content-Type: application/json" -X POST -d '{
|
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
|
}' 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
|
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:
|
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
|
}' 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:
|
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
1
examples/hello-go/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/hello
|
||||||
6
examples/hello-go/Dockerfile
Normal file
6
examples/hello-go/Dockerfile
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
FROM iron/go
|
||||||
|
|
||||||
|
WORKDIR /function
|
||||||
|
ADD hello .
|
||||||
|
|
||||||
|
ENTRYPOINT ["./hello"]
|
||||||
4
examples/hello-go/README.md
Normal file
4
examples/hello-go/README.md
Normal 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 .
|
||||||
1
examples/hello-go/VERSION
Normal file
1
examples/hello-go/VERSION
Normal file
@@ -0,0 +1 @@
|
|||||||
|
0.0.1
|
||||||
8
examples/hello-go/build.sh
Executable file
8
examples/hello-go/build.sh
Executable 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 .
|
||||||
26
examples/hello-go/hello.go
Normal file
26
examples/hello-go/hello.go
Normal 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
17
examples/hello-go/release.sh
Executable 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
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
USERNAME=iron
|
USERNAME=iron
|
||||||
# build it
|
# build it
|
||||||
docker build -t $USERNAME/hello .
|
docker build -t $USERNAME/hello:ruby .
|
||||||
# test it
|
# test it
|
||||||
echo '{"name":"Johnny"}' | docker run --rm -i $USERNAME/hello
|
echo '{"name":"Johnny"}' | docker run --rm -i $USERNAME/hello
|
||||||
# tag it
|
# tag it
|
||||||
docker run --rm -v "$PWD":/app treeder/bump patch
|
docker run --rm -v "$PWD":/app treeder/bump patch
|
||||||
docker tag $USERNAME/hello:latest $USERNAME/hello:`cat VERSION`
|
|
||||||
# push it
|
# push it
|
||||||
docker push $USERNAME/hello
|
docker push $USERNAME/hello:ruby
|
||||||
2
main.go
2
main.go
@@ -29,7 +29,7 @@ const (
|
|||||||
func init() {
|
func init() {
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err)
|
log.WithError(err).Fatalln("")
|
||||||
}
|
}
|
||||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
viper.SetDefault(envLogLevel, "info")
|
viper.SetDefault(envLogLevel, "info")
|
||||||
|
|||||||
Reference in New Issue
Block a user