diff --git a/README.md b/README.md new file mode 100644 index 0000000..19057c8 --- /dev/null +++ b/README.md @@ -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 = "Hello world from the Ruby template" + + 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 +``` + diff --git a/template/ruby-http/Dockerfile b/template/ruby-http/Dockerfile new file mode 100644 index 0000000..a817bef --- /dev/null +++ b/template/ruby-http/Dockerfile @@ -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"] diff --git a/template/ruby-http/Gemfile b/template/ruby-http/Gemfile new file mode 100644 index 0000000..cca8293 --- /dev/null +++ b/template/ruby-http/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem "sinatra" diff --git a/template/ruby-http/function/Gemfile b/template/ruby-http/function/Gemfile new file mode 100644 index 0000000..c9721cb --- /dev/null +++ b/template/ruby-http/function/Gemfile @@ -0,0 +1,2 @@ +source 'https://rubygems.org' + diff --git a/template/ruby-http/function/handler.rb b/template/ruby-http/function/handler.rb new file mode 100644 index 0000000..d0d2c99 --- /dev/null +++ b/template/ruby-http/function/handler.rb @@ -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 diff --git a/template/ruby-http/index.rb b/template/ruby-http/index.rb new file mode 100644 index 0000000..92c2a3b --- /dev/null +++ b/template/ruby-http/index.rb @@ -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 + diff --git a/template/ruby-http/template.yml b/template/ruby-http/template.yml new file mode 100644 index 0000000..976d84b --- /dev/null +++ b/template/ruby-http/template.yml @@ -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