application.rb 8.95 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1 2 3
require File.expand_path('../boot', __FILE__)

require 'rails/all'
4

5
Bundler.require(:default, Rails.env)
gitlabhq's avatar
gitlabhq committed
6

7 8
require 'elasticsearch/rails/instrumentation'

gitlabhq's avatar
gitlabhq committed
9 10
module Gitlab
  class Application < Rails::Application
11
    require_dependency Rails.root.join('lib/gitlab/redis/wrapper')
12 13 14
    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')
15
    require_dependency Rails.root.join('lib/gitlab/request_context')
16
    require_dependency Rails.root.join('lib/gitlab/current_settings')
17

gitlabhq's avatar
gitlabhq committed
18 19 20 21
    # 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.

22 23 24 25 26 27 28 29
    # 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
30
    config.eager_load_paths.push(*%W[#{config.root}/lib
31
                                     #{config.root}/app/models/hooks
32
                                     #{config.root}/app/models/members
33
                                     #{config.root}/app/models/project_services
34
                                     #{config.root}/app/workers/concerns
35
                                     #{config.root}/app/services/concerns
36
                                     #{config.root}/app/serializers/concerns
37
                                     #{config.root}/app/finders/concerns])
gitlabhq's avatar
gitlabhq committed
38

39 40
    config.generators.templates.push("#{config.root}/generator_templates")

41
    ## EE-specific paths config START
42 43 44 45 46
    ee_paths = config.eager_load_paths.each_with_object([]) do |path, memo|
      ee_path = config.root.join('ee', Pathname.new(path).relative_path_from(config.root))
      memo << ee_path.to_s if ee_path.exist?
    end
    config.eager_load_paths.concat(ee_paths)
47

48 49 50
    config.paths['lib/tasks'] << "#{config.root}/ee/lib/tasks"
    config.paths['app/views'] << "#{config.root}/ee/app/views"
    config.helpers_paths << "#{config.root}/ee/app/helpers"
51
    ## EE-specific paths config END
Clement Ho's avatar
Clement Ho committed
52

Nick Thomas's avatar
Nick Thomas committed
53 54 55 56
    # Rake tasks ignore the eager loading settings, so we need to set the
    # autoload paths explicitly
    config.autoload_paths = config.eager_load_paths.dup

gitlabhq's avatar
gitlabhq committed
57 58 59 60 61 62 63
    # 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
64
    config.i18n.enforce_available_locales = false
gitlabhq's avatar
gitlabhq committed
65

66 67 68
    # Translation for AR attrs is not working well for POROs like WikiPage
    config.gettext_i18n_rails.use_for_active_record_attributes = false

gitlabhq's avatar
gitlabhq committed
69 70 71 72
    # Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

    # Configure sensitive parameters which will be filtered from the log file.
73
    #
74
    # Parameters filtered:
75
    # - Any parameter ending with `token`
76 77
    # - Any parameter containing `password`
    # - Any parameter containing `secret`
78 79
    # - Two-factor tokens (:otp_attempt)
    # - Repo/Project Import URLs (:import_url)
80
    # - Build traces (:trace)
81 82 83 84 85
    # - Build variables (:variables)
    # - GitLab Pages SSL cert/key info (:certificate, :encrypted_key)
    # - Webhook URLs (:hook)
    # - Sentry DSN (:sentry_dsn)
    # - Deploy keys (:key)
86
    # - Secret variable values (:value)
87
    config.filter_parameters += [/token$/, /password/, /secret/]
88 89 90 91 92 93 94 95
    config.filter_parameters += %i(
      certificate
      encrypted_key
      hook
      import_url
      key
      otp_attempt
      sentry_dsn
96
      trace
97
      variables
98
      value
99
    )
gitlabhq's avatar
gitlabhq committed
100

Riyad Preukschas's avatar
Riyad Preukschas committed
101 102 103 104 105 106 107 108
    # 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

109 110 111 112
    # Configure webpack
    config.webpack.config_file = "config/webpack.config.js"
    config.webpack.output_dir  = "public/assets/webpack"
    config.webpack.public_path = "assets/webpack"
113 114

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

gitlabhq's avatar
gitlabhq committed
117 118
    # Enable the asset pipeline
    config.assets.enabled = true
119

120 121
    # Support legacy unicode file named img emojis, `1F939.png`
    config.assets.paths << Gemojione.images_path
122
    config.assets.paths << "#{config.root}/vendor/assets/fonts"
123

124
    config.assets.precompile << "print.css"
125
    config.assets.precompile << "notify.css"
126
    config.assets.precompile << "mailers/*.css"
127
    config.assets.precompile << "xterm/xterm.css"
128
    config.assets.precompile << "performance_bar.css"
129
    config.assets.precompile << "lib/ace.js"
130
    config.assets.precompile << "test.css"
131
    config.assets.precompile << "locale/**/app.js"
132

133 134 135 136 137 138 139 140 141 142 143 144 145 146
    ## EE-specific assets config START
    %w[images javascripts stylesheets].each do |path|
      config.assets.paths << "#{config.root}/ee/app/assets/#{path}"
    end

    # Compile non-JS/CSS assets in the ee/app/assets folder by default
    # Mimic sprockets-rails default: https://github.com/rails/sprockets-rails/blob/v3.2.1/lib/sprockets/railtie.rb#L84-L87
    LOOSE_EE_APP_ASSETS = lambda do |logical_path, filename|
      filename.start_with?(config.root.join("ee/app/assets").to_s) &&
        !['.js', '.css', ''].include?(File.extname(logical_path))
    end
    config.assets.precompile << LOOSE_EE_APP_ASSETS
    ## EE-specific assets config END

gitlabhq's avatar
gitlabhq committed
147 148
    # Version of your assets, change this if you want to expire all your assets
    config.assets.version = '1.0'
149

150 151
    config.action_view.sanitized_allowed_protocols = %w(smb)

152
    config.middleware.insert_after Warden::Manager, Rack::Attack
153 154

    # Allow access to GitLab API from other domains
155 156 157 158 159 160 161
    config.middleware.insert_before Warden::Manager, Rack::Cors do
      allow do
        origins Gitlab.config.gitlab.url
        resource '/api/*',
          credentials: true,
          headers: :any,
          methods: :any,
162
          expose: ['Link', 'X-Total', 'X-Total-Pages', 'X-Per-Page', 'X-Page', 'X-Next-Page', 'X-Prev-Page']
163 164 165
      end

      # Cross-origin requests must not have the session cookie available
166 167
      allow do
        origins '*'
Fred Chasen's avatar
Fred Chasen committed
168
        resource '/api/*',
169
          credentials: false,
Fred Chasen's avatar
Fred Chasen committed
170
          headers: :any,
171
          methods: :any,
172
          expose: ['Link', 'X-Total', 'X-Total-Pages', 'X-Per-Page', 'X-Page', 'X-Next-Page', 'X-Prev-Page']
173 174
      end
    end
175

176 177 178 179
    # 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
180
    if Sidekiq.server? # threaded context
181 182
      caching_config_hash[:pool_size] = Sidekiq.options[:concurrency] + 5
      caching_config_hash[:pool_timeout] = 1
183
    end
184

185
    config.cache_store = :redis_store, caching_config_hash
186

187 188 189 190
    config.active_record.raise_in_transactional_callbacks = true

    config.active_job.queue_adapter = :sidekiq

191 192
    # This is needed for gitlab-shell
    ENV['GITLAB_PATH_OUTSIDE_HOOK'] = ENV['PATH']
193
    ENV['GIT_TERMINAL_PROMPT'] = '0'
194

195 196
    # Gitlab Read-only middleware support
    config.middleware.insert_after ActionDispatch::Flash, 'Gitlab::Middleware::ReadOnly'
197

198
    config.generators do |g|
199
      g.factory_bot false
200
    end
201 202 203 204 205

    config.after_initialize do
      Rails.application.reload_routes!

      project_url_helpers = Module.new do
206 207 208
        extend ActiveSupport::Concern

        Gitlab::Application.routes.named_routes.helper_names.each do |name|
209 210 211
          next unless name.include?('namespace_project')

          define_method(name.sub('namespace_project', 'project')) do |project, *args|
212
            send(name, project&.namespace, project, *args) # rubocop:disable GitlabSecurity/PublicSend
213 214 215 216
          end
        end
      end

217 218 219
      # 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.
220
      Gitlab::Routing.add_helpers(project_url_helpers)
221
      Gitlab::Routing.add_helpers(MilestonesRoutingHelper)
222
    end
gitlabhq's avatar
gitlabhq committed
223 224
  end
end