From 96668cc2dc0367e3d1a3ec94265ce889b62d8ada Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Fri, 19 Apr 2013 12:36:45 +0300 Subject: Add restful API --- lib/oxidized/api/domain.rb | 48 ++++++++++++++++++++++++++++++++++++++++++++++ lib/oxidized/api/rest.rb | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 lib/oxidized/api/domain.rb create mode 100644 lib/oxidized/api/rest.rb (limited to 'lib/oxidized/api') diff --git a/lib/oxidized/api/domain.rb b/lib/oxidized/api/domain.rb new file mode 100644 index 0000000..fa6a1c7 --- /dev/null +++ b/lib/oxidized/api/domain.rb @@ -0,0 +1,48 @@ +# this is not used, just added here if I want to revive it + +module Oxidized + require 'socket' + require 'json' + module API + class Domain + def initialize nodes, socket=CFG.api + puts 'here' + @nodes = nodes + File.unlink socket rescue Errno::ENOENT + @server = UNIXServer.new socket + end + def work + io = select [@server], nil, nil, Config::Sleep + process io.first.first.accept if io + end + def read + @socket.recv 1024 + end + def write data='' + begin + @socket.send JSON.dump(data), 0 + rescue Errno::EPIPE + end + end + def process socket + @socket = socket + cmd = read + cmd, data = cmd.split /\s+/, 2 + data = data.to_s.chomp + case cmd + when /next/i + @nodes.next data + write 'OK' + when /reload/i + @nodes.load if data.match /nodes/i + write 'OK' + when /list/i + write @nodes.map{|e|e.name} + when /node/i + write @nodes.show(data) + end + @socket.close + end + end + end +end diff --git a/lib/oxidized/api/rest.rb b/lib/oxidized/api/rest.rb new file mode 100644 index 0000000..ee06bc8 --- /dev/null +++ b/lib/oxidized/api/rest.rb @@ -0,0 +1,43 @@ +module Oxidized + require 'webrick' + require 'json' + module API + class Rest + def initialize nodes, listen + @nodes = nodes + addr, port = listen.to_s.split ':' + port, addr = addr, nil if not port + @web = WEBrick::HTTPServer.new :BindAddress=>addr, :Port=>port + maps + end + def work + req = select @web.listeners, nil, nil, Config::Sleep + while req + @web.run req.first.first.accept + req = select @web.listeners, nil, nil, 0 + end + end + def maps + @web.mount_proc '/nodes' do |req, res| + #script_name, #path_info + case req.path_info[1..-1] + # /nodes/reload - reloads list of nodes + when 'reload' + @nodes.load + res.body = JSON.dump 'OK' + # /nodes/next/node - moves node to head of queue + when /next\/(.*)/ + @nodes.next $1 + res.body = JSON.dump 'OK' + # /nodes/list - returns list of nodes + when 'list' + res.body = JSON.dump @nodes.list + # /nodes/show/node - returns data about node + when /show\/(.*)/ + res.body = JSON.dump @nodes.show $1 + end + end + end + end + end +end -- cgit v1.2.1