Last active
June 2, 2020 14:45
-
-
Save crush-157/24ae584f3382fb80793673883ef692aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "http/server" | |
require "json" | |
module FnHelper | |
def self.socket_path | |
ENV.["FN_LISTENER"].try(&.[5..]) || "/tmp/iofs/lsnr.sock" | |
end | |
def self.handle(&block : JSON::Any -> String) | |
server = HTTP::Server.new do |context| | |
body = context.request.body.try(&.gets_to_end) | |
body = "{}" if body.try(&.empty?) || body.nil? | |
context.response.content_type = "application/json" | |
body.try { |b| context.response.print block.call JSON.parse b } | |
end | |
server.bind UNIXServer.new socket_path | |
server.listen | |
end | |
end | |
my_proc = ->(input : JSON::Any) do | |
name = input["name"]? || "world" | |
%({"message": "Hello #{name}"}) | |
end | |
FnHelper.handle &my_proc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment