crystal-http-template

A Crystal HTTP template for OpenFaaS

crystal-http

Usage

faas-cli template pull https://github.com/koffeinfrei/crystal-http-template
faas-cli new --lang crystal-http function_name

Example

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

require "http/request"
require "http/headers"

class Handler
  def run(request : HTTP::Request)
    {
      body:        "<p>Hello, Crystal. You said: #{request.body.try(&.gets_to_end)}</p>",
      status_code: 200,
      headers:     HTTP::Headers{"Content-Type" => "text/html"},
    }
  end
end

Add a shard to the function_name/shard.yml if you need additional dependencies.

Deploy

faas-cli up -f function_name.yml

Specification

The handler needs to return a NamedTuple with the keys body, headers, status_code. At least one of the keys must be present. The following is the type annotation for the return value:

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)

Credits

This template is inspired by ruby-http and crystal_openfaas

Repository

crystal-http-template

Owner
Statistic
  • 3
  • 0
  • 0
  • 0
  • 0
  • 2 months ago
  • October 13, 2019
License

MIT License

Links
Synced at

Thu, 21 Nov 2024 18:46:19 GMT

Languages