class CommunityZero::Server
A single instance of the Community Server
.
@author Seth Vargo <sethvargo@gmail.com>
Constants
- DEFAULT_OPTIONS
Default options to populate
Attributes
The list of options passed to the server.
@return [Hash]
Public Class Methods
Create a new Community site server.
@param [Hash] options
a list of options to pass in
@option options [String] :host
the host to listen on (default is 0.0.0.0)
@option options [String, Fixnum] :port
the port to listen on (default is 3389)
# File lib/community_zero/server.rb, line 56 def initialize(options = {}) @options = DEFAULT_OPTIONS.merge(options) @options[:host] = "[#{@options[:host]}]" if @options[:host].include?(':') @options.freeze end
Public Instance Methods
# File lib/community_zero/server.rb, line 188 def inspect "#<#{self.class} @url=#{url.inspect}>" end
Clear out any existing entires and reset the server’s contents to a clean state.
# File lib/community_zero/server.rb, line 180 def reset! store.destroy_all end
Boolean method to determine if the server is currently ready to accept requests. This method will attempt to make an HTTP request against the server. If this method returns true, you are safe to make a request.
@return [Boolean]
true if the server is accepting requests, false otherwise
# File lib/community_zero/server.rb, line 143 def running? if @server.nil? || @server.status != :Running return false end uri = URI.join(url, 'cookbooks') headers = { 'Accept' => 'application/json' } Timeout.timeout(0.1) { !open(uri, headers).nil? } rescue SocketError, Errno::ECONNREFUSED, Timeout::Error false end
Start a Community Zero server in the current thread. You can stop this server by canceling the current thread.
@param [Boolean] publish
publish the server information to STDOUT
@return [nil]
this method will block the main thread until interrupted
# File lib/community_zero/server.rb, line 88 def start(publish = true) if publish puts <<-EOH.gsub(/^ {10}/, '') >> Starting Community Zero (v#{CommunityZero::VERSION})... >> WEBrick (v#{WEBrick::VERSION}) on Rack (v#{Rack.release}) is listening at #{url} >> Press CTRL+C to stop EOH end thread = start_background %w[INT TERM].each do |signal| Signal.trap(signal) do puts "\n>> Stopping Community Zero..." @server.shutdown end end # Move the background process to the main thread thread.join end
Start a Community Zero server in a forked process. This method returns the PID to the forked process.
@param [Fixnum] wait
the number of seconds to wait for the server to start
@return [Thread]
the thread the background process is running in
# File lib/community_zero/server.rb, line 121 def start_background(wait = 5) @server = WEBrick::HTTPServer.new( :BindAddress => @options[:host], :Port => @options[:port], :AccessLog => [], :Logger => WEBrick::Log.new(StringIO.new, 7) ) @server.mount('/', Rack::Handler::WEBrick, app) @thread = Thread.new { @server.start } @thread.abort_on_exception = true @thread end
Gracefully stop the Community Zero server.
@param [Fixnum] wait
the number of seconds to wait before raising force-terminating the server
# File lib/community_zero/server.rb, line 163 def stop(wait = 5) Timeout.timeout(wait) do @server.shutdown @thread.join(wait) if @thread end rescue Timeout::Error if @thread $stderr.puts("Community Zero did not stop within #{wait} seconds! Killing...") @thread.kill end ensure @server = nil @thread = nil end
The data store (by default, this is just a regular store)
# File lib/community_zero/server.rb, line 65 def store @store ||= Store.new end
# File lib/community_zero/server.rb, line 184 def to_s "#<#{self.class} #{url}>" end
The URL for this Community Zero server.
@return [String]
# File lib/community_zero/server.rb, line 74 def url "http://#{@options[:host]}:#{@options[:port]}" end
Private Instance Methods
The actual application the server will respond to.
@return [RackApp]
# File lib/community_zero/server.rb, line 199 def app lambda do |env| request = Request.new(env) response = router.call(request) response[-1] = Array(response[-1]) response end end
# File lib/community_zero/server.rb, line 209 def router @router ||= Router.new(self, ['/search', SearchEndpoint], ['/cookbooks', CookbooksEndpoint], ['/cookbooks/:name', CookbookEndpoint], ['/cookbooks/:name/versions/:version', CookbookVersionsVersionEndpoint], ) end