Class: Chook::Server
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- Chook::Server
- Defined in:
- lib/chook/server.rb,
lib/chook/server/log.rb,
lib/chook/server/auth.rb,
lib/chook/server/routes.rb,
lib/chook/server/routes/log.rb,
lib/chook/server/routes/home.rb,
lib/chook/server/routes/handlers.rb,
lib/chook/server/routes/login_logout.rb,
lib/chook/server/routes/handle_by_name.rb,
lib/chook/server/routes/handle_webhook_event.rb
Overview
see server.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_PORT =
80
- DEFAULT_SSL_PORT =
443
- DEFAULT_CONCURRENCY =
true
- DEFAULT_SESSION_EXPIRE =
one day
24 * 60 * 60
- HANDLE_EVENT_ROUTE =
'/handle_webhook_event'.freeze
Class Method Summary collapse
-
.admin_user_pw ⇒ Object
self.webhooks_user_pw.
-
.humanize_secs(secs) ⇒ Object
Very handy! lifted from stackoverflow.com/questions/4136248/how-to-generate-a-human-readable-time-range-using-ruby-on-rails.
-
.prep_to_run ⇒ Object
self.run.
-
.pw_from_command(cmd) ⇒ Object
def pw_from_conf(setting).
- .pw_from_conf(setting) ⇒ Object
- .pw_from_file(file) ⇒ Object
-
.run!(log_level: nil) ⇒ Object
Run the server.
-
.starttime ⇒ Object
prep to run.
- .uptime ⇒ Object
-
.webhooks_user_pw ⇒ Object
Learn the webhook or admin passwords from config.
Class Method Details
.admin_user_pw ⇒ Object
self.webhooks_user_pw
122 123 124 |
# File 'lib/chook/server/auth.rb', line 122 def self.admin_user_pw @admin_user_pw ||= pw_from_conf Chook.config.admin_pw end |
.humanize_secs(secs) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/chook/server.rb', line 110 def self.humanize_secs(secs) [[60, :second], [60, :minute], [24, :hour], [7, :day], [52.179, :week], [1_000_000, :year]].map do |count, name| next unless secs.positive? secs, n = secs.divmod(count) n = n.to_i "#{n} #{n == 1 ? name : (name.to_s + 's')}" end.compact.reverse.join(' ') end |
.prep_to_run ⇒ Object
self.run
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/chook/server.rb', line 73 def self.prep_to_run @start_time = Time.now log_level ||= Chook.config.log_level @log_level = Chook::Procs::STRING_TO_LOG_LEVEL.call log_level configure do set :logger, Log.startup(@log_level) set :server, :thin set :bind, '0.0.0.0' set :port, Chook.config.port set :show_exceptions, :after_handler if development? set :root, "#{File.dirname __FILE__}/server" enable :static enable :sessions set :sessions, expire_after: Chook.config.admin_session_expires if Chook.config.admin_user if Chook.config.concurrency set :threaded, true else enable :lock end end # configure Chook::HandledEvent::Handlers.load_handlers end |
.pw_from_command(cmd) ⇒ Object
def pw_from_conf(setting)
138 139 140 141 142 143 |
# File 'lib/chook/server/auth.rb', line 138 def self.pw_from_command(cmd) cmd = cmd.chomp '|' output = `#{cmd} 2>&1`.chomp raise "Can't get password from #{setting}: #{output}" unless $CHILD_STATUS.exitstatus.zero? output end |
.pw_from_conf(setting) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/chook/server/auth.rb', line 126 def self.pw_from_conf(setting) return '' unless setting # if the path ends with a pipe, its a command that will # return the desired password, so remove the pipe, # execute it, and return stdout from it. return pw_from_command(setting) if setting.end_with? '|' # otherwise its a file path, and read the pw from the contents pw_from_file(setting) end |
.pw_from_file(file) ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/chook/server/auth.rb', line 145 def self.pw_from_file(file) file = Pathname.new file return nil unless file.file? stat = file.stat mode = format('%o', stat.mode) raise "Password file #{setting} has insecure mode, must be 0600." unless mode.end_with?('0600') raise "Password file #{setting} has insecure owner, must be owned by UID #{Process.euid}." unless stat.owned? # chomping an empty string removes all trailing \n's and \r\n's file.read.chomp('') end |
.run!(log_level: nil) ⇒ Object
Run the server
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/chook/server.rb', line 55 def self.run!(log_level: nil) prep_to_run if Chook.config.use_ssl super do |server| server.ssl = true server. = { cert_chain_file: Chook.config.ssl_cert_path.to_s, private_key_file: Chook.config.ssl_private_key_path.to_s, verify_peer: false } end # super do else # no ssl super end # if use ssl end |
.starttime ⇒ Object
prep to run
98 99 100 |
# File 'lib/chook/server.rb', line 98 def self.starttime @start_time end |
.uptime ⇒ Object
102 103 104 |
# File 'lib/chook/server.rb', line 102 def self.uptime @start_time ? "#{humanize_secs(Time.now - @start_time)} ago" : 'Not Running' end |
.webhooks_user_pw ⇒ Object
Learn the webhook or admin passwords from config. so we can authenticate them from the browser and the JSS
This is at the Server level, since we only need read it once per server startup, so we store it in a server instance var.
118 119 120 |
# File 'lib/chook/server/auth.rb', line 118 def self.webhooks_user_pw @webhooks_user_pw ||= pw_from_conf Chook.config.webhooks_user_pw end |