mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Added Ruby support to fnctl init. (#286)
This commit is contained in:
2
examples/hello-go/.gitignore
vendored
2
examples/hello-go/.gitignore
vendored
@@ -1,2 +0,0 @@
|
||||
/func
|
||||
vendor
|
||||
@@ -1,5 +0,0 @@
|
||||
FROM iron/go
|
||||
|
||||
ADD func .
|
||||
|
||||
ENTRYPOINT ["./func"]
|
||||
@@ -1,77 +0,0 @@
|
||||
# Hello Function Image (Go Language)
|
||||
|
||||
This images compares the payload info with the header.
|
||||
|
||||
## Requirements
|
||||
|
||||
- IronFunctions API
|
||||
|
||||
## Development
|
||||
|
||||
### Building image locally
|
||||
|
||||
```
|
||||
# SET BELOW TO YOUR DOCKER HUB USERNAME
|
||||
USERNAME=YOUR_DOCKER_HUB_USERNAME
|
||||
|
||||
# build it
|
||||
./build.sh
|
||||
```
|
||||
|
||||
### Publishing to DockerHub
|
||||
|
||||
```
|
||||
# tagging
|
||||
docker run --rm -v "$PWD":/app treeder/bump patch
|
||||
docker tag $USERNAME/func-hello-go:latest $USERNAME/func-hello-go:`cat VERSION`
|
||||
|
||||
# pushing to docker hub
|
||||
docker push $USERNAME/func-hello-go
|
||||
```
|
||||
|
||||
### Testing image
|
||||
|
||||
```
|
||||
./test.sh
|
||||
```
|
||||
|
||||
## Running it on IronFunctions
|
||||
|
||||
### Let's define some environment variables
|
||||
|
||||
```
|
||||
# Set your Function server address
|
||||
# Eg. 127.0.0.1:8080
|
||||
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
|
||||
```
|
||||
|
||||
### Running with IronFunctions
|
||||
|
||||
With this command we are going to create an application with name `hello`.
|
||||
|
||||
```
|
||||
curl -X POST --data '{
|
||||
"app": {
|
||||
"name": "hello",
|
||||
}
|
||||
}' http://$FUNCAPI/v1/apps
|
||||
```
|
||||
|
||||
Now, we can create our route
|
||||
|
||||
```
|
||||
curl -X POST --data '{
|
||||
"route": {
|
||||
"image": "'$USERNAME'/func-hello-go",
|
||||
"path": "/hello",
|
||||
}
|
||||
}' http://$FUNCAPI/v1/apps/hello/routes
|
||||
```
|
||||
|
||||
#### Testing function
|
||||
|
||||
Now that we created our IronFunction route, let's test our new route
|
||||
|
||||
```
|
||||
curl -X POST --data '{"name": "Johnny"}' http://$FUNCAPI/r/hello/hello
|
||||
```
|
||||
@@ -1 +0,0 @@
|
||||
0.0.1
|
||||
@@ -1,8 +0,0 @@
|
||||
#!/bin/bash
|
||||
set -ex
|
||||
|
||||
FUNCPKG=$(pwd | sed "s|$GOPATH/src/||")
|
||||
|
||||
# build image
|
||||
docker run --rm -v "$PWD":/go/src/$FUNCPKG -w /go/src/$FUNCPKG iron/go:dev go build -o func
|
||||
docker build -t iron/func-hello-go .
|
||||
@@ -1,3 +0,0 @@
|
||||
image: iron/func-hello-gp
|
||||
build:
|
||||
- ./build.sh
|
||||
@@ -1,22 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Input struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
input := &Input{}
|
||||
if err := json.NewDecoder(os.Stdin).Decode(input); err != nil {
|
||||
// log.Println("Bad payload or no payload. Ignoring.", err)
|
||||
}
|
||||
if input.Name == "" {
|
||||
input.Name = "World"
|
||||
}
|
||||
fmt.Printf("Hello %v!\n", input.Name)
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
HOST="${1:-localhost:8080}"
|
||||
REQ="${2:-1}"
|
||||
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"app": { "name":"myapp" }
|
||||
}' http://$HOST/v1/apps
|
||||
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"route": {
|
||||
"type": "async",
|
||||
"path":"/hello-async",
|
||||
"image":"iron/hello"
|
||||
}
|
||||
}' http://$HOST/v1/apps/myapp/routes
|
||||
|
||||
for i in `seq 1 $REQ`;
|
||||
do
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"name":"Johnny"
|
||||
}' http://$HOST/r/myapp/hello-async
|
||||
done;
|
||||
@@ -1,23 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
HOST="${1:-localhost:8080}"
|
||||
REQ="${2:-1}"
|
||||
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"app": { "name":"myapp" }
|
||||
}' http://$HOST/v1/apps
|
||||
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"route": {
|
||||
"type": "sync",
|
||||
"path":"/hello-sync",
|
||||
"image":"iron/hello"
|
||||
}
|
||||
}' http://$HOST/v1/apps/myapp/routes
|
||||
|
||||
for i in `seq 1 $REQ`;
|
||||
do
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"name":"Johnny"
|
||||
}' http://$HOST/r/myapp/hello-sync
|
||||
done;
|
||||
@@ -1,9 +0,0 @@
|
||||
#!/bin/bash
|
||||
set -x
|
||||
|
||||
./build.sh
|
||||
|
||||
PAYLOAD='{"name":"Johnny"}'
|
||||
|
||||
# test it
|
||||
echo $PAYLOAD | docker run --rm -i iron/func-hello-go
|
||||
2
examples/hello-ruby/.gitignore
vendored
2
examples/hello-ruby/.gitignore
vendored
@@ -1,2 +0,0 @@
|
||||
bundle/
|
||||
.bundle/
|
||||
@@ -1,9 +0,0 @@
|
||||
FROM iron/ruby:dev
|
||||
|
||||
WORKDIR /function
|
||||
ADD Gemfile* /function/
|
||||
RUN bundle install
|
||||
|
||||
ADD . /function/
|
||||
|
||||
ENTRYPOINT ["ruby", "function.rb"]
|
||||
@@ -1,77 +0,0 @@
|
||||
# Hello Function Image (Ruby)
|
||||
|
||||
This images compares the payload info with the header.
|
||||
|
||||
## Requirements
|
||||
|
||||
- IronFunctions API
|
||||
|
||||
## Development
|
||||
|
||||
### Building image locally
|
||||
|
||||
```
|
||||
# SET BELOW TO YOUR DOCKER HUB USERNAME
|
||||
USERNAME=YOUR_DOCKER_HUB_USERNAME
|
||||
|
||||
# build it
|
||||
./build.sh
|
||||
```
|
||||
|
||||
### Publishing to DockerHub
|
||||
|
||||
```
|
||||
# tagging
|
||||
docker run --rm -v "$PWD":/app treeder/bump patch
|
||||
docker tag $USERNAME/func-hello-ruby:latest $USERNAME/func-hello-ruby:`cat VERSION`
|
||||
|
||||
# pushing to docker hub
|
||||
docker push $USERNAME/func-hello-ruby
|
||||
```
|
||||
|
||||
### Testing image
|
||||
|
||||
```
|
||||
./test.sh
|
||||
```
|
||||
|
||||
## Running it on IronFunctions
|
||||
|
||||
### Let's define some environment variables
|
||||
|
||||
```
|
||||
# Set your Function server address
|
||||
# Eg. 127.0.0.1:8080
|
||||
FUNCAPI=YOUR_FUNCTIONS_ADDRESS
|
||||
```
|
||||
|
||||
### Running with IronFunctions
|
||||
|
||||
With this command we are going to create an application with name `hello`.
|
||||
|
||||
```
|
||||
curl -X POST --data '{
|
||||
"app": {
|
||||
"name": "hello",
|
||||
}
|
||||
}' http://$FUNCAPI/v1/apps
|
||||
```
|
||||
|
||||
Now, we can create our route
|
||||
|
||||
```
|
||||
curl -X POST --data '{
|
||||
"route": {
|
||||
"image": "'$USERNAME'/func-hello-ruby",
|
||||
"path": "/hello",
|
||||
}
|
||||
}' http://$FUNCAPI/v1/apps/hello/routes
|
||||
```
|
||||
|
||||
#### Testing function
|
||||
|
||||
Now that we created our IronFunction route, let's test our new route
|
||||
|
||||
```
|
||||
curl -X POST --data '{"name": "Johnny"}' http://$FUNCAPI/r/hello/hello
|
||||
```
|
||||
@@ -1 +0,0 @@
|
||||
0.0.1
|
||||
@@ -1,4 +0,0 @@
|
||||
#!/bin/bash
|
||||
set -ex
|
||||
|
||||
docker build -t iron/func-hello-ruby .
|
||||
@@ -1,3 +0,0 @@
|
||||
image: iron/func-hello-ruby
|
||||
build:
|
||||
- ./build.sh
|
||||
@@ -1,23 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
HOST="${1:-localhost:8080}"
|
||||
REQ="${2:-1}"
|
||||
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"app": { "name":"myapp" }
|
||||
}' http://$HOST/v1/apps
|
||||
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"route": {
|
||||
"type": "async",
|
||||
"path":"/hello-async",
|
||||
"image":"iron/hello:ruby"
|
||||
}
|
||||
}' http://$HOST/v1/apps/myapp/routes
|
||||
|
||||
for i in `seq 1 $REQ`;
|
||||
do
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"name":"Johnny"
|
||||
}' http://$HOST/r/myapp/hello-async
|
||||
done;
|
||||
@@ -1,23 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
HOST="${1:-localhost:8080}"
|
||||
REQ="${2:-1}"
|
||||
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"app": { "name":"myapp" }
|
||||
}' http://$HOST/v1/apps
|
||||
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"route": {
|
||||
"type": "sync",
|
||||
"path":"/hello-sync",
|
||||
"image":"iron/hello:ruby
|
||||
}
|
||||
}' http://$HOST/v1/apps/myapp/routes
|
||||
|
||||
for i in `seq 1 $REQ`;
|
||||
do
|
||||
curl -H "Content-Type: application/json" -X POST -d '{
|
||||
"name":"Johnny"
|
||||
}' http://$HOST/r/myapp/hello-sync
|
||||
done;
|
||||
@@ -1,9 +0,0 @@
|
||||
#!/bin/bash
|
||||
set -x
|
||||
|
||||
./build.sh
|
||||
|
||||
PAYLOAD='{"name":"Johnny"}'
|
||||
|
||||
# test it
|
||||
echo $PAYLOAD | docker run --rm -i -e TEST=1 iron/func-hello-ruby
|
||||
@@ -1,4 +0,0 @@
|
||||
FROM iron/go
|
||||
WORKDIR /function
|
||||
ADD . /function/
|
||||
ENTRYPOINT ["./func"]
|
||||
@@ -20,3 +20,8 @@ Now you can call your function on IronFunctions:
|
||||
```sh
|
||||
curl -H "Content-Type: application/json" -X POST -d @hello.payload.json http://localhost:8080/r/myapp/hello
|
||||
```
|
||||
|
||||
## Dependencies
|
||||
|
||||
Be sure you're dependencies are in the `vendor/` directory and that's it.
|
||||
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
name: treeder/hello4
|
||||
version: 0.0.1
|
||||
runtime: go
|
||||
entrypoint: ./func
|
||||
@@ -16,3 +16,26 @@ fnctl routes create myapp /hello
|
||||
```
|
||||
|
||||
Now surf to: http://localhost:8080/r/myapp/hello
|
||||
|
||||
## Dependencies
|
||||
|
||||
Create a [package.json](https://docs.npmjs.com/getting-started/using-a-package.json) file in your functions directory.
|
||||
|
||||
Run:
|
||||
|
||||
```sh
|
||||
docker run --rm -v "$PWD":/function -w /function iron/node:dev npm install
|
||||
```
|
||||
|
||||
Then everything should work.
|
||||
|
||||
For example, using the `package.json` file in this directory which includes the [request](https://www.npmjs.com/package/request) package, you can add this to func.js and it will work:
|
||||
|
||||
```js
|
||||
var request = require('request');
|
||||
request('http://www.google.com', function (error, response, body) {
|
||||
if (!error && response.statusCode == 200) {
|
||||
console.log(body) // Show the HTML for the Google homepage.
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
name: treeder/node4
|
||||
version: 0.0.2
|
||||
runtime: node
|
||||
entrypoint: node hello.js
|
||||
@@ -1,4 +1,7 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"name": "my-awesome-func",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"request": "^2.78.0"
|
||||
}
|
||||
}
|
||||
5
examples/hello/ruby/.gitignore
vendored
Normal file
5
examples/hello/ruby/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
bundle/
|
||||
.bundle/
|
||||
func.yaml
|
||||
Dockerfile
|
||||
Gemfile.lock
|
||||
@@ -1,3 +1,4 @@
|
||||
source 'https://rubygems.org'
|
||||
|
||||
gem 'httparty', '~> 0.14.0'
|
||||
gem 'json', '> 1.8.2'
|
||||
44
examples/hello/ruby/README.md
Normal file
44
examples/hello/ruby/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
## Quick Example for a Ruby Function (4 minutes)
|
||||
|
||||
This example will show you how to test and deploy a Ruby function to IronFunctions.
|
||||
|
||||
```sh
|
||||
# create your func.yaml file
|
||||
fnctl init <YOUR_DOCKERHUB_USERNAME>/hello
|
||||
# install dependencies, we need the json gem to run this
|
||||
docker run --rm -v "$PWD":/worker -w /worker iron/ruby:dev bundle install --standalone --clean
|
||||
# build the function
|
||||
fnctl build
|
||||
# test it
|
||||
cat hello.payload.json | fnctl run
|
||||
# push it to Docker Hub for use with IronFunctions
|
||||
fnctl push
|
||||
# Create a route to this function on IronFunctions
|
||||
fnctl routes create myapp /hello
|
||||
```
|
||||
|
||||
Now surf to: http://localhost:8080/r/myapp/hello
|
||||
|
||||
## Dependencies
|
||||
|
||||
Create a [Gemfile](http://bundler.io/gemfile.html) file in your function directory, then run:
|
||||
|
||||
```sh
|
||||
docker run --rm -v "$PWD":/worker -w /worker iron/ruby:dev bundle install --standalone --clean
|
||||
```
|
||||
|
||||
Ruby doesn't pick up the gems automatically, so you'll have to add this to the top of your `func.rb` file:
|
||||
|
||||
```ruby
|
||||
require_relative 'bundle/bundler/setup'
|
||||
```
|
||||
|
||||
Open `func.rb` to see it in action.
|
||||
|
||||
To update dependencies:
|
||||
|
||||
```sh
|
||||
docker run --rm -v "$PWD":/worker -w /worker iron/ruby:dev bundle update
|
||||
# then install again to vendor them
|
||||
docker run --rm -v "$PWD":/worker -w /worker iron/ruby:dev bundle update
|
||||
```
|
||||
@@ -1,3 +1,4 @@
|
||||
require_relative 'bundle/bundler/setup'
|
||||
require 'json'
|
||||
|
||||
name = "World"
|
||||
@@ -8,4 +9,4 @@ if payload != ""
|
||||
name = payload['name']
|
||||
end
|
||||
|
||||
puts "Hello #{name}!"
|
||||
puts "Hello #{name} from Ruby!"
|
||||
3
examples/hello/ruby/hello.payload.json
Normal file
3
examples/hello/ruby/hello.payload.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "Johnny"
|
||||
}
|
||||
Reference in New Issue
Block a user