-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
router.lua
142 lines (111 loc) · 4.07 KB
/
router.lua
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
--
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local require = require
local http_route = require("apisix.http.route")
local apisix_upstream = require("apisix.upstream")
local core = require("apisix.core")
local plugin_checker = require("apisix.plugin").plugin_checker
local str_lower = string.lower
local error = error
local ipairs = ipairs
local _M = {version = 0.3}
local function filter(route)
route.orig_modifiedIndex = route.modifiedIndex
route.update_count = 0
route.has_domain = false
if not route.value then
return
end
if route.value.host then
route.value.host = str_lower(route.value.host)
elseif route.value.hosts then
for i, v in ipairs(route.value.hosts) do
route.value.hosts[i] = str_lower(v)
end
end
apisix_upstream.filter_upstream(route.value.upstream, route)
core.log.info("filter route: ", core.json.delay_encode(route, true))
end
-- attach common methods if the router doesn't provide its custom implementation
local function attach_http_router_common_methods(http_router)
if http_router.routes == nil then
http_router.routes = function ()
if not http_router.user_routes then
return nil, nil
end
local user_routes = http_router.user_routes
return user_routes.values, user_routes.conf_version
end
end
if http_router.init_worker == nil then
http_router.init_worker = function (filter)
http_router.user_routes = http_route.init_worker(filter)
end
end
end
function _M.http_init_worker()
local conf = core.config.local_conf()
local router_http_name = "radixtree_uri"
local router_ssl_name = "radixtree_sni"
if conf and conf.apisix and conf.apisix.router then
router_http_name = conf.apisix.router.http or router_http_name
router_ssl_name = conf.apisix.router.ssl or router_ssl_name
end
local router_http = require("apisix.http.router." .. router_http_name)
attach_http_router_common_methods(router_http)
router_http.init_worker(filter)
_M.router_http = router_http
local router_ssl = require("apisix.ssl.router." .. router_ssl_name)
router_ssl.init_worker()
_M.router_ssl = router_ssl
_M.api = require("apisix.api_router")
local global_rules, err = core.config.new("/global_rules", {
automatic = true,
item_schema = core.schema.global_rule,
checker = plugin_checker,
})
if not global_rules then
error("failed to create etcd instance for fetching /global_rules : "
.. err)
end
_M.global_rules = global_rules
end
function _M.stream_init_worker()
local router_ssl_name = "radixtree_sni"
local router_stream = require("apisix.stream.router.ip_port")
router_stream.stream_init_worker(filter)
_M.router_stream = router_stream
local router_ssl = require("apisix.ssl.router." .. router_ssl_name)
router_ssl.init_worker()
_M.router_ssl = router_ssl
end
function _M.ssls()
return _M.router_ssl.ssls()
end
function _M.http_routes()
return _M.router_http.routes()
end
function _M.stream_routes()
-- maybe it's not inited.
if not _M.router_stream then
return nil, nil
end
return _M.router_stream.routes()
end
-- for test
_M.filter_test = filter
return _M