Sha256: 0a576e09957fb0eef00a091638e8b0a4975c36fc7000d06d66fd6f7e7f8953bd

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

require 'role'
require 'sinatra/base'
require 'db_crap'

class RailsRole < Role
  PORT = 9300
  role 'rails'

  def initialize(manifest)
    super
    init_links 'db_write', @manifest['db_write']
    init_links 'db_read', @manifest['db_read']
    init_links 'db_peer', @manifest['db_peer']
  end

  def start
    # @manifest['db'].each do |db_addr|
    #  @db_sockets[db_addr] = DemoSocket.connect(db_addr, DbRole::PORT)
    #end

    start_server
  end

  def stop
    @http_server.shutdown
    # @db_sockets.each { |a,s| s.close }
  end

  alias :enable :start
  alias :disable :stop

  private

  def start_server
    my_app = MyApp.new(@active_links, @manifest)

    trap('INT') do
      @http_server.shutdown if @http_server
      exit
    end

    Thread.new {
      Rack::Handler::WEBrick.run(my_app, :Port => PORT.to_s) do |server|
        @http_server = server
      end
    }
  end

  class MyApp < Sinatra::Base
    def initialize(active_links, manifest)
      @active_links = active_links
      @manifest = manifest
      super()
    end

    set :public, File.dirname(__FILE__) + '/../public'
    set :views, File.dirname(__FILE__) + '/../views'

    get '/one' do
      @list = list
      erb :one
    end

    get '/two' do
      @list = list
      erb :two
    end

    post '/' do
      write request.params['key'], request.params['value']
      redirect request.params['return_to']
    end

    get '/ping' do
      'pong'
    end

    private

    include DbCrap
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bum-0.0.17 lib/app_role.rb
bum-0.0.16 lib/app_role.rb
bum-0.0.15 lib/app_role.rb
bum-0.0.14 lib/app_role.rb
bum-0.0.13 lib/app_role.rb
bum-0.0.12 lib/app_role.rb