Testing functions in crystal. Had to commit the template because it was outdated
This commit is contained in:
parent
0045b432b4
commit
f2be91d628
3
.gitignore
vendored
3
.gitignore
vendored
@ -161,4 +161,5 @@ cython_debug/
|
||||
|
||||
.venv
|
||||
.vscode/
|
||||
template/
|
||||
build
|
||||
.secrets
|
||||
|
@ -15,3 +15,7 @@ functions:
|
||||
lang: python3-flask
|
||||
handler: ./tapas
|
||||
image: ralsina/tapas:latest
|
||||
tapas2:
|
||||
lang: crystal-http
|
||||
handler: ./tapas2
|
||||
image: ralsina/tapas2:latest
|
||||
|
21
tapas2/handler.cr
Normal file
21
tapas2/handler.cr
Normal file
@ -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
|
2
tapas2/shard.yml
Normal file
2
tapas2/shard.yml
Normal file
@ -0,0 +1,2 @@
|
||||
name: crystal-http-template
|
||||
version: 0.1.0
|
37
template/crystal-http/Dockerfile
Normal file
37
template/crystal-http/Dockerfile
Normal file
@ -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"]
|
12
template/crystal-http/function/handler.cr
Normal file
12
template/crystal-http/function/handler.cr
Normal file
@ -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
|
2
template/crystal-http/function/shard.yml
Normal file
2
template/crystal-http/function/shard.yml
Normal file
@ -0,0 +1,2 @@
|
||||
name: crystal-http-template
|
||||
version: 0.1.0
|
41
template/crystal-http/main.cr
Normal file
41
template/crystal-http/main.cr
Normal file
@ -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
|
6
template/crystal-http/template.yml
Normal file
6
template/crystal-http/template.yml
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user