From f2be91d62854fb2a18049afb31d4eb1ff57ec290 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Thu, 1 Jun 2023 09:44:33 -0300 Subject: [PATCH] Testing functions in crystal. Had to commit the template because it was outdated --- .gitignore | 3 +- functions.yml | 4 +++ tapas2/handler.cr | 21 ++++++++++++ tapas2/shard.yml | 2 ++ template/crystal-http/Dockerfile | 37 ++++++++++++++++++++ template/crystal-http/function/handler.cr | 12 +++++++ template/crystal-http/function/shard.yml | 2 ++ template/crystal-http/main.cr | 41 +++++++++++++++++++++++ template/crystal-http/template.yml | 6 ++++ 9 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 tapas2/handler.cr create mode 100644 tapas2/shard.yml create mode 100644 template/crystal-http/Dockerfile create mode 100644 template/crystal-http/function/handler.cr create mode 100644 template/crystal-http/function/shard.yml create mode 100644 template/crystal-http/main.cr create mode 100644 template/crystal-http/template.yml diff --git a/.gitignore b/.gitignore index 8d0aa3a..4e5a895 100644 --- a/.gitignore +++ b/.gitignore @@ -161,4 +161,5 @@ cython_debug/ .venv .vscode/ -template/ +build +.secrets diff --git a/functions.yml b/functions.yml index 18bb806..e4dbadb 100644 --- a/functions.yml +++ b/functions.yml @@ -15,3 +15,7 @@ functions: lang: python3-flask handler: ./tapas image: ralsina/tapas:latest + tapas2: + lang: crystal-http + handler: ./tapas2 + image: ralsina/tapas2:latest diff --git a/tapas2/handler.cr b/tapas2/handler.cr new file mode 100644 index 0000000..a41603e --- /dev/null +++ b/tapas2/handler.cr @@ -0,0 +1,21 @@ +require "http/request" +require "http/headers" + +class Handler + def run(request : HTTP::Request) + + if request.body.nil? + return { + body: "Foo", + status_code: 200, + headers: HTTP::Headers{"Content-Type" => "text/plain"}, + } + end + + { + body: "Hello, Crystal. You said: #{request.body.try(&.gets_to_end)}", + status_code: 200, + headers: HTTP::Headers{"Content-Type" => "text/plain"}, + } + end +end diff --git a/tapas2/shard.yml b/tapas2/shard.yml new file mode 100644 index 0000000..b65c156 --- /dev/null +++ b/tapas2/shard.yml @@ -0,0 +1,2 @@ +name: crystal-http-template +version: 0.1.0 diff --git a/template/crystal-http/Dockerfile b/template/crystal-http/Dockerfile new file mode 100644 index 0000000..e359108 --- /dev/null +++ b/template/crystal-http/Dockerfile @@ -0,0 +1,37 @@ +FROM ghcr.io/openfaas/of-watchdog:0.9.11 as watchdog + +FROM crystallang/crystal:1.8.2 + +COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog +RUN chmod +x /usr/bin/fwatchdog + +ARG ADDITIONAL_PACKAGE +RUN apt-get update \ + && apt-get install -qy --no-install-recommends ${ADDITIONAL_PACKAGE} + +WORKDIR /home/app + +COPY . . +COPY function/shard.yml shard.yml +RUN shards install +RUN crystal build main.cr -o handler --release + +# Create a non-root user +RUN addgroup --system app \ + && adduser --system --ingroup app app + +RUN chown app:app -R /home/app + +USER app + +WORKDIR /home/app + +ENV fprocess="./handler" +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/crystal-http/function/handler.cr b/template/crystal-http/function/handler.cr new file mode 100644 index 0000000..4ebe178 --- /dev/null +++ b/template/crystal-http/function/handler.cr @@ -0,0 +1,12 @@ +require "http/request" +require "http/headers" + +class Handler + def run(request : HTTP::Request) + { + body: "Hello, Crystal. You said: #{request.body.try(&.gets_to_end)}", + status_code: 200, + headers: HTTP::Headers{"Content-Type" => "text/plain"}, + } + end +end diff --git a/template/crystal-http/function/shard.yml b/template/crystal-http/function/shard.yml new file mode 100644 index 0000000..b65c156 --- /dev/null +++ b/template/crystal-http/function/shard.yml @@ -0,0 +1,2 @@ +name: crystal-http-template +version: 0.1.0 diff --git a/template/crystal-http/main.cr b/template/crystal-http/main.cr new file mode 100644 index 0000000..90acc5f --- /dev/null +++ b/template/crystal-http/main.cr @@ -0,0 +1,41 @@ +require "http/server" +require "./function/handler" + +server = HTTP::Server.new do |context| + response_triple : NamedTuple(body: String, headers: HTTP::Headers, status_code: Int32) | + NamedTuple(body: String, headers: HTTP::Headers) | + NamedTuple(body: String, status_code: Int32) | + NamedTuple(body: String) | + NamedTuple(headers: HTTP::Headers, status_code: Int32) | + NamedTuple(headers: HTTP::Headers) | + NamedTuple(status_code: Int32) + + handler = Handler.new + response_triple = handler.run(context.request) + + if response_triple.is_a?(NamedTuple(body: String, headers: HTTP::Headers, status_code: Int32) | + NamedTuple(body: String, status_code: Int32) | + NamedTuple(headers: HTTP::Headers, status_code: Int32) | + NamedTuple(status_code: Int32)) + context.response.status_code = response_triple[:status_code] + end + + if response_triple.is_a?(NamedTuple(body: String, headers: HTTP::Headers, status_code: Int32) | + NamedTuple(body: String, headers: HTTP::Headers) | + NamedTuple(headers: HTTP::Headers, status_code: Int32) | + NamedTuple(headers: HTTP::Headers)) + response_triple[:headers].each do |key, value| + context.response.headers[key] = value + end + end + + if response_triple.is_a?(NamedTuple(body: String, headers: HTTP::Headers, status_code: Int32) | + NamedTuple(body: String, headers: HTTP::Headers) | + NamedTuple(body: String, status_code: Int32) | + NamedTuple(body: String)) + context.response.print(response_triple[:body]) + end +end + +server.bind_tcp "0.0.0.0", 5000 +server.listen diff --git a/template/crystal-http/template.yml b/template/crystal-http/template.yml new file mode 100644 index 0000000..66d437b --- /dev/null +++ b/template/crystal-http/template.yml @@ -0,0 +1,6 @@ +language: crystal +fprocess: ./handler +welcome_message: | + You have created a new function which uses crystal 1.0.0. + To include third-party dependencies, use a vendoring tool like shards: + shards documentation: https://github.com/crystal-lang/shards