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

    use GrapeLogging::Middleware::RequestLogger,
8 9
        logger: Logger.new(LOG_FILENAME),
        formatter: Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp.new,
S
Stan Hu 已提交
10 11 12 13
        include: [
          GrapeLogging::Loggers::FilterParameters.new,
          GrapeLogging::Loggers::ClientEnv.new
        ]
14

15
    allow_access_with_scope :api
16
    prefix :api
17

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

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

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

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

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

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

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

C
Connor Shea 已提交
73
    # Retain 405 error rather than a 500 error for Grape 0.15.0+.
74 75 76 77 78
    # 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 已提交
79 80
    rescue_from Grape::Exceptions::Base do |e|
      error! e.message, e.status, e.headers
81 82
    end

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

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

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

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

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

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

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