diff options
author | Saku Ytti <saku@ytti.fi> | 2014-02-23 19:11:29 +0200 |
---|---|---|
committer | Saku Ytti <saku@ytti.fi> | 2014-02-23 19:11:29 +0200 |
commit | 8880188001da23a5f0960e3970c4eb9bbd448306 (patch) | |
tree | 41a1f79450ff1683a2d5aa1f024a564e84230e25 /lib/oxidized/api/web/webapp.rb | |
parent | 06e5f68db6cfcbd80295874db1f00a25e8ba1229 (diff) |
Migrate to sinatra/puma from webrick
As I can't do IO#select on sinatra/puma to run it when I have time, I
have to run it on separate thread.
This means Nodes container needs to be thread safe, it now has ghetto
mutex locking, but I probably need to be be more focused what are the
external methods that can be called and wrap those in @mutex.synchronize
Provide also HTML UI not just JSON for ghetto UI to people who don't want to
integrate
Diffstat (limited to 'lib/oxidized/api/web/webapp.rb')
-rw-r--r-- | lib/oxidized/api/web/webapp.rb | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/oxidized/api/web/webapp.rb b/lib/oxidized/api/web/webapp.rb new file mode 100644 index 0000000..13bd0ab --- /dev/null +++ b/lib/oxidized/api/web/webapp.rb @@ -0,0 +1,98 @@ +require 'sinatra/base' +require 'sinatra/json' +require 'haml' +require 'pp' +module Oxidized + module API + class WebApp < Sinatra::Base + + get '/nodes.?:format?' do + @data = nodes.list.map do |node| + node[:status] = 'never' + node[:time] = 'never' + node[:group] = 'default' unless node[:group] + if node[:last] + node[:status] = node[:last][:status] + node[:time] = node[:last][:end] + end + node + end + out :nodes + end + + get '/reload.?:format?' do + nodes.load + @data = 'reloaded list of nodes' + out + end + + + get '/node/fetch/:node' do + node, @json = route_parse :node + @data = nodes.fetch node, nil + out :text + end + + get '/node/fetch/:group/:node' do + node, @json = route_parse :node + @data = nodes.fetch node, params[:group] + out :text + end + + + get '/node/next/:node' do + node, @json = route_parse :node + nodes.next node + redirect '/nodes' unless @json + @data = 'ok' + out + end + + # use this to attach author/email/message to commit + put '/node/next/:node' do + node, @json = route_parse :node + opt = JSON.load request.body.read + nodes.next node, opt + redirect '/nodes' unless @json + @data = 'ok' + out + end + + get '/node/show/:node' do + node, @json = route_parse :node + @data = nodes.show node + out :node + end + + #get '/node/:node' do + # @data = nodes.show params[:node] + # out + #end + + def out template=:default + if @json or params[:format] == 'json' + json @data + elsif template == :text + content_type :text + @data + else + haml template + end + end + + def nodes + settings.nodes + end + + def route_parse param + json = false + e = params[param].split '.' + if e.last == 'json' + e.pop + json = true + end + [e.join('.'), json] + end + end + end +end |