# We need to require application after the server sets environment,
# otherwise the --environment option given to the server won't propagate.
# Require application after server sets environment to propagate
# the --environment option.
requireAPP_PATH
Dir.chdir(Rails.application.root)
server.start
end
end
defrequire_command!(command)
require"rails/commands/#{command}"
end
end
end
end
```
This file will change into the Rails root directory (a path two directories up
from `APP_PATH` which points at `config/application.rb`), but only if the
`config.ru` file isn't found. This then requires `rails/commands/server` which
sets up the `Rails::Server` class.
```ruby
require'fileutils'
require'optparse'
require'action_dispatch'
require'rails'
moduleRails
classServer<::Rack::Server
```
`fileutils` and `optparse` are standard Ruby libraries which provide helper functions for working with files and parsing options.
`config.ru` file isn't found. This then starts up the `Rails::Server` class.
### `actionpack/lib/action_dispatch.rb`
Action Dispatch is the routing component of the Rails framework.
It adds functionality like routing, session, and common middlewares.
### `rails/commands/server.rb`
### `rails/commands/server/server_command.rb`
The `Rails::Server` class is defined in this file by inheriting from `Rack::Server`. When `Rails::Server.new` is called, this calls the `initialize` method in `rails/commands/server.rb`:
The `Rails::Server` class is defined in this file by inheriting from
`Rack::Server`. When `Rails::Server.new` is called, this calls the `initialize`
method in `rails/commands/server/server_command.rb`:
```ruby
definitialize(*)
...
...
@@ -252,7 +242,10 @@ end
In this case, `options` will be `nil` so nothing happens in this method.
After `super` has finished in `Rack::Server`, we jump back to `rails/commands/server.rb`. At this point, `set_environment` is called within the context of the `Rails::Server` object and this method doesn't appear to do much at first glance:
After `super` has finished in `Rack::Server`, we jump back to
`rails/commands/server/server_command.rb`. At this point, `set_environment`
is called within the context of the `Rails::Server` object and this method
doesn't appear to do much at first glance:
```ruby
defset_environment
...
...
@@ -289,17 +282,15 @@ With the `default_options` set to this: