lib/tailog.rb in tailog-0.1.6 vs lib/tailog.rb in tailog-0.2.0

- old
+ new

@@ -1,17 +1,23 @@ require 'tailog/version' -require 'tailog/ext/file' require 'sinatra/base' require 'active_support/configurable' +require 'tailog/ext/file' +require 'securerandom' +require 'json' module Tailog include ActiveSupport::Configurable config_accessor :log_path do File.expand_path("log", Dir.pwd) end + config_accessor :server_uuid do + SecureRandom.uuid + end + class App < Sinatra::Base set :root, File.expand_path("../../app", __FILE__) set :public_folder do "#{root}/assets" end set :views do "#{root}/views" end @@ -20,13 +26,33 @@ Rack::Utils.escape_html(text) end end get '/' do - if params[:seek] - erb :ajax - else - erb :index + redirect to('/logs') + end + + get '/logs' do + erb :'logs/index' + end + + post '/logs' do + begin + file_path = File.join Tailog.log_path, params[:file] + file = File.open file_path + file_size = file.size + seek = params[:seek] && params[:seek][Tailog.server_uuid] || file_size + file.seek seek.to_i + content = erb :'logs/list', locals: { file: file } + file.close + rescue => error + content = erb :error, locals: { error: error } end + + { + server_uuid: Tailog.server_uuid, + file_size: file_size, + content: content + }.to_json end end end