<commit_before>require 'sensu/base'
require 'thin'
require 'sinatra/async'
require 'em-http-request'
require 'slim'
require 'sass'
require 'uri'
require 'sprockets'
require 'yui/compressor'
require 'handlebars_assets'

module Sensu
  class Dashboard < Sinatra::Base
    register Sinatra::Async

    configure do
      set :assets, (Sprockets::Environment.new { |env|
          env.append_path(settings.root + "/assets/javascripts")
          env.append_path(settings.root + "/assets/stylesheets")
          env.append_path HandlebarsAssets.path
          if ENV['RACK_ENV'] == 'production'
            env.js_compressor  = YUI::JavaScriptCompressor.new
            env.css_compressor = YUI::CssCompressor.new
          end
        })
    end

    class << self
      def run(options={})
        EM::run do
          setup(options)
          start
          trap_signals
        end
      end

      def invalid_settings(reason, details={})
        $logger.fatal('invalid settings', {
          :reason => reason
        }.merge(details))
        $logger.fatal('SENSU DASHBOARD NOT RUNNING!')
        exit 2
      end

      def setup(options={})
        base = Sensu::Base.new(options)
        $logger = base.logger
        settings = base.settings
        $dashboard_settings = settings[:dashboard] || {
          :port => 8080,
          :user => 'admin',
          :password => '<PASSWORD>'
        }
        $api_settings = settings[:api] || {
          :host => 'localhost',
          :port => 4567,
          :user => nil,
          :password => <PASSWORD>
        }
        unless $dashboard_settings.is_a?(Hash)
          invalid_settings('misconfigured dashboard configuration')
        end
        unless $dashboard_settings[:port].is_a?(Integer)
          invalid_settings('dashboard port must be an integer', {
            :settings => $dashboard_settings
          })
        end
        unless $dashboard_settings[:user].is_a?(String) && $dashboard_settings[:password].is_a?(String)
          invalid_settings('dashboard user and password must be strings', {
            :settings => $dashboard_settings
          })
        end
        base.setup_process
        $api_url = 'http://' + $api_settings[:host] + ':' + $api_settings[:port].to_s
        $api_options = {:head => {}}
        if $api_settings[:user] && $api_settings[:password]
          $api_options.merge!(:head => {:authorization => [$api_settings[:user], $api_settings[:password]]})
        end
      end

      def start
        Thin::Logging.silent = true
        Thin::Server.start(self, $dashboard_settings[:port])
      end

      def stop
        $logger.warn('stopping')
        EM::stop_event_loop
      end

      def trap_signals
        %w[INT TERM].each do |signal|
          Signal.trap(signal) do
            $logger.warn('received signal', {
              :signal => signal
            })
            stop
          end
        end
      end
    end

    def request_log_line
      $logger.info([env['REQUEST_METHOD'], env['REQUEST_PATH']].join(' '), {
        :remote_address => env['REMOTE_ADDR'],
        :user_agent => env['HTTP_USER_AGENT'],
        :request_method => env['REQUEST_METHOD'],
        :request_uri => env['REQUEST_URI'],
        :request_body =>  env['rack.input'].read
      })
      env['rack.input'].rewind
    end

    set :root, File.dirname(__FILE__)
    set :static, true
    set :public_folder, Proc.new { File.join(root, 'public') }

    use Rack::Auth::Basic do |user, password|
      user == $dashboard_settings[:user] && password == $dashboard_settings[:password]
    end

    before do
      content_type 'text/html'
      request_log_line<commit_msg>don't set auth defaults for the dashboard<commit_after>require 'sensu/base'
require 'thin'
require 'sinatra/async'
require 'em-http-request'
require 'slim'
require 'sass'
require 'uri'
require 'sprockets'
require 'yui/compressor'
require 'handlebars_assets'

module Sensu
  class Dashboard < Sinatra::Base
    register Sinatra::Async

    configure do
      set :assets, (Sprockets::Environment.new { |env|
          env.append_path(settings.root + "/assets/javascripts")
          env.append_path(settings.root + "/assets/stylesheets")
          env.append_path HandlebarsAssets.path
          if ENV['RACK_ENV'] == 'production'
            env.js_compressor  = YUI::JavaScriptCompressor.new
            env.css_compressor = YUI::CssCompressor.new
          end
        })
    end

    class << self
      def run(options={})
        EM::run do
          setup(options)
          start
          trap_signals
        end
      end

      def invalid_settings(reason, details={})
        $logger.fatal('invalid settings', {
          :reason => reason
        }.merge(details))
        $logger.fatal('SENSU DASHBOARD NOT RUNNING!')
        exit 2
      end

      def setup(options={})
        base = Sensu::Base.new(options)
        $logger = base.logger
        settings = base.settings
        $dashboard_settings = settings[:dashboard] || {
          :port => 8080
        }
        $api_settings = settings[:api] || {
          :host => 'localhost',
          :port => 4567,
          :user => nil,
          :password => <PASSWORD>
        }
        unless $dashboard_settings.is_a?(Hash)
          invalid_settings('misconfigured dashboard configuration')
        end
        unless $dashboard_settings[:port].is_a?(Integer)
          invalid_settings('dashboard port must be an integer', {
            :settings => $dashboard_settings
          })
        end
        base.setup_process
        $api_url = 'http://' + $api_settings[:host] + ':' + $api_settings[:port].to_s
        $api_options = {:head => {}}
        if $api_settings[:user] && $api_settings[:password]
          $api_options.merge!(:head => {:authorization => [$api_settings[:user], $api_settings[:password]]})
        end
      end

      def start
        Thin::Logging.silent = true
        Thin::Server.start(self, $dashboard_settings[:port])
      end

      def stop
        $logger.warn('stopping')
        EM::stop_event_loop
      end

      def trap_signals
        %w[INT TERM].each do |signal|
          Signal.trap(signal) do
            $logger.warn('received signal', {
              :signal => signal
            })
            stop
          end
        end
      end
    end

    def request_log_line
      $logger.info([env['REQUEST_METHOD'], env['REQUEST_PATH']].join(' '), {
        :remote_address => env['REMOTE_ADDR'],
        :user_agent => env['HTTP_USER_AGENT'],
        :request_method => env['REQUEST_METHOD'],
        :request_uri => env['REQUEST_URI'],
        :request_body =>  env['rack.input'].read
      })
      env['rack.input'].rewind
    end

    set :root, File.dirname(__FILE__)
    set :static, true
    set :public_folder, Proc.new { File.join(root, 'public') }

    helpers do
      def protected!
        unless authorized?
          response['WWW-Authenticate'] = %(Basic realm='Restricted Area')
          throw(:halt, [401, 'Not authorized\n'])
        end
      end

      def authorized?
        return true if [$dashboard_settings[:user], $dashboard_settings[:password]].all? { |param| param.nil? }
        @auth ||= Rack::Auth::Basic::Request.new(request.env)
        @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [$dashboard_settings[:user], $dashboard_settings[:password]]
      end
    end

    before do
      content_type 'text/html'
      request_log_line
      protected!
{'fraction_non_alphanumeric': 0.1281437125748503, 'fraction_numerical': 0.0027944111776447107, 'mean_word_length': 2.2424503882657465, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2329950', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2194, 'n_words': 448}