Skip to content

Commit

Permalink
Merge pull request #1 from bay6/master
Browse files Browse the repository at this point in the history
merge back sphinx
  • Loading branch information
hlee committed May 13, 2013
2 parents 427325c + 0aab09f commit 6d8b4f6
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

# Ignore the default SQLite database.
/db/*.sqlite3
/db/sphinx/

# Ignore all logfiles and tempfiles.
/log/*.log
/log/*
/tmp

# Ignore uploads
Expand All @@ -26,3 +28,5 @@

# Ignore the database.yml
/config/database.yml
/config/development.sphinx.conf
dump.rdb
4 changes: 4 additions & 0 deletions Capfile
Original file line number Diff line number Diff line change
@@ -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
8 changes: 5 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
source 'http://ruby.taobao.org/'

gem 'rails', '3.2.12'
gem 'mysql2'
gem 'mysql2', "~> 0.3.12b6"
gem 'slim-rails'
gem 'kaminari'
gem 'nokogiri'
Expand All @@ -16,6 +16,8 @@ gem 'carrierwave'
gem 'mini_magick'
# tag
gem 'acts-as-taggable-on'
# fulltext search
gem 'thinking-sphinx'

group :assets do
gem 'sass-rails', '~> 3.2.3'
Expand All @@ -24,7 +26,7 @@ group :assets do
gem 'uglifier', '>= 1.0.3'
gem 'turbo-sprockets-rails3'
gem 'bootstrap-sass'
gem 'therubyracer', :platforms => :ruby #Recommended to install Node.js, without therubyracer
#gem 'therubyracer', :platforms => :ruby #Recommended to install Node.js, without therubyracer
end

group :development do
Expand All @@ -49,7 +51,7 @@ group :test do
end

group :production do
gem 'pg'
#gem 'pg'
gem 'exception_notification'
gem 'unicorn', :platforms => :ruby
end
22 changes: 12 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ GEM
highline (1.6.16)
hike (1.2.1)
i18n (0.6.4)
innertube (1.0.2)
jasmine (1.3.2)
jasmine-core (~> 1.3.1)
rack (~> 1.0)
Expand All @@ -109,7 +110,6 @@ GEM
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
kgio (2.8.0)
libv8 (3.11.8.13)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
Expand All @@ -118,11 +118,12 @@ GEM
rack-contrib
railties
method_source (0.8.1)
middleware (0.1.0)
mime-types (1.21)
mini_magick (3.5.0)
subexec (~> 0.2.1)
multi_json (1.7.1)
mysql2 (0.3.11)
mysql2 (0.3.12b6)
net-scp (1.1.0)
net-ssh (>= 2.6.5)
net-sftp (2.1.1)
Expand All @@ -132,7 +133,6 @@ GEM
net-ssh (>= 2.6.5)
nokogiri (1.5.8)
orm_adapter (0.4.0)
pg (0.15.1)
polyglot (0.3.3)
pry (0.9.12)
coderay (~> 1.0.5)
Expand Down Expand Up @@ -170,7 +170,7 @@ GEM
rake (10.0.3)
rdoc (3.12.2)
json (~> 1.4)
ref (1.0.3)
riddle (1.5.6)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
Expand Down Expand Up @@ -220,13 +220,16 @@ GEM
tilt (~> 1.1, != 1.3.0)
subexec (0.2.2)
temple (0.5.5)
therubyracer (0.11.4)
libv8 (~> 3.11.8.12)
ref
thin (1.5.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
thinking-sphinx (3.0.3)
activerecord (>= 3.1.0)
builder (>= 2.1.2)
innertube (>= 1.0.2)
middleware (>= 0.1.0)
riddle (>= 1.5.6)
thor (0.17.0)
tilt (1.3.6)
treetop (1.4.12)
Expand Down Expand Up @@ -272,9 +275,8 @@ DEPENDENCIES
kaminari
meta_request
mini_magick
mysql2
mysql2 (~> 0.3.12b6)
nokogiri
pg
pry-nav
quiet_assets
rails (= 3.2.12)
Expand All @@ -283,8 +285,8 @@ DEPENDENCIES
simple_form
simplecov
slim-rails
therubyracer
thin
thinking-sphinx
turbo-sprockets-rails3
uglifier (>= 1.0.3)
unicorn
4 changes: 2 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
## 演示地址
heroku: http://shbook.herokuapp.com

## books-share [![Build Status](https://travis-ci.org/caok/books-share.png?branch=master)](https://travis-ci.org/caok/books-share)
This is the source code of [books-share](http://book-share.cloudfoundry.com).

## Take a look
http://book-share.cloudfoundry.com
cloudfoundry: http://book-share.cloudfoundry.com
heroku: http://shbook.herokuapp.com

## Requirements

Expand Down
9 changes: 8 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
class ApplicationController < ActionController::Base
protect_from_forgery

# cancan exception handler
# cancan exception handler
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, alert: I18n.t("unauthorized.default")
end

protected
def sphinx_escape(s, default='')
s = default.strip if s.nil?
keywords = s.split(/\s+/).map{|k| Riddle.escape("#{k}")}
keywords.join(' | ')
end
end
9 changes: 9 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ def index
@books = Book.tagged_with(params[:author], :on => :authors, :any => true)
when params[:translator]
@books = Book.tagged_with(params[:translator], :on => :translators, :any => true)
when params[:q]
search_options = {
:with => {},
:page => params[:page],
:per_page => 2,
:order => :created_at,
:sort_mode => :desc
}
@books = Book.search sphinx_escape(params[:q]), search_options
else
@books = Book.all
end
Expand Down
10 changes: 10 additions & 0 deletions app/indices/book_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ThinkingSphinx::Index.define :book, :with => :active_record do
indexes name, sortable: true
indexes content
indexes publishing_house
indexes :ISBN

indexes authors.name, :as => :authors

has id, created_at, updated_at
end
1 change: 1 addition & 0 deletions app/views/books/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@
b= resource.format
td= resource.file_size
td= link_to t("helpers.links.hyperlink"), resource.links, class: "btn btn-mini btn-success", target: "_blank"
= render "shared/uyan_js", :su => "/books/#{@book.id}"
2 changes: 1 addition & 1 deletion app/views/common/_search_form.html.slim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*= form_tag '/search', class: 'navbar-search' do*/
/*= text_field_tag 'q', @q, class: 'input-medium search-query', placeholder: @q || 'Search'*/
form.navbar-search action="/search" method="get" target='_blank'
form.navbar-search action="/books" method="get"
input.search-query.span3 name="q" placeholder=t("layout.search") type="text" value=(params[:q])
7 changes: 7 additions & 0 deletions app/views/shared/_uyan_js.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#uyan_frame
javascript:
var uyan_config = {
du: "#{Rails.env.production? ? "book-share.cloudfoundry.com" : "test.me"}",
su: "#{su}"
};
script type="text/javascript" id="UYScript" src="http://v1.uyan.cc/js/iframe.js?UYUserId=1777087" async=""
67 changes: 67 additions & 0 deletions config/deploy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#encoding:utf-8
require 'bundler/capistrano'
require 'thinking_sphinx/capistrano'

set :application, "books-share"
#set :repository, "git://github.com/caok/books-share.git"
set :repository, File.expand_path('../../.git/', __FILE__)
#set :branch, "master"

set :scm, :git

set :user, ENV['USER'] || "ruby"
set :use_sudo, false

# 部署路径修改为当前用户的目录,如果用默认的根目录且没有root权限会引起Permission denied的错误
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache # 不要每次都获取全新的repository
set :deploy_server, 'localhost'

set :bundle_without, [:development, :test]

# for rbenv
set :rbenv_version, ENV['RBENV_VERSION'] || "1.9.3-p392"
set :default_environment, {
'PATH' => "/home/#{user}/.rbenv/shims:/home/#{user}/.rbenv/bin:$PATH",
'RBENV_VERSION' => "#{rbenv_version}",
}

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

after "deploy", "deploy:cleanup" # keep only the last 5 releases

namespace :deploy do
desc "Start Application"
task :start, :roles => :app do
run "cd #{current_path}; RAILS_ENV=production bundle exec unicorn_rails -c config/unicorn.rb -D"
end

desc "Stop Application"
task :stop, :roles => :app do
run "kill -QUIT `cat #{shared_path}/pids/unicorn.#{application}.pid`"
end

desc "Restart Application"
task :restart, :roles => :app do
run "kill -USR2 `cat #{shared_path}/pids/unicorn.#{application}.pid`"
end

desc "Populates the Production Database"
task :seed do
run "cd #{current_path}; bundle exec rake db:seed"
end

task :setup_config, roles: :app do
run "mkdir -p #{shared_path}/config"
put File.read("config/database.yml.example"), "#{shared_path}/config/database.yml"
end
after "deploy:setup", "deploy:setup_config"

task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update","deploy:symlink_config"
end
8 changes: 8 additions & 0 deletions config/thinking_sphinx.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
development: &default
enable_star: 1
min_infix_len: 1
ngram_len: 1
ngram_chars : "U+3000..U+2FA1F"

production :
<<: *default
61 changes: 57 additions & 4 deletions config/unicorn.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
# config/unicorn.rb
worker_processes 3
timeout 30
preload_app true
# encoding: utf-8
# Spawn unicorn master worker for user apps (group: apps)
user ENV['USER'] || 'ruby', ENV['USER'] || 'ruby'

# Set your full path to application.
user = ENV['USER'] || 'ruby'
application = "books-share"
app_path = "/home/#{user}/apps/#{application}"
shared_path = "#{app_path}/shared"
current_path = "#{app_path}/current"

# Set unicorn options
worker_processes 2
preload_app true # Preload our app for more speed
timeout 180
# 可同时监听 Unix 本地 socket 或 TCP 端口
listen 9000, :tcp_nopush => true
#listen "/tmp/unicorn.#{application}.sock", :backlog => 64

# Fill path to your app
working_directory current_path

# Should be 'production' by default, otherwise use other env
rails_env = 'production'

# Log everything to one file
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"
# Set master PID location
pid "#{shared_path}/pids/unicorn.#{application}.pid"

if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end

before_fork do |server, worker|
ActiveRecord::Base.connection.disconnect!

old_pid = "#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end

after_fork do |server, worker|
ActiveRecord::Base.establish_connection
end

# 修正无缝重启unicorn后更新的Gem未生效的问题,原因是config/boot.rb会优先从ENV中获取BUNDLE_GEMFILE,
# 而无缝重启时ENV['BUNDLE_GEMFILE']的值并>未被清除,仍指向旧目录的Gemfile
before_exec do |server|
ENV["BUNDLE_GEMFILE"] = "#{current_path}/Gemfile"
end

0 comments on commit 6d8b4f6

Please sign in to comment.