Skip to content

Commit

Permalink
Initial version of Ruby template ported to of-watchdog
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (VMware) <[email protected]>
  • Loading branch information
alexellis committed Sep 28, 2018
0 parents commit 4b2cf0e
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## ruby-http

### Usage:

```
faas template pull
faas new --lang ruby-http homepage
```

#### Example:

Edit the `homepage/handler.rb` file to return some HTML:

```ruby
class Handler
def run(body, headers)
response_headers = {"content-type": "text/html"}
body = "<html>Hello world from the Ruby template</html>"

return body, response_headers
end
end
```

Add a gem to the `homepage/Gemfile` if you need additional dependencies.

Deploy:

```sh
faas-cli up -f homepage.yml
```

42 changes: 42 additions & 0 deletions template/ruby-http/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM ruby:2.4-alpine3.6

ARG ADDITIONAL_PACKAGE

# Alternatively use ADD https:// (which will not be cached by Docker builder)
RUN apk --no-cache add curl ${ADDITIONAL_PACKAGE} \
&& echo "Pulling watchdog binary from Github." \
&& curl -sSL https://github.com/openfaas-incubator/of-watchdog/releases/download/0.4.0/of-watchdog > /usr/bin/fwatchdog \
&& chmod +x /usr/bin/fwatchdog \
&& apk del curl

WORKDIR /home/app

COPY Gemfile .
COPY index.rb .
COPY function function

RUN bundle install \
&& mkdir -p /home/app/function

WORKDIR /home/app/function

RUN bundle install

RUN addgroup -S app \
&& adduser app -S -G app

RUN chown app:app -R /home/app

USER app

WORKDIR /home/app

ENV fprocess="ruby index.rb"
EXPOSE 8080

HEALTHCHECK --interval=2s CMD [ -e /tmp/.lock ] || exit 1

ENV upstream_url="http://127.0.0.1:5000"
ENV mode="http"

CMD ["fwatchdog"]
3 changes: 3 additions & 0 deletions template/ruby-http/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source 'https://rubygems.org'

gem "sinatra"
2 changes: 2 additions & 0 deletions template/ruby-http/function/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source 'https://rubygems.org'

8 changes: 8 additions & 0 deletions template/ruby-http/function/handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Handler
def run(body, headers)
response_headers = {"content-type": "text/plain"}
body = "Hello world from the Ruby template"

return body, response_headers
end
end
40 changes: 40 additions & 0 deletions template/ruby-http/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) Alex Ellis 2017. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.

require_relative 'function/handler'

require 'sinatra'

set :port, 5000
# set :bind, '0.0.0.0'

handler = Handler.new

get '/*' do
res, res_headers = handler.run request.body, request.env

headers = res_headers

return res
end

post '/*' do
res, res_headers = handler.run request.body, request.env
headers = res_headers
return res
end

put '/*' do
res, res_headers = handler.run request.body, request.env
headers = res_headers

return res
end

delete '/*' do
res, res_headers = handler.run request.body, request.env
headers = res_headers

return res
end

15 changes: 15 additions & 0 deletions template/ruby-http/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: ruby
fprocess: ruby index.rb
build_options:
- name: dev
packages:
- make
- automake
- gcc
- g++
- subversion
- python3-dev
- musl-dev
- libffi-dev
- libssh
- libssh-dev

0 comments on commit 4b2cf0e

Please sign in to comment.