From 5a87c29f70115ce0927cf6f90fdf8c83161c6052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E5=B3=A5?= Date: Wed, 19 Dec 2012 18:20:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E6=B5=8B=E8=AF=95=EF=BC=8C=E5=AE=9E=E7=8E=B0=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=8A=95=E9=80=92=E5=92=8C=E8=AE=A2=E9=98=85=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 6148 bytes .gitignore | 3 + Capfile | 4 ++ Gemfile | 24 ++++++- Gemfile.lock | 116 +++++++++++++++++++++++++++++++ config.ru | 7 ++ config/deploy.rb | 46 ++++++++++++ config/server.yml | 3 + lib/postman.rb | 52 ++++++++++++++ lib/postman/app/controller/mc.rb | 39 +++++++++++ lib/postman/configration.rb | 24 +++++++ public/.DS_Store | Bin 0 -> 6148 bytes public/favicon.ico | Bin 0 -> 4286 bytes script/console | 11 +++ 14 files changed, 326 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 Capfile create mode 100644 Gemfile.lock create mode 100644 config.ru create mode 100644 config/deploy.rb create mode 100644 config/server.yml create mode 100644 lib/postman/app/controller/mc.rb create mode 100644 lib/postman/configration.rb create mode 100644 public/.DS_Store create mode 100644 public/favicon.ico create mode 100755 script/console diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6056e16a7912be276fb215c37dbcde394da70a15 GIT binary patch literal 6148 zcmeHKL2uJA6noAN!!mEyF!O$beXlKnaM_w1L?u^khUXvV!=q6!gNNR0VyWE+gzIjz~2 zs<{OevX7X~sZS${4*fN@sW1u{1^zb$c^~WH0Z|9&nb`xqXBqYP zRMe$37mZ5&EC=37{QmSp6R0qeuuPQ4m^)soyE}$D;0g2 zUeY17Y13=!Ag@tG7n^24^D02em|QtRS090u7re2HIKtg5RZD3x1C`y?RdSu>xA!QIdY zRqPY57Y*N6?#yNn532TFeRp29XZ!p0s{LT^(R^NlReo5v@6{f^ICf^A=U=|cSV`c- zk#Xqp%%T8s)xy1(Y2Jbb&;B_ z0!{F>g_zgz#yLuJ^O(s#_?gut=O@iuz2Q}%QqU@2Rfnb@qkvK1+9<&LgN?-4P`F4? zZXL+v6#$q=wKSCZhk<=;g$;#^1knN$i7PO173zv15_i86v!pd6+Zvlm*4+0Ugps#U=+Ac3W$8G(`w?4)Y-cBZhY3- uNFR{cn72q!x*$`>u`KYN@-C7zwAow$HWV%rL=DXR2uK-BW)%3R3j75`= 2.3.5" @@ -6,9 +6,27 @@ source "http://rubygems.org" # 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" \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..1e1398e --- /dev/null +++ b/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 diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..a515fd0 --- /dev/null +++ b/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) \ No newline at end of file diff --git a/config/deploy.rb b/config/deploy.rb new file mode 100644 index 0000000..ed0bf42 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/config/server.yml b/config/server.yml new file mode 100644 index 0000000..924be4f --- /dev/null +++ b/config/server.yml @@ -0,0 +1,3 @@ +--- +:redis: redis://127.0.0.1:6379/ +:amqp: '127.0.0.1' \ No newline at end of file diff --git a/lib/postman.rb b/lib/postman.rb index e69de29..03de2e3 100644 --- a/lib/postman.rb +++ b/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} diff --git a/lib/postman/app/controller/mc.rb b/lib/postman/app/controller/mc.rb new file mode 100644 index 0000000..40a6447 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/postman/configration.rb b/lib/postman/configration.rb new file mode 100644 index 0000000..99c72dc --- /dev/null +++ b/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 \ No newline at end of file diff --git a/public/.DS_Store b/public/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..a71f73ce549a33add8e3efb5028448e12143c372 GIT binary patch literal 6148 zcmeHKy-vem3_Ld-DzS8A{6<#ZAgT%yE4%<|kr1e;L&ctrm*bt_`~#v1tPF6L>`VNe z9p#2N24Jh_yK`U(U_p1pi-)QCx%vhDM;c?u_u1B0`ryvESfE17dQsAE|z?to~I7w8L0#ZNI#=l=!$hyC9rU*D`(e7Tyflh^cF v+vvCS4nxGDN+c4ckm!<35wiB%4q?0ZXfj-<>BMCkbidHhYN1hQv7PVH znW=BTO??M_s)ml+)Ctc0^f>r^&3MrBt^PmA-fOeQXZ!K1Y~Fv8w;B&pRr5KoZP(gV zRr>{R?>Q0v0lg#ij zq=mXM(|@?l79$-*$|MElOP5otyJ^=qd;Eeq$z)IRwmpCDWElnVL6oLMP@bWoA|sme zbd9)O%69GFccO#e(S*NeS~}>%q=P_P?@v2XPVa5+KzwIap7$x{H+fi{xZo zAus1=p`VzWb(y5NE>yv7xVaw2&to&G)3YC{4(;XNTK6)!9;J5&iBnA^#F)vQZJ~UT zh3CsHR1}+-lXVGA3RXJMf4QQa@lqoN^DIn_6!n&Fp{(dm{ugzV zF8CpV21d9XWkCOC+?{p_{#l3R-^w31)7v9BB?%@v~CitTMMmyCz zl&|pZz}L&bLJ@zB%EYV`^FR1O0R~+Du2X<4l{U%L~H~Cb1lh3p_czxqlHmtwYcYI|h-fnHopJm2h z+D3Gcme>d_36r(NM`@W7+C-3iAH#+m=`;VmM(WAQG$9?;Ok%`pQYNn>Rr9K?O^jTF zN>Rz+LCrS4=g^ONWz#u_YMM;nDYP>eo})V&=q*H+wJ z#MWi@YTlnWU~KG`LRG_yPR=_Tne6L+Mk4!s|m(m)r1h z-yz1g6Yr6G#aQ;^>AH{L;P2V6^H;lj@cs5b>%JSGcv`HrMxs=0goo$}R~b-;8wgj6 z^E(O)GY~ML9i_~OY>WY^XB#rlQ%F6&L+RB*)t(y<%)^8DFT8P`HI?0jOzcGN(T=Z} zgCPnX5fgPp1+}6H(Ge{a8Q4nLI343Wbs{Hvaj$huN60R`z?Or*|1198-&T4{gI__USeIXi4U58vm5u`=ikr&UVC4+|MMU2_g7!# P;lGamz5fS1?(07QwmWnK literal 0 HcmV?d00001 diff --git a/script/console b/script/console new file mode 100755 index 0000000..2a9e048 --- /dev/null +++ b/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 \ No newline at end of file