require'fileutils'require'optparse'require'action_dispatch'require'rails'require'rails/dev_caching'moduleRailsclassServer<::Rack::ServerclassOptionsDEFAULT_PID_PATH=File.expand_path("tmp/pids/server.pid").freezedefparse!(args)args,options=args.dup,{}option_parser(options).parse!argsoptions[:log_stdout]=options[:daemonize].blank?&&(options[:environment]||Rails.env)=="development"options[:server]=args.shiftoptionsendprivatedefoption_parser(options)OptionParser.newdo|opts|opts.banner="Usage: rails server [mongrel, thin etc] [options]"opts.on("-p","--port=port",Integer,"Runs Rails on the specified port.","Default: 3000"){|v|options[:Port]=v}opts.on("-b","--binding=IP",String,"Binds Rails to the specified IP.","Default: localhost"){|v|options[:Host]=v}opts.on("-c","--config=file",String,"Uses a custom rackup configuration."){|v|options[:config]=v}opts.on("-d","--daemon","Runs server as a Daemon."){options[:daemonize]=true}opts.on("-e","--environment=name",String,"Specifies the environment to run this server under (test/development/production).","Default: development"){|v|options[:environment]=v}opts.on("-P","--pid=pid",String,"Specifies the PID file.","Default: tmp/pids/server.pid"){|v|options[:pid]=v}opts.on("-C","--[no-]dev-caching","Specifies whether to perform caching in development.","true or false"){|v|options[:caching]=v}opts.separator""opts.on("-h","--help","Shows this help message."){putsopts;exit}endendenddefinitialize(*)superset_environmentend# TODO: this is no longer required but we keep it for the moment to support older config.ru files.defapp@app||=beginapp=superapp.respond_to?(:to_app)?app.to_app:appendenddefopt_parserOptions.newenddefset_environmentENV["RAILS_ENV"]||=options[:environment]enddefstartprint_boot_informationtrap(:INT){exit}create_tmp_directoriessetup_dev_cachinglog_to_stdoutifoptions[:log_stdout]superensure# The '-h' option calls exit before @options is set.# If we call 'options' with it unset, we get double help banners.puts'Exiting'unless@options&&options[:daemonize]enddefmiddlewareHash.new([])enddefdefault_optionssuper.merge({Port:ENV.fetch('PORT',3000).to_i,DoNotReverseLookup:true,environment: (ENV['RAILS_ENV']||ENV['RACK_ENV']||"development").dup,daemonize: false,caching: nil,pid: Options::DEFAULT_PID_PATH,restart_cmd: restart_command})endprivatedefsetup_dev_cachingifoptions[:environment]=="development"Rails::DevCaching.enable_by_argument(options[:caching])endenddefprint_boot_informationurl="#{options[:SSLEnable]?'https':'http'}://#{options[:Host]}:#{options[:Port]}"puts"=> Booting #{ActiveSupport::Inflector.demodulize(server)}"puts"=> Rails #{Rails.version} application starting in #{Rails.env} on #{url}"puts"=> Run `rails server -h` for more startup options"enddefcreate_tmp_directories%w(cache pids sockets).eachdo|dir_to_make|FileUtils.mkdir_p(File.join(Rails.root,'tmp',dir_to_make))endenddeflog_to_stdoutwrapped_app# touch the app so the logger is set upconsole=ActiveSupport::Logger.new(STDOUT)console.formatter=Rails.logger.formatterconsole.level=Rails.logger.levelunlessActiveSupport::Logger.logger_outputs_to?(Rails.logger,STDOUT)Rails.logger.extend(ActiveSupport::Logger.broadcast(console))endenddefrestart_command"bin/rails server #{ARGV.join(' ')}"endendend