Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: allowed to use different router. #364

Merged
merged 4 commits into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feature: allowed to use different router.
  • Loading branch information
membphis committed Aug 4, 2019
commit 337d6ceda0d2ef01793c0cd67ecda1a0b17360ba
8 changes: 4 additions & 4 deletions conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ apisix:
enable_debug: false
allow_admin: # http://nginx.org/en/docs/http/ngx_http_access_module.html#allow
- 127.0.0.0/24
# port_admin: 9180 # use a separate port
real_ip_header: "X-Real-IP" # http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_header
real_ip_from: # http://nginx.org/en/docs/http/ngx_http_realip_module.html#set_real_ip_from
- 127.0.0.1
- 'unix:'
# port_admin: 9180 # use a separate port
route_idx: 'uri' # how to create the route index:
# `uri`: only use `uri` for routing
# `host+uri`: use `host+uri` for routing
router:
http: 'r3_uri'
ssl: 'r3_sni'

etcd:
host: "http://127.0.0.1:2379" # etcd address
Expand Down
36 changes: 5 additions & 31 deletions lua/apisix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@

local require = require
local core = require("apisix.core")
local router = require("apisix.http.route").get
local plugin = require("apisix.plugin")
local service_fetch = require("apisix.http.service").get
local ssl_match = require("apisix.http.ssl").match
local admin_init = require("apisix.admin.init")
local get_var = require("resty.ngxvar").fetch
local local_conf = core.config.local_conf
local router = require("apisix.http.router")
local ngx = ngx
local get_method = ngx.req.get_method
local ngx_exit = ngx.exit
local ngx_ERROR = ngx.ERROR
local str_reverse = string.reverse
local math = math
local match_opts = {}
local error = error
local load_balancer


local _M = {version = 0.1}
local _M = {version = 0.2}


function _M.http_init()
Expand Down Expand Up @@ -57,10 +53,8 @@ function _M.http_init_worker()

require("apisix.admin.init").init_worker()

require("apisix.http.route").init_worker()
router.init_worker()
require("apisix.http.service").init_worker()
require("apisix.http.ssl").init_worker()

require("apisix.plugin").init_worker()
require("apisix.consumer").init_worker()
end
Expand Down Expand Up @@ -133,7 +127,7 @@ function _M.http_ssl_phase()
ngx_ctx.api_ctx = api_ctx
end

local ok, err = ssl_match(api_ctx)
local ok, err = router.router_ssl.match(api_ctx)
if not ok then
if err then
core.log.error("failed to fetch ssl config: ", err)
Expand All @@ -153,28 +147,8 @@ function _M.http_access_phase()
end

core.ctx.set_vars_meta(api_ctx)
core.table.clear(match_opts)
match_opts.method = api_ctx.var.method

local ok

if local_conf().apisix
and local_conf().apisix.route_idx == "host+uri" then
local host = api_ctx.var.host
host = host and str_reverse(host) or "[^/]+"
host = host .. api_ctx.var.uri
ok = router():dispatch2(nil, host, match_opts, api_ctx)
core.log.info("match string: ", host)

else
match_opts.host = api_ctx.var.host
ok = router():dispatch2(nil, api_ctx.var.uri, match_opts, api_ctx)
end

if not ok then
core.log.info("not find any matched route")
return core.response.exit(404)
end
router.router_http.match(api_ctx)

core.log.info("route: ",
core.json.delay_encode(api_ctx.matched_route, true))
Expand Down
6 changes: 3 additions & 3 deletions lua/apisix/admin/services.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local core = require("apisix.core")
local routes = require("apisix.http.route").routes
local get_routes = require("apisix.http.router").http_routes
local schema_plugin = require("apisix.admin.plugins").check_schema
local tostring = tostring
local ipairs = ipairs
Expand All @@ -8,7 +8,7 @@ local type = type


local _M = {
version = 0.1,
version = 0.2,
}


Expand Down Expand Up @@ -126,7 +126,7 @@ function _M.delete(id)
return 400, {error_msg = "missing service id"}
end

local routes, routes_ver = routes()
local routes, routes_ver = get_routes()
core.log.info("routes: ", core.json.delay_encode(routes, true))
core.log.info("routes_ver: ", routes_ver)
if routes_ver and routes then
Expand Down
4 changes: 2 additions & 2 deletions lua/apisix/admin/upstreams.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local core = require("apisix.core")
local get_routes = require("apisix.http.route").routes
local get_routes = require("apisix.http.router").http_routes
local get_services = require("apisix.http.service").services
local tostring = tostring
local ipairs = ipairs
Expand All @@ -8,7 +8,7 @@ local type = type


local _M = {
version = 0.1,
version = 0.2,
}


Expand Down
32 changes: 32 additions & 0 deletions lua/apisix/http/router.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
local core = require("apisix.core")
local local_conf = core.config.local_conf


local _M = {version = 0.1}


function _M.init_worker()
local conf = local_conf()
local router_http_name = "r3_uri"
local router_ssl_name = "r3_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)
router_http.init_worker()
_M.router_http = router_http

local router_ssl = require("apisix.http.router." .. router_ssl_name)
router_ssl:init_worker()
_M.router_ssl = router_ssl
end


function _M.http_routes()
return _M.router_http.routes()
end


return _M
4 changes: 2 additions & 2 deletions lua/apisix/http/ssl.lua → lua/apisix/http/router/r3_sni.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ function _M.match(api_ctx)
local r3, err = core.lrucache.global("/ssl", ssl.conf_version,
create_r3_router, ssl.values)
if not r3 then
return false, "gailed to fetch ssl router: " .. err
return false, "failed to fetch ssl router: " .. err
end

local sni = ngx_ssl.server_name()
if type(sni) ~= "string" then
return false, "gailed to fetch SNI: " .. err
return false, "failed to fetch SNI: " .. err
end

core.log.debug("sni: ", sni)
Expand Down
30 changes: 23 additions & 7 deletions lua/apisix/http/route.lua → lua/apisix/http/router/r3_uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local require = require
local r3router = require("resty.r3")
local core = require("apisix.core")
local plugin = require("apisix.plugin")
local local_conf = core.config.local_conf
local ipairs = ipairs
local type = type
local error = error
Expand Down Expand Up @@ -34,9 +35,8 @@ local function create_r3_router(routes)
end
end

local local_conf = core.config.local_conf()
local route_idx = local_conf and local_conf.apisix and
local_conf.apisix.route_idx
local conf = local_conf()
local route_idx = conf and conf.apisix and conf.apisix.route_idx

for _, route in ipairs(routes) do
if type(route) == "table" then
Expand Down Expand Up @@ -85,10 +85,26 @@ local function create_r3_router(routes)
end


function _M.get()
core.log.info("routes conf_version: ", routes.conf_version)
return core.lrucache.global("/routes", routes.conf_version,
create_r3_router, routes.values)
local match_opts = {}
function _M.match(api_ctx)
local router, err = core.lrucache.global("/routes", routes.conf_version,
create_r3_router, routes.values)
if not router then
core.log.error("failed to fetch http router: ", err)
return core.response.exit(404)
end

core.table.clear(match_opts)
match_opts.method = api_ctx.var.method
match_opts.host = api_ctx.var.host

local ok = router:dispatch2(nil, api_ctx.var.uri, match_opts, api_ctx)
if not ok then
core.log.info("not find any matched route")
return core.response.exit(404)
end

return true
end


Expand Down