Rails 3 提供了 match
方法供我们自定义 routes,然而我们要小心使用它以避免“跨站脚本攻击”(XSS Attack)。比如像这样的 routes:
注:(r3 代表 Rails 3,r4 代表 Rails 4)
# routes.rb
class ApiLogger < Grape::Middleware::Base | |
def before | |
Rails.logger.info "[api] Requested: #{request_log_data.to_json}\n" + | |
"[api] #{response_log_data[:description]} #{response_log_data[:source_file]}:#{response_log_data[:source_line]}" | |
end | |
private | |
def request_log_data |
module Todo | |
class API < Grape::API | |
use Rack::Session::Cookie | |
version 'v1', :format => :json | |
helpers do | |
def current_user | |
return nil if env['rack.session'][:user_id].nil? | |
@current_user ||= User.get(env['rack.session'][:user_id]) | |
end |
# | |
# Wide-open CORS config for nginx | |
# | |
location / { | |
if ($request_method = 'OPTIONS') { | |
add_header 'Access-Control-Allow-Origin' '*'; | |
# |
require 'jpush_api_ruby_client' | |
class ClientExample | |
app_key = '466f7032ac604e02fb7bda89' #必填,例如466f7032ac604e02fb7bda89 | |
master_secret = '57c45646c772983fede7c455' #必填,每个应用都对应一个masterSecret | |
# 保存离线的时长。秒为单位。最多支持10天(864000秒)。 | |
# 0 表示该消息不保存离线。即:用户在线马上发出,当前不在线用户将不会收到此消息。 | |
# 此参数不设置则表示默认,默认为保存1天的离线消息(86400秒)。 |
types { | |
# other mime types... | |
application/json json; | |
} |
require 'mina/bundler' | |
require 'mina/rails' | |
require 'mina/git' | |
require 'mina/rvm' | |
# Usually mina focuses on deploying to one host and the deploy options are therefore simple. | |
# In our case, there is a number of possible servers to deploy to, it is therefore necessary to | |
# specify the host that we are targeting. | |
server = ENV['server'] | |
# Since the same host can have multiple applications running in parallel, it is necessary to |
DEPLOY_PATH = { | |
:staging => 'app-name', | |
:production => 'app-name', | |
:uat => 'app-name-uat' | |
} | |
SERVICES = %w(app-name) | |
ENVIRONMENTS = [:staging, :uat] #这是测试环境的配置,对于产品环境可以改成[:production] | |
TIMEOUT = 30.seconds | |
LONG_TIMEOUT = 60.seconds |
# lib/tasks/deploy.rake | |
namespace :deploy do | |
desc 'Deploy to staging environment' | |
task :staging do | |
exec 'mina deploy -f config/deploy/staging.rb' | |
end | |
end |