api.rb 4.7 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2
  class API < Grape::API
V
Valery Sizov 已提交
3
    include APIGuard
O
Oswaldo Ferreira 已提交
4

5 6 7 8 9 10 11 12 13
    LOG_FILENAME = Rails.root.join("log", "api_json.log")

    use GrapeLogging::Middleware::RequestLogger,
        logger: ::Gitlab::ApiLogger.new(LOG_FILENAME),
        formatter: GrapeLogging::Formatters::Json.new,
        include: [ GrapeLogging::Loggers::Response.new,
                   GrapeLogging::Loggers::FilterParameters.new,
                   GrapeLogging::Loggers::ClientEnv.new ]

14
    allow_access_with_scope :api
15
    prefix :api
16

O
Oswaldo Ferreira 已提交
17 18 19
    version %w(v3 v4), using: :path

    version 'v3', using: :path do
20
      helpers ::API::V3::Helpers
21
      helpers ::API::Helpers::CommonHelpers
22

R
Robert Schilling 已提交
23
      mount ::API::V3::AwardEmoji
R
Robert Schilling 已提交
24 25
      mount ::API::V3::Boards
      mount ::API::V3::Branches
R
Robert Schilling 已提交
26
      mount ::API::V3::BroadcastMessages
27
      mount ::API::V3::Builds
28
      mount ::API::V3::Commits
29
      mount ::API::V3::DeployKeys
R
Robert Schilling 已提交
30
      mount ::API::V3::Environments
31
      mount ::API::V3::Files
R
Robert Schilling 已提交
32
      mount ::API::V3::Groups
33
      mount ::API::V3::Issues
R
Robert Schilling 已提交
34
      mount ::API::V3::Labels
35
      mount ::API::V3::Members
R
Robert Schilling 已提交
36
      mount ::API::V3::MergeRequestDiffs
37
      mount ::API::V3::MergeRequests
38
      mount ::API::V3::Notes
39
      mount ::API::V3::Pipelines
R
Robert Schilling 已提交
40
      mount ::API::V3::ProjectHooks
J
Jarka Kadlecova 已提交
41
      mount ::API::V3::Milestones
O
Oswaldo Ferreira 已提交
42
      mount ::API::V3::Projects
43
      mount ::API::V3::ProjectSnippets
R
Robert Schilling 已提交
44
      mount ::API::V3::Repositories
R
Robert Schilling 已提交
45 46
      mount ::API::V3::Runners
      mount ::API::V3::Services
47
      mount ::API::V3::Settings
48
      mount ::API::V3::Snippets
49
      mount ::API::V3::Subscriptions
R
Robert Schilling 已提交
50 51
      mount ::API::V3::SystemHooks
      mount ::API::V3::Tags
52
      mount ::API::V3::Templates
R
Robert Schilling 已提交
53 54
      mount ::API::V3::Todos
      mount ::API::V3::Triggers
R
Robert Schilling 已提交
55
      mount ::API::V3::Users
R
Robert Schilling 已提交
56
      mount ::API::V3::Variables
O
Oswaldo Ferreira 已提交
57
    end
N
Nihad Abbasov 已提交
58

59
    before { header['X-Frame-Options'] = 'SAMEORIGIN' }
60

61
    # The locale is set to the current user's locale when `current_user` is loaded
62
    after { Gitlab::I18n.use_default_locale }
63

64 65 66 67
    rescue_from Gitlab::Access::AccessDeniedError do
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
    end

N
Nihad Abbasov 已提交
68
    rescue_from ActiveRecord::RecordNotFound do
69
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
N
Nihad Abbasov 已提交
70 71
    end

C
Connor Shea 已提交
72
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
73 74 75 76 77
    # https://github.com/ruby-grape/grape/blob/a3a28f5b5dfbb2797442e006dbffd750b27f2a76/UPGRADING.md#changes-to-method-not-allowed-routes
    rescue_from Grape::Exceptions::MethodNotAllowed do |e|
      error! e.message, e.status, e.headers
    end

C
Connor Shea 已提交
78 79
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
80 81
    end

82
    rescue_from Gitlab::Auth::TooManyIps do |e|
83
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
84 85
    end

F
Felix Gilcher 已提交
86
    rescue_from :all do |exception|
S
Stan Hu 已提交
87
      handle_api_exception(exception)
88 89
    end

N
Nihad Abbasov 已提交
90
    format :json
91 92
    content_type :txt, "text/plain"

93
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
S
Stan Hu 已提交
94
    helpers ::SentryHelper
95
    helpers ::API::Helpers
96
    helpers ::API::Helpers::CommonHelpers
97

98 99 100
    NO_SLASH_URL_PART_REGEX = %r{[^/]+}
    PROJECT_ENDPOINT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze

101
    # Keep in alphabetical order
102
    mount ::API::AccessRequests
103
    mount ::API::AwardEmoji
R
Robert Schilling 已提交
104
    mount ::API::Boards
105
    mount ::API::Branches
106
    mount ::API::BroadcastMessages
107
    mount ::API::CircuitBreakers
108
    mount ::API::Commits
R
Robert Schilling 已提交
109
    mount ::API::CommitStatuses
110
    mount ::API::DeployKeys
Z
Z.J. van de Weg 已提交
111
    mount ::API::Deployments
112
    mount ::API::Environments
M
Mark Fletcher 已提交
113
    mount ::API::Events
114
    mount ::API::Features
115 116 117
    mount ::API::Files
    mount ::API::Groups
    mount ::API::Internal
118
    mount ::API::Issues
119
    mount ::API::Jobs
120
    mount ::API::JobArtifacts
121 122
    mount ::API::Keys
    mount ::API::Labels
123
    mount ::API::Lint
124
    mount ::API::Members
125
    mount ::API::MergeRequestDiffs
R
Robert Schilling 已提交
126
    mount ::API::MergeRequests
F
Felipe Artur 已提交
127 128
    mount ::API::ProjectMilestones
    mount ::API::GroupMilestones
129
    mount ::API::Namespaces
130
    mount ::API::Notes
131
    mount ::API::NotificationSettings
Z
Z.J. van de Weg 已提交
132
    mount ::API::Pipelines
133
    mount ::API::PipelineSchedules
134
    mount ::API::ProjectHooks
135
    mount ::API::Projects
R
Robert Schilling 已提交
136
    mount ::API::ProjectSnippets
E
Eric 已提交
137
    mount ::API::ProtectedBranches
138
    mount ::API::Repositories
T
Tomasz Maczukin 已提交
139
    mount ::API::Runner
140
    mount ::API::Runners
141
    mount ::API::Services
142
    mount ::API::Session
143
    mount ::API::Settings
144
    mount ::API::SidekiqMetrics
145
    mount ::API::Snippets
146 147
    mount ::API::Subscriptions
    mount ::API::SystemHooks
148
    mount ::API::Tags
Z
ZJ van de Weg 已提交
149
    mount ::API::Templates
D
Douglas Barbosa Alexandre 已提交
150
    mount ::API::Todos
151
    mount ::API::Triggers
152
    mount ::API::Users
153
    mount ::API::Variables
S
Ini  
Shinya Maeda 已提交
154
    mount ::API::GroupVariables
R
Robert Schilling 已提交
155
    mount ::API::Version
156 157

    route :any, '*path' do
158
      error!('404 Not Found', 404)
159
    end
N
Nihad Abbasov 已提交
160
  end
161
end