Repository navigation
Expand file tree
/
Copy pathinit.lua
More file actions
149 lines (102 loc) · 3.56 KB
/
Copy pathinit.lua
File metadata and controls
149 lines (102 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
local lapis = require "lapis"
local utils = require "kong.tools.utils"
local tablex = require "pl.tablex"
local responses = require "kong.tools.responses"
local singletons = require "kong.singletons"
local app_helpers = require "lapis.application"
local api_helpers = require "kong.api.api_helpers"
local find = string.find
local app = lapis.Application()
local NEEDS_BODY = tablex.readonly({ PUT = 1, POST = 2, PATCH = 3 })
local function parse_params(fn)
return app_helpers.json_params(function(self, ...)
if NEEDS_BODY[ngx.req.get_method()] then
local content_type = self.req.headers["content-type"]
if content_type and find(content_type:lower(), "application/json",
nil, true) then
if not self.json then
return responses.send_HTTP_BAD_REQUEST("Cannot parse JSON body")
end
end
end
self.params = api_helpers.normalize_nested_params(self.params)
return fn(self, ...)
end)
end
local function on_error(self)
local err = self.errors[1]
if type(err) == "table" then
if err.db then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err.message)
elseif err.unique then
return responses.send_HTTP_CONFLICT(err.tbl)
elseif err.foreign then
return responses.send_HTTP_NOT_FOUND(err.tbl)
else
return responses.send_HTTP_BAD_REQUEST(err.tbl or err.message)
end
end
end
app.default_route = function(self)
local path = self.req.parsed_url.path:match("^(.*)/$")
if path and self.app.router:resolve(path, self) then
return
elseif self.app.router:resolve(self.req.parsed_url.path .. "/", self) then
return
end
return self.app.handle_404(self)
end
app.handle_404 = function(self)
return responses.send_HTTP_NOT_FOUND()
end
app.handle_error = function(self, err, trace)
if err and find(err, "don't know how to respond to", nil, true) then
return responses.send_HTTP_METHOD_NOT_ALLOWED()
end
ngx.log(ngx.ERR, err, "\n", trace)
-- We just logged the error so no need to give it to responses and log it
-- twice
return responses.send_HTTP_INTERNAL_SERVER_ERROR()
end
app:before_filter(function(self)
if NEEDS_BODY[ngx.req.get_method()]
and not self.req.headers["content-type"] then
return responses.send_HTTP_UNSUPPORTED_MEDIA_TYPE()
end
end)
local handler_helpers = {
responses = responses,
yield_error = app_helpers.yield_error
}
local function attach_routes(routes)
for route_path, methods in pairs(routes) do
methods.on_error = methods.on_error or on_error
for method_name, method_handler in pairs(methods) do
local wrapped_handler = function(self)
return method_handler(self, singletons.dao, handler_helpers)
end
methods[method_name] = parse_params(wrapped_handler)
end
app:match(route_path, route_path, app_helpers.respond_to(methods))
end
end
ngx.log(ngx.DEBUG, "Loading Admin API endpoints")
-- Load core routes
for _, v in ipairs({"kong", "apis", "consumers", "plugins", "cache",
"certificates", "snis", "upstreams"}) do
local routes = require("kong.api.routes." .. v)
attach_routes(routes)
end
-- Loading plugins routes
if singletons.configuration and singletons.configuration.plugins then
for k in pairs(singletons.configuration.plugins) do
local loaded, mod = utils.load_module_if_exists("kong.plugins." .. k .. ".api")
if loaded then
ngx.log(ngx.DEBUG, "Loading API endpoints for plugin: ", k)
attach_routes(mod)
else
ngx.log(ngx.DEBUG, "No API endpoints loaded for plugin: ", k)
end
end
end
return app