Class: Statusz::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/statusz.rb

Overview

A Rack server that can serve statusz.

Instance Method Summary collapse

Constructor Details

#initialize(filename = "./statusz.json", extra_fields = {}) ⇒ Server

Set up the Statusz::Server Rack app.

Parameters:

  • filename (String) (defaults to: "./statusz.json")

    the json statusz file written at deploy time. If filename is nil, then statusz will output an html file containing only the fields in extra_fields.

  • extra_fields (Hash) (defaults to: {})

    extra key/value pairs to include in the output.



116
117
118
119
# File 'lib/statusz.rb', line 116

def initialize(filename = "./statusz.json", extra_fields = {})
  @filename = filename
  @extra_fields = extra_fields
end

Instance Method Details

#call(env) ⇒ Object

The usual Rack app call method.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/statusz.rb', line 122

def call(env)
  headers = {}
  path = Rack::Request.new(env).path
  if path =~ /\.json$/
    headers["Content-Type"] = "application/json"
    format = :json
  elsif path =~ /\.txt$/
    headers["Content-Type"] = "text/plain"
    format = :text
  else
    headers["Content-Type"] = "text/html"
    format = :html
  end
  begin
    body = Statusz.render_from_json(@filename, format, @extra_fields)
  rescue StandardError => error
    return [500, { "Content-Type" => "text/plain" }, ["Error with statusz:\n#{error.message}"]]
  end
  [200, headers, [body]]
end