Sha256: 51ebf11ba1d499533fc595cf7a94a24580c8f23745f998bd0242ee63852f8999

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

require 'zygote/http'
require 'thin'

# Main zygote namespace
module Zygote
  # Wrapper around thin / event machine to run zygote sinatra rack app in.
  class Server
    def initialize(port: 7000, threads: 1000, host: '0.0.0.0', config_path: nil, cells: [], debug: false)
      debug ||= ENV['DEBUG']

      cell_config = YAML.load(File.read(config_path || File.join(Dir.pwd, 'config', 'cells.yml')))
      Zygote::Web.views = [File.expand_path('../../../views', __FILE__), cells].flatten
      Zygote::Web.cell_config = cell_config
      if debug
        $stdout.sync = true
        $stderr.sync = true
      end

      app = Zygote::Web.new
      dispatch = Rack::Builder.app do
        map '/' do
          run app
        end
      end
      Thin::Logging.trace = true if debug
      @server = Thin::Server.new(port, host, dispatch, threadpool_size: threads).backend
    end

    def start
      @server.start
    rescue => ex
      puts ex
      puts ex.backtrace.join("\n")
    end

    def run
      EM.run do
        init_sighandlers
        @server.start
      end
    end
  end
end

def init_sighandlers
  clean_quit = lambda do
    EM.stop
    exit
  end

  Signal.trap('INT') { clean_quit.call }
  Signal.trap('TERM') { clean_quit.call }
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
zygote-0.2.15 lib/zygote/server.rb
zygote-0.2.14 lib/zygote/server.rb
zygote-0.2.13 lib/zygote/server.rb