Skip to content

Commit

Permalink
初始化代码,测试,实现消息投递和订阅功能
Browse files Browse the repository at this point in the history
  • Loading branch information
charlescui committed Dec 19, 2012
1 parent 9cc8875 commit 5a87c29
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 3 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -47,3 +47,6 @@ pkg

# For rubinius:
#*.rbc

log/*
./config/server.yml
4 changes: 4 additions & 0 deletions Capfile
@@ -0,0 +1,4 @@
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
# load 'deploy/assets'
load 'config/deploy' # remove this line to skip loading any of the default tasks
24 changes: 21 additions & 3 deletions Gemfile
@@ -1,14 +1,32 @@
source "http://rubygems.org"
source "http://ruby.taobao.org"
# Add dependencies required to use your gem here.
# Example:
# gem "activesupport", ">= 2.3.5"

# Add dependencies to develop your gem here.
# Include everything needed to run rake, tests, features, etc.
group :development do
gem "better_errors"
gem "binding_of_caller"
gem "shoulda", ">= 0"
gem "rdoc", "~> 3.12"
gem "bundler", "~> 1.0.0"
gem "bundler", "> 1.0.0"
gem "jeweler", "~> 1.8.4"
gem "rcov", ">= 0"
gem "simplecov"
gem 'linecache19', :git => 'git://github.com/mark-moseley/linecache'
gem 'ruby-debug-base19x', '~> 0.11.30.pre4'
gem 'ruby-debug19'
end

group :production do

end

gem "thin"
gem "amqp"
gem "json"
gem 'uuid'
gem "redis"
gem "hiredis"
gem "sinatra"
gem "eventmachine"
116 changes: 116 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,116 @@
GIT
remote: git://github.com/mark-moseley/linecache
revision: 869c6a65155068415925067e480741bd0a71527e
specs:
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)

GEM
remote: http://ruby.taobao.org/
specs:
activesupport (3.2.9)
i18n (~> 0.6)
multi_json (~> 1.0)
amq-client (0.9.10)
amq-protocol (>= 0.9.4)
eventmachine
amq-protocol (1.0.1)
amqp (0.9.8)
amq-client (~> 0.9.5)
amq-protocol (>= 0.9.4)
eventmachine
archive-tar-minitar (0.5.2)
better_errors (0.2.0)
coderay (>= 1.0.0)
erubis (>= 2.7.0)
binding_of_caller (0.6.8)
bourne (1.1.2)
mocha (= 0.10.5)
coderay (1.0.8)
columnize (0.3.6)
daemons (1.1.9)
erubis (2.7.0)
eventmachine (1.0.0)
git (1.2.5)
hiredis (0.4.5)
i18n (0.6.1)
jeweler (1.8.4)
bundler (~> 1.0)
git (>= 1.2.5)
rake
rdoc
json (1.7.5)
macaddr (1.6.1)
systemu (~> 2.5.0)
metaclass (0.0.1)
mocha (0.10.5)
metaclass (~> 0.0.1)
multi_json (1.5.0)
rack (1.4.1)
rack-protection (1.3.2)
rack
rake (10.0.3)
rdoc (3.12)
json (~> 1.4)
redis (3.0.2)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby_core_source (>= 0.1.4)
ruby-debug-base19x (0.11.30.pre10)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
rake (>= 0.8.1)
ruby_core_source (>= 0.1.4)
ruby-debug19 (0.11.6)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
shoulda (3.3.2)
shoulda-context (~> 1.0.1)
shoulda-matchers (~> 1.4.1)
shoulda-context (1.0.1)
shoulda-matchers (1.4.2)
activesupport (>= 3.0.0)
bourne (~> 1.1.2)
simplecov (0.7.1)
multi_json (~> 1.0)
simplecov-html (~> 0.7.1)
simplecov-html (0.7.1)
sinatra (1.3.3)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
systemu (2.5.2)
thin (1.5.0)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
tilt (1.3.3)
uuid (2.3.6)
macaddr (~> 1.0)

PLATFORMS
ruby

DEPENDENCIES
amqp
better_errors
binding_of_caller
bundler (> 1.0.0)
eventmachine
hiredis
jeweler (~> 1.8.4)
json
linecache19!
rdoc (~> 3.12)
redis
ruby-debug-base19x (~> 0.11.30.pre4)
ruby-debug19
shoulda
simplecov
sinatra
thin
uuid
7 changes: 7 additions & 0 deletions config.ru
@@ -0,0 +1,7 @@
require "pp"
require "./lib/postman"
require "ruby-debug" if Postman.env == 'development'

pp ::Postman::App::Controller.controllers_url_map

run Rack::URLMap.new(::Postman::App::Controller.controllers_url_map)
46 changes: 46 additions & 0 deletions config/deploy.rb
@@ -0,0 +1,46 @@
set :application, "postman"

set :repository, "."
set :scm, :none
set :scm_username, "zheng.cuizh@gmail.com"
set :keep_releases, 5 # 留下多少个版本的源代码
set :deploy_via, :copy
set :deploy_to, "/home/www/postman"

# set :scm, :git # You can set :scm explicitly or Capistrano will make an intelligent guess based on known version control directory names
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`

role :web, "42.121.89.18" # Your HTTP server, Apache/etc
role :app, "42.121.89.18" # This may be the same as your `Web` server
role :db, "42.121.89.18", :primary => true # This is where Rails migrations will run

set :user, "www"
set :use_sudo, false
# if you want to clean up old releases on each deploy uncomment this:
after "deploy:restart", "deploy:cleanup"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true
ssh_options[:auth_methods] = %w(publickey)
ssh_options[:port] = 27788

namespace :deploy do
task :start, :roles => :app do
run "cd #{current_path};bundle exec thin start -p 8000 -e production"
end
desc "Reload"
task :restart, :roles => :app do
run "cd #{current_path};bundle exec thin restart -e production"
end
task :stop, :roles => :app do
run "cd #{current_path};bundle exec thin stop -e production"
end
end

after 'deploy:update_code' do
softlinks = [
"rm -rf #{release_path}/tmp/sockets;ln -nfs #{deploy_to}/shared/sockets #{release_path}/tmp/sockets",
"rm -rf #{release_path}/config/server.yml;ln -nfs #{deploy_to}/shared/system/server.yml #{release_path}/config/server.yml"
]
run "#{softlinks.join(';')}"
end
3 changes: 3 additions & 0 deletions config/server.yml
@@ -0,0 +1,3 @@
---
:redis: redis://127.0.0.1:6379/
:amqp: '127.0.0.1'
52 changes: 52 additions & 0 deletions lib/postman.rb
@@ -0,0 +1,52 @@
$:.unshift File.dirname(__FILE__)

require "amqp"
require "sinatra/base"
require "better_errors"
require_relative "postman/configration"
require "active_support"
require 'active_support/core_ext/string/inflections'

Dir[File.join(File.dirname(__FILE__), 'postman', 'app', 'model', '*.rb')].each{|file| require_relative file}

module Postman
def self.env
@_env ||= ENV["ENV"] || ENV["RACK_ENV"] || "development"
end

module App
module Controller
def self.controllers_url_map
@@_controllers_url_map ||= {'/' => ApplicationController}
end

class ApplicationController < Sinatra::Base
def self.inherited(subclass)
super
Controller.controllers_url_map['/'+subclass.to_s.gsub('Postman::App::Controller::', '').underscore] = subclass
end

dir = File.dirname(File.expand_path(__FILE__))

set :views, "#{dir}/postman/app/view"
if respond_to? :public_folder
set :public_folder, "../#{dir}/public"
else
set :public, "../#{dir}/public"
end
set :static, true

configure :production, :development do
enable :logging
end

configure :development do
use BetterErrors::Middleware
BetterErrors.application_root = File.expand_path("..", __FILE__)
end
end#ApplicationController
end#Controller
end#App
end#Postman

Dir[File.join(File.dirname(__FILE__), 'postman', 'app', 'controller', '*.rb')].each{|file| require_relative file}
39 changes: 39 additions & 0 deletions lib/postman/app/controller/mc.rb
@@ -0,0 +1,39 @@
module Postman
module App
module Controller
class Mc < ApplicationController
get '/' do
"message center."
end

get '/pub' do
stream do |out|
AMQP::Channel.new(::P::C.amqp) do |channel, open_ok|
exchange = channel.default_exchange
AMQP::Queue.new(channel, params[:key], :auto_delete => true, :durable => true) do |queue|
exchange.publish params[:content], :routing_key => queue.name do
out << "hihihi"
channel.close do |close_ok|
out << {:status => 0, :msg => 'ok'}.to_json
end
end
end
end
end
end#pub

get '/sub' do
stream :keep_open do |out|
AMQP::Channel.new(::P::C.amqp) do |channel, open_ok|
AMQP::Queue.new(channel, params[:key], :auto_delete => true, :durable => true) do |queue|
queue.subscribe do |payload|
out << payload
end
end
end
end
end#sub
end
end
end
end
24 changes: 24 additions & 0 deletions lib/postman/configration.rb
@@ -0,0 +1,24 @@
module Postman
module Configration
class << self
def config!
YAML.load(File.open File.join(File.dirname(__FILE__), "..", "..", "config","server.yml"))
end

def config
@config ||= self.config!
end

def redis
@redis ||= Redis.connect(:url => config[:redis])
end

def amqp
@amqp ||= AMQP.connect(:host => config[:amqp])
end
end
end
end

P = Postman
P::C = Postman::Configration
Binary file added public/.DS_Store
Binary file not shown.
Binary file added public/favicon.ico
Binary file not shown.
11 changes: 11 additions & 0 deletions script/console
@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
require 'ruby-debug'
APP_PATH = File.expand_path('../..', __FILE__)
require File.join(APP_PATH, 'lib/postman')

require 'irb'
require 'irb/completion'

require 'ruby-debug' if Postman.env == 'development'

IRB.start

0 comments on commit 5a87c29

Please sign in to comment.