我正在使用瘦 Web 服务器来提供 Ruby/Sinatra 应用程序。引起我注意的是,当我跑步时:
RACK_ENV=production rackup
每个请求都记录了 5 次,并且具有不同的时间戳。
vagrant@lucid32:/app$ RACK_ENV=production rackup
** Building /assets/application.js...
** Building /assets/screen.css...
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0024
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0034
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0044
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0054
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0065
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0076
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 4 0.0094
10.0.2.2 - - [12/Aug/2012 03:54:56] "GET / HTTP/1.1" 200 - 0.0102
这让我相信,不知何故,请求被多次提出。还是只是多次记录?
当我跑步时
rackup
并尝试相同的请求。这就是我得到的。
vagrant@lucid32:/app$ rackup
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:9292, CTRL+C to stop
10.0.2.2 - - [12/Aug/2012 03:56:42] "GET / HTTP/1.1" 200 - 0.1228
区别基本上是状态码后面的“-”,就像生产服务器中的最终日志行一样。但我真的不知道那是什么意思。
我想知道在生产中是否以某种方式处理了多次请求。我正在使用这样的 Sinatra,这条路线。
class Project::Base < Sinatra::Application
..configs here
end
class Project::Routes::Home < Project::Base
get '/'
"something"
end
end
配置.ru
RACK_ENV = ENV["RACK_ENV"] ||= "development" unless defined? RACK_ENV
require 'sprockets'
map '/assets' do
environment = Sprockets::Environment.new
environment.append_path 'app/assets/javascripts'
environment.append_path 'app/assets/stylesheets'
run environment
end
require File.dirname(__FILE__) + '/config/boot.rb'
use Rack::Deflater
run GRBTV::Main
./config/boot.rb
require 'rubygems'
gem 'psych'
# Require default environment config
require File.join(File.dirname(__FILE__), 'env')
# Bundler setup check
require 'bundler'
begin
Bundler.setup
rescue Bundler::BundlerError => e
$stderr.puts e.message
$stderr.puts "Run 'bundle install' to install missing gems"
exit e.status_code
end
# Sinatra
require 'sinatra/base'
require 'sinatra/flash'
require 'json'
RACK_ENV = ENV['RACK_ENV'] ||= 'development' unless defined? RACK_ENV
ROOT_DIR = File.dirname(__FILE__).gsub(/config/,'') unless defined? ROOT_DIR
# Path helper methods
# root_path("config", "settings.yml")
def root_path(*args)
File.join(ROOT_DIR, *args)
end
# relative_from_root_path("~/projects/project_name/config/settings.yml") #=> "config/settings.yml"
def relative_from_root_path(path)
Pathname.new(path).relative_path_from(Pathname.new(ROOT_DIR)).to_s
end
# public_path("images") #=> "public/images"
def public_path(*args)
root_path('public', *args)
end
# Module containers for autoloaded helpers and routes
module GRBTV
module Helpers
end
module Routes
end
end
# Attempts to require all dependencies with bundler, if this fails, bundle and then try again
require 'bundler'
Bundler.setup(:default)
Bundler.require(:default)
# Dependencies contains all required gems and core configuration
require root_path('config', 'dependencies.rb')
def app() GRBTV::Main end
./config/dependencies.rb
require File.join(File.dirname(__FILE__), './loader')
initializers = []
# Requires initializers
Loader.load_files('config/initializers/*.rb') do |file|
require file
begin
# Save Initializer module in the initializers array
file_class = File.basename(file, '.rb').camelize
initializers << "#{file_class}Initializer".constantize
rescue NameError
end
end
require 'yaml'
YAML::ENGINE.yamler = 'psych'
# Load app components
['lib/*.rb', 'app/uploaders/*.rb', 'app/models/*.rb'].each do |glob|
Loader.load_files(glob)
end
# Basic Sinatra::Application settingss
class GRBTV::Base < Sinatra::Application
def options() settings end
set :root, root_path
set :views, root_path('app', 'views')
set :public_folder, root_path('public')
set :environment, RACK_ENV.to_sym if defined? RACK_ENV
set :protection, :except => :frame_options
use Rack::MethodOverride
enable :sessions
register Sinatra::NamedRoutes
register MongoMapper
helpers Sinatra::FieldBuilder
helpers Sinatra::Warden::Helpers
# Require all helpers
Loader.load_files("app/helpers/**/*.rb") do |file|
relative_path = relative_from_root_path(file).gsub(/\.rb$/, '').gsub('app/helpers/', '')
module_name = "GRBTV::Helpers::#{relative_path.camelize}"
Loader.define_namespaces(module_name)
require file
helpers module_name.constantize
end
# Sets the default layout for every route
before do
@default_layout = :'layout'
end
before '/assets/*' do
headers['Cache-Control'] = 'public, max-age=86400'
end
end
# Load mailers
Loader.load_files('app/mailers/*.rb')
class GRBTV::Main < GRBTV::Base
# Requiring routes after extending path helpers
Loader.load_files("app/routes/**/*.rb") do |file|
relative_path= relative_from_root_path(file).gsub(/\.rb$/, '').gsub('app/routes/', '')
module_name = "GRBTV::Routes::#{relative_path.camelize}"
Loader.define_namespaces(module_name)
require file
use module_name.constantize
end
end
# Load all initializers after setup is done
initializers.each do |initializer|
GRBTV::Base.register initializer
end