api.rb 5.0 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
    LOG_FILENAME = Rails.root.join("log", "api_json.log")

7 8 9 10
    NO_SLASH_URL_PART_REGEX = %r{[^/]+}
    PROJECT_ENDPOINT_REQUIREMENTS = { id: NO_SLASH_URL_PART_REGEX }.freeze
    COMMIT_ENDPOINT_REQUIREMENTS = PROJECT_ENDPOINT_REQUIREMENTS.merge(sha: NO_SLASH_URL_PART_REGEX).freeze

11
    use GrapeLogging::Middleware::RequestLogger,
12 13
        logger: Logger.new(LOG_FILENAME),
        formatter: Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp.new,
S
Stan Hu 已提交
14 15
        include: [
          GrapeLogging::Loggers::FilterParameters.new,
16 17
          GrapeLogging::Loggers::ClientEnv.new,
          Gitlab::GrapeLogging::Loggers::UserLogger.new
S
Stan Hu 已提交
18
        ]
19

20
    allow_access_with_scope :api
21
    prefix :api
22

O
Oswaldo Ferreira 已提交
23 24 25
    version %w(v3 v4), using: :path

    version 'v3', using: :path do
26
      helpers ::API::V3::Helpers
27
      helpers ::API::Helpers::CommonHelpers
28

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

65 66 67 68
    before do
      header['X-Frame-Options'] = 'SAMEORIGIN'
      header['X-Content-Type-Options'] = 'nosniff'
    end
69

70
    # The locale is set to the current user's locale when `current_user` is loaded
71
    after { Gitlab::I18n.use_default_locale }
72

73 74 75 76
    rescue_from Gitlab::Access::AccessDeniedError do
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
    end

N
Nihad Abbasov 已提交
77
    rescue_from ActiveRecord::RecordNotFound do
78
      rack_response({ 'message' => '404 Not found' }.to_json, 404)
N
Nihad Abbasov 已提交
79 80
    end

C
Connor Shea 已提交
81
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
82 83 84 85 86
    # 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 已提交
87 88
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
89 90
    end

91
    rescue_from Gitlab::Auth::TooManyIps do |e|
92
      rack_response({ 'message' => '403 Forbidden' }.to_json, 403)
93 94
    end

F
Felix Gilcher 已提交
95
    rescue_from :all do |exception|
S
Stan Hu 已提交
96
      handle_api_exception(exception)
97 98
    end

N
Nihad Abbasov 已提交
99
    format :json
100 101
    content_type :txt, "text/plain"

102
    # Ensure the namespace is right, otherwise we might load Grape::API::Helpers
S
Stan Hu 已提交
103
    helpers ::SentryHelper
104
    helpers ::API::Helpers
105
    helpers ::API::Helpers::CommonHelpers
106

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

    route :any, '*path' do
170
      error!('404 Not Found', 404)
171
    end
N
Nihad Abbasov 已提交
172
  end
173
end