api.rb 4.9 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 16 17
        include: [
          GrapeLogging::Loggers::FilterParameters.new,
          GrapeLogging::Loggers::ClientEnv.new
        ]
18

19
    allow_access_with_scope :api
20
    prefix :api
21

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

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

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

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

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

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

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

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

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

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

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

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

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

    route :any, '*path' do
164
      error!('404 Not Found', 404)
165
    end
N
Nihad Abbasov 已提交
166
  end
167
end