application.rb 10.6 KB
Newer Older
L
Lin Jen-Shin 已提交
1
require File.expand_path('boot', __dir__)
G
gitlabhq 已提交
2 3

require 'rails/all'
4

5
Bundler.require(:default, Rails.env)
G
gitlabhq 已提交
6 7

module Gitlab
J
Jan Provaznik 已提交
8 9 10
  # This method is used for smooth upgrading from the current Rails 4.x to Rails 5.0.
  # https://gitlab.com/gitlab-org/gitlab-ce/issues/14286
  def self.rails5?
J
Jan Provaznik 已提交
11
    !%w[0 false].include?(ENV["RAILS5"])
J
Jan Provaznik 已提交
12 13
  end

G
gitlabhq 已提交
14
  class Application < Rails::Application
15
    require_dependency Rails.root.join('lib/gitlab/redis/wrapper')
16 17 18
    require_dependency Rails.root.join('lib/gitlab/redis/cache')
    require_dependency Rails.root.join('lib/gitlab/redis/queues')
    require_dependency Rails.root.join('lib/gitlab/redis/shared_state')
19
    require_dependency Rails.root.join('lib/gitlab/request_context')
20
    require_dependency Rails.root.join('lib/gitlab/current_settings')
21
    require_dependency Rails.root.join('lib/gitlab/middleware/read_only')
J
Jan Provaznik 已提交
22
    require_dependency Rails.root.join('lib/gitlab/middleware/basic_health_check')
23

J
Jan Provaznik 已提交
24 25 26 27 28
    # This needs to be loaded before DB connection is made
    # to make sure that all connections have NO_ZERO_DATE
    # setting disabled
    require_dependency Rails.root.join('lib/mysql_zero_date')

29 30 31
    # This can be removed when we drop support for rails 4
    require_dependency Rails.root.join('lib/rails4_migration_version')

G
gitlabhq 已提交
32 33 34 35
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

36 37 38 39 40 41 42 43
    # Sidekiq uses eager loading, but directories not in the standard Rails
    # directories must be added to the eager load paths:
    # https://github.com/mperham/sidekiq/wiki/FAQ#why-doesnt-sidekiq-autoload-my-rails-application-code
    # Also, there is no need to add `lib` to autoload_paths since autoloading is
    # configured to check for eager loaded paths:
    # https://github.com/rails/rails/blob/v4.2.6/railties/lib/rails/engine.rb#L687
    # This is a nice reference article on autoloading/eager loading:
    # http://blog.arkency.com/2014/11/dont-forget-about-eager-load-when-extending-autoload
L
Lin Jen-Shin 已提交
44
    config.eager_load_paths.push(*%W[#{config.root}/lib
45
                                     #{config.root}/app/models/badges
46
                                     #{config.root}/app/models/hooks
47
                                     #{config.root}/app/models/members
48
                                     #{config.root}/app/models/project_services
49
                                     #{config.root}/app/workers/concerns
50
                                     #{config.root}/app/policies/concerns
51
                                     #{config.root}/app/services/concerns
52
                                     #{config.root}/app/serializers/concerns
B
Bob Van Landuyt 已提交
53
                                     #{config.root}/app/finders/concerns
54 55
                                     #{config.root}/app/graphql/resolvers/concerns
                                     #{config.root}/app/graphql/mutations/concerns])
G
gitlabhq 已提交
56

57 58
    config.generators.templates.push("#{config.root}/generator_templates")

59 60 61 62
    # Rake tasks ignore the eager loading settings, so we need to set the
    # autoload paths explicitly
    config.autoload_paths = config.eager_load_paths.dup

G
gitlabhq 已提交
63 64 65 66 67 68 69
    # Only load the plugins named here, in the order given (default is alphabetical).
    # :all can be used as a placeholder for all plugins not explicitly named.
    # config.plugins = [ :exception_notification, :ssl_requirement, :all ]

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de
70
    config.i18n.enforce_available_locales = false
G
gitlabhq 已提交
71

J
Jan Provaznik 已提交
72 73 74 75 76 77
    # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
    # the I18n.default_locale when a translation can not be found).
    # We have to explicitly set default locale since 1.1.0 - see:
    # https://github.com/svenfuchs/i18n/pull/415
    config.i18n.fallbacks = [:en]

78 79 80
    # Translation for AR attrs is not working well for POROs like WikiPage
    config.gettext_i18n_rails.use_for_active_record_attributes = false

G
gitlabhq 已提交
81 82 83
    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

84 85 86 87 88
    # ActionCable mount point.
    # The default Rails' mount point is `/cable` which may conflict with existing
    # namespaces/users.
    # https://github.com/rails/rails/blob/5-0-stable/actioncable/lib/action_cable.rb#L38
    # Please change this value when configuring ActionCable for real usage.
89
    config.action_cable.mount_path = "/-/cable" if rails5?
90

G
gitlabhq 已提交
91
    # Configure sensitive parameters which will be filtered from the log file.
92
    #
93
    # Parameters filtered:
94
    # - Any parameter ending with `token`
95 96
    # - Any parameter containing `password`
    # - Any parameter containing `secret`
97
    # - Any parameter ending with `key`
98 99
    # - Two-factor tokens (:otp_attempt)
    # - Repo/Project Import URLs (:import_url)
100
    # - Build traces (:trace)
101 102 103 104
    # - Build variables (:variables)
    # - GitLab Pages SSL cert/key info (:certificate, :encrypted_key)
    # - Webhook URLs (:hook)
    # - Sentry DSN (:sentry_dsn)
105
    # - File content from Web Editor (:content)
106 107 108
    #
    # NOTE: It is **IMPORTANT** to also update gitlab-workhorse's filter when adding parameters here to not
    #       introduce another security vulnerability: https://gitlab.com/gitlab-org/gitlab-workhorse/issues/182
109
    config.filter_parameters += [/token$/, /password/, /secret/, /key$/]
110 111 112 113 114 115 116
    config.filter_parameters += %i(
      certificate
      encrypted_key
      hook
      import_url
      otp_attempt
      sentry_dsn
117
      trace
118
      variables
119
      content
120
    )
G
gitlabhq 已提交
121

R
Riyad Preukschas 已提交
122 123 124 125 126 127 128 129
    # Enable escaping HTML in JSON.
    config.active_support.escape_html_entities_in_json = true

    # Use SQL instead of Active Record's schema dumper when creating the database.
    # This is necessary if your schema can't be completely dumped by the schema dumper,
    # like if you have constraints or database-specific column types
    # config.active_record.schema_format = :sql

130 131 132 133
    # Configure webpack
    config.webpack.config_file = "config/webpack.config.js"
    config.webpack.output_dir  = "public/assets/webpack"
    config.webpack.public_path = "assets/webpack"
134 135

    # Webpack dev server configuration is handled in initializers/static_files.rb
136
    config.webpack.dev_server.enabled = false
137

G
gitlabhq 已提交
138 139
    # Enable the asset pipeline
    config.assets.enabled = true
140

141 142
    # Support legacy unicode file named img emojis, `1F939.png`
    config.assets.paths << Gemojione.images_path
143 144
    config.assets.paths << "#{config.root}/vendor/assets/fonts"

145
    config.assets.precompile << "print.css"
D
Douwe Maan 已提交
146
    config.assets.precompile << "notify.css"
R
Robert Speicher 已提交
147
    config.assets.precompile << "mailers/*.css"
148
    config.assets.precompile << "page_bundles/ide.css"
F
Filipa Lacerda 已提交
149
    config.assets.precompile << "page_bundles/xterm.css"
150
    config.assets.precompile << "performance_bar.css"
151
    config.assets.precompile << "lib/ace.js"
152
    config.assets.precompile << "test.css"
H
haseeb 已提交
153
    config.assets.precompile << "snippets.css"
154
    config.assets.precompile << "locale/**/app.js"
T
Tim Zallmann 已提交
155
    config.assets.precompile << "emoji_sprites.css"
P
Paul Slaughter 已提交
156
    config.assets.precompile << "errors.css"
C
Clement Ho 已提交
157
    config.assets.precompile << "csslab.css"
158

159
    # Import gitlab-svgs directly from vendored directory
160
    config.assets.paths << "#{config.root}/node_modules/@gitlab/svgs/dist"
161 162 163 164
    config.assets.precompile << "icons.svg"
    config.assets.precompile << "icons.json"
    config.assets.precompile << "illustrations/*.svg"

F
Filipa Lacerda 已提交
165 166
    # Import css for xterm
    config.assets.paths << "#{config.root}/node_modules/xterm/src/"
167 168
    config.assets.precompile << "xterm.css"

G
gitlabhq 已提交
169 170
    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'
171

172 173
    config.action_view.sanitized_allowed_protocols = %w(smb)

174 175 176
    # Nokogiri is significantly faster and uses less memory than REXML
    ActiveSupport::XmlMini.backend = 'Nokogiri'

177 178
    # This middleware needs to precede ActiveRecord::QueryCache and other middlewares that
    # connect to the database.
J
Jan Provaznik 已提交
179
    config.middleware.insert_after Rails::Rack::Logger, ::Gitlab::Middleware::BasicHealthCheck
180

M
Michael Kozono 已提交
181
    config.middleware.insert_after Warden::Manager, Rack::Attack
182 183

    # Allow access to GitLab API from other domains
184
    config.middleware.insert_before Warden::Manager, Rack::Cors do
185 186 187 188 189 190
      allow do
        origins Gitlab.config.gitlab.url
        resource '/api/*',
          credentials: true,
          headers: :any,
          methods: :any,
191
          expose: ['Link', 'X-Total', 'X-Total-Pages', 'X-Per-Page', 'X-Page', 'X-Next-Page', 'X-Prev-Page']
192 193 194
      end

      # Cross-origin requests must not have the session cookie available
195 196
      allow do
        origins '*'
F
Fred Chasen 已提交
197
        resource '/api/*',
198
          credentials: false,
F
Fred Chasen 已提交
199
          headers: :any,
200
          methods: :any,
201
          expose: ['Link', 'X-Total', 'X-Total-Pages', 'X-Per-Page', 'X-Page', 'X-Next-Page', 'X-Prev-Page']
202 203
      end
    end
204

205 206 207 208
    # Use caching across all environments
    caching_config_hash = Gitlab::Redis::Cache.params
    caching_config_hash[:namespace] = Gitlab::Redis::Cache::CACHE_NAMESPACE
    caching_config_hash[:expires_in] = 2.weeks # Cache should not grow forever
209
    if Sidekiq.server? # threaded context
210 211
      caching_config_hash[:pool_size] = Sidekiq.options[:concurrency] + 5
      caching_config_hash[:pool_timeout] = 1
212
    end
213

214
    config.cache_store = :redis_store, caching_config_hash
D
Dmitriy Zaporozhets 已提交
215

J
Jan Provaznik 已提交
216
    config.active_record.raise_in_transactional_callbacks = true unless rails5?
V
Valery Sizov 已提交
217 218 219

    config.active_job.queue_adapter = :sidekiq

D
Dmitriy Zaporozhets 已提交
220 221
    # This is needed for gitlab-shell
    ENV['GITLAB_PATH_OUTSIDE_HOOK'] = ENV['PATH']
222
    ENV['GIT_TERMINAL_PROMPT'] = '0'
223

224
    # GitLab Read-only middleware support
225
    config.middleware.insert_after ActionDispatch::Flash, ::Gitlab::Middleware::ReadOnly
T
Toon Claes 已提交
226

227
    config.generators do |g|
228
      g.factory_bot false
229
    end
230 231 232 233 234

    config.after_initialize do
      Rails.application.reload_routes!

      project_url_helpers = Module.new do
235 236 237
        extend ActiveSupport::Concern

        Gitlab::Application.routes.named_routes.helper_names.each do |name|
238 239 240
          next unless name.include?('namespace_project')

          define_method(name.sub('namespace_project', 'project')) do |project, *args|
L
Lin Jen-Shin 已提交
241
            send(name, project&.namespace, project, *args)
242 243 244 245
          end
        end
      end

246 247 248
      # We add the MilestonesRoutingHelper because we know that this does not
      # conflict with the methods defined in `project_url_helpers`, and we want
      # these methods available in the same places.
249
      Gitlab::Routing.add_helpers(project_url_helpers)
250
      Gitlab::Routing.add_helpers(MilestonesRoutingHelper)
251
    end
G
gitlabhq 已提交
252 253
  end
end