Sha256: 0eb727c95f43929bbde13efd6c1f44997f4fae56b3068a3a544c7e57ad96aa86

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

#!/usr/bin/env ruby
require 'rubygems'
require 'main'

URI_MATCH = /(mongo|memcache):\/\/(.+?)\:(\d+)\/(\w+)\/{0,1}(\w*)/

Main {  
  option( 'pool=[pool]', 'p' ) {
    validate    { |pool| pool =~ URI_MATCH }
    description "specify server uri. Must be of the form {[mongo|memcache]}://{host}:{port}/{[database_name|namespace]}[/{collection_name}]"
  }
  environment( 'RACK_ENV' ) {
    default 'production'
  }
  
  @@options = {}
  
  # Enter main loop  
  def run
    require File.expand_path( File.join(File.dirname(__FILE__), %w[.. lib mongo3]))    
    
    @@options = parse_args( params[:pool].value )
    ENV['RACK_ENV'] = params['RACK_ENV'].value
    
    Thread.new do 
      puts "-"*100
      puts "Initializing mongo3 -- Version #{Mongo3::VERSION}"
      puts "-"*100      
      puts "\n"*2
      puts ">>> Waiting for Franky to warm up..."
      puts "\n"*2  
      sleep( sleep_time )
      puts "\n"*2  
      puts ">>> Opening console..."
      puts "\n"*2
      open( "http://localhost:#{default_port}" )
    end
    
    # Clearing args for franky!
    ARGV.clear
    require 'sinatra'    
    require File.join(File.dirname(__FILE__), %w[.. lib app.rb])
    Sinatra::Application.run! :port => default_port, :environment => 'production'
  end
  
  # default port
  def default_port() 6663; end
  
  # sleepy time...
  def sleep_time() 3; end
  
  # open console...
  def open(path)
    case RUBY_PLATFORM
      when /darwin/
        system 'open', path
      when /mswin(?!ce)|mingw|cygwin|bccwin/
        system 'start', path
      else
        system 'firefox', path
    end
  end
  
  # parse uri into sub components
  def parse_args( server_uri )
    return unless server_uri
    tokens = server_uri.match( URI_MATCH ).captures
    opts = { :protocol => tokens.first, :host => tokens[1], :port => tokens[2] }
    if opts[:protocol] == "mongo"
      opts[:db_name]   = tokens[3]
      opts[:cltn_name] = tokens[4]
    else
      opts[:namespace] = tokens[3]
    end
    opts
  end  
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo3-0.1.2 bin/mongo3
mongo3-0.1.1 bin/mongo3