2020-11-26 14:51:57 +01:00
|
|
|
require "http/server"
|
|
|
|
require "option_parser"
|
2020-11-26 16:15:04 +01:00
|
|
|
require "./payload.cr"
|
2020-11-26 14:51:57 +01:00
|
|
|
|
|
|
|
VERSION = 0.1
|
|
|
|
|
2020-11-28 16:54:53 +01:00
|
|
|
port = 3000
|
2020-11-27 02:39:27 +01:00
|
|
|
storage = "webhooksd-data"
|
2020-12-16 00:43:00 +01:00
|
|
|
scriptfile = "default.zsh"
|
2020-11-28 16:54:53 +01:00
|
|
|
jsonfile = "payload.json"
|
2020-11-27 02:39:27 +01:00
|
|
|
|
2020-11-26 16:31:50 +01:00
|
|
|
args = [] of String
|
|
|
|
|
2020-11-26 14:51:57 +01:00
|
|
|
OptionParser.parse do |parser|
|
2020-11-27 02:39:27 +01:00
|
|
|
parser.banner = "usage: webhooksd [option]"
|
2020-11-26 14:51:57 +01:00
|
|
|
parser.on "-v", "--version", "Show version" do
|
|
|
|
puts "version #{VERSION}"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2020-11-28 16:54:53 +01:00
|
|
|
parser.on "-h", "--help", "Show help" do
|
2020-11-26 14:51:57 +01:00
|
|
|
puts parser
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on "-p PORT", "--port=PORT", "Port to listen for connections. Default: 3000" do |p|
|
|
|
|
port = p.to_i
|
|
|
|
end
|
2020-11-26 16:31:50 +01:00
|
|
|
|
2020-11-27 02:39:27 +01:00
|
|
|
parser.on "-s STORAGE", "--storage=STORAGE", "Default: #{storage}" do |s|
|
|
|
|
storage = s
|
|
|
|
end
|
|
|
|
|
2020-12-16 00:43:00 +01:00
|
|
|
parser.on "-e SCRIPT", "--script=SCRIPT", "Default: #{scriptfile}" do |s|
|
|
|
|
scriptfile = s
|
|
|
|
end
|
|
|
|
|
2020-11-26 16:31:50 +01:00
|
|
|
parser.invalid_option do |flag|
|
|
|
|
STDERR.puts "ERROR: #{flag} is not a valid option."
|
|
|
|
STDERR.puts parser
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.unknown_args do |x|
|
|
|
|
args = x
|
|
|
|
|
2020-11-27 02:39:27 +01:00
|
|
|
if args.size != 0
|
2020-11-26 16:31:50 +01:00
|
|
|
puts parser
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-26 14:51:57 +01:00
|
|
|
server = HTTP::Server.new do |context|
|
2020-11-26 15:27:17 +01:00
|
|
|
if context.request.method != "POST" || context.request.path != "/"
|
|
|
|
context.response.status = HTTP::Status::NOT_FOUND
|
|
|
|
else
|
2020-11-26 16:15:04 +01:00
|
|
|
payload = Payload.new(context.request)
|
|
|
|
|
2020-11-27 02:39:27 +01:00
|
|
|
path_project = storage + "/" + payload.project
|
2020-12-16 00:43:00 +01:00
|
|
|
path_scriptfile = "../../../" + scriptfile
|
2020-11-27 16:59:17 +01:00
|
|
|
path_jsonfile = path_project + "/" + jsonfile
|
2020-11-27 02:39:27 +01:00
|
|
|
|
|
|
|
status = false
|
|
|
|
|
|
|
|
if File.exists?(path_project) == false
|
|
|
|
STDERR.puts "ERROR: Project #{payload.project} not found"
|
|
|
|
status = false
|
|
|
|
else
|
2020-12-16 00:43:00 +01:00
|
|
|
if File.exists?(scriptfile) == false
|
|
|
|
p scriptfile
|
2020-11-27 02:39:27 +01:00
|
|
|
STDERR.puts "ERROR: Scriptfile not found"
|
|
|
|
status = false
|
|
|
|
else
|
2020-11-27 16:59:17 +01:00
|
|
|
File.write(path_jsonfile, payload.content)
|
2020-12-16 00:43:00 +01:00
|
|
|
status = Process.run command: "zsh", args: [path_scriptfile], shell: true,
|
2020-11-27 02:39:27 +01:00
|
|
|
error: STDERR, output: STDOUT, chdir: path_project
|
|
|
|
end
|
|
|
|
end
|
2020-11-27 16:59:17 +01:00
|
|
|
|
|
|
|
context.response.content_type = "text/plain"
|
2020-11-27 02:39:27 +01:00
|
|
|
if status
|
2020-11-26 16:31:50 +01:00
|
|
|
context.response.print "SUCCESS"
|
|
|
|
else
|
|
|
|
context.response.print "FAILURE"
|
|
|
|
end
|
2020-11-26 15:27:17 +01:00
|
|
|
end
|
2020-11-26 14:51:57 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
address = server.bind_tcp port
|
|
|
|
server.listen
|