webhooksd/src/webhooksd.cr

84 lines
1.7 KiB
Crystal

require "http/server"
require "option_parser"
require "./payload.cr"
VERSION = 0.1
port = 3000
storage = "webhooksd-data"
args = [] of String
OptionParser.parse do |parser|
parser.banner = "usage: webhooksd [option]"
parser.on "-v", "--version", "Show version" do
puts "version #{VERSION}"
exit
end
parser.on "-h", "--help", "Show help" do
puts parser
exit
end
parser.on "-p PORT", "--port=PORT", "Port to listen for connections. Default: 3000" do |p|
port = p.to_i
end
parser.on "-s STORAGE", "--storage=STORAGE", "Default: #{storage}" do |s|
storage = s
end
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
if args.size != 0
puts parser
exit 1
end
end
end
scriptfile = "on-push"
server = HTTP::Server.new do |context|
if context.request.method != "POST" || context.request.path != "/"
context.response.status = HTTP::Status::NOT_FOUND
else
payload = Payload.new(context.request)
path_project = storage + "/" + payload.project
path_scriptfile = path_project + "/" + scriptfile
status = false
if File.exists?(path_project) == false
STDERR.puts "ERROR: Project #{payload.project} not found"
status = false
else
if File.exists?(path_scriptfile) == false
STDERR.puts "ERROR: Scriptfile not found"
status = false
else
context.response.content_type = "text/plain"
status = Process.run command: "sh", args: [scriptfile], shell: true,
error: STDERR, output: STDOUT, chdir: path_project
end
end
if status
context.response.print "SUCCESS"
else
context.response.print "FAILURE"
end
end
end
address = server.bind_tcp port
server.listen