Skeleton of basic functionality, all as TODOs

This commit is contained in:
2024-06-28 14:09:58 -03:00
parent 11f1066619
commit 238d045691
9 changed files with 224 additions and 2 deletions

View File

@ -0,0 +1,39 @@
FROM --platform=${TARGETPLATFORM:-linux/amd64} ghcr.io/openfaas/of-watchdog:0.9.10 as watchdog
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine as build
ARG ADDITIONAL_PACKAGE
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN apk update && apk upgrade && apk add crystal shards openssl-dev ${ADDITIONAL_PACKAGE} && apk cache clean
WORKDIR /home/app
COPY function/shard.yml shard.yml
RUN shards install
COPY . .
RUN crystal build main.cr -o handler --error-trace -p && rm -rf /root/.cache
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine as ship
ARG ADDITIONAL_PACKAGE
RUN apk update && apk upgrade && apk add openssl pcre2 libgcc gc libevent ${ADDITIONAL_PACKAGE} && apk cache clean
# Add non root user
# Add non root user
RUN addgroup -S app && adduser app -S -G app
WORKDIR /home/app
USER app
COPY --from=build /home/app/function/ .
COPY --from=build /home/app/handler .
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
ENV fprocess="./handler"
ENV exec_timeout=30
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"]

View 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

View File

@ -0,0 +1,2 @@
name: crystal-http-template
version: 0.1.0

View 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

View 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