From c2a891ee885646becd1c18394f36fb92ffd1aedd Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Mon, 18 Dec 2017 15:11:30 +0100 Subject: [PATCH] use apicast.loader to load policies instead of using a package.path --- .busted | 3 +-- gateway/bin/apicast | 3 +-- gateway/conf/nginx.conf.liquid | 3 +-- gateway/http.d/init.conf | 2 ++ gateway/libexec/boot.lua | 6 ++---- gateway/src/apicast/loader.lua | 9 +++++++++ gateway/src/apicast/policy_chain.lua | 2 ++ t/TestAPIcast.pm | 3 +-- 8 files changed, 19 insertions(+), 12 deletions(-) diff --git a/.busted b/.busted index b1ab83a81..ed0ed5dbd 100644 --- a/.busted +++ b/.busted @@ -11,8 +11,7 @@ local default = { ['shuffle-tests'] = true, ['shuffle-files'] = true, - -- src/?/policy.lua allows us to require apicast.policy.apolicy - lpath = path.join(root, 'spec/?.lua;') .. path.join(root, 'gateway/src/?.lua;gateway/src/?/policy.lua'), + lpath = path.join(root, 'spec/?.lua;') .. path.join(root, 'gateway/src/?.lua;'), } if ci then diff --git a/gateway/bin/apicast b/gateway/bin/apicast index 71c131f1c..2b549c4d4 100755 --- a/gateway/bin/apicast +++ b/gateway/bin/apicast @@ -65,8 +65,7 @@ if ($rover && !$lua_path) { $ENV{APICAST_DIR} = $apicast_conf; -# src/?/policy.lua allows us to require apicast.policy.apolicy -$ENV{LUA_PATH} = sprintf('%1$s/?.lua;%1$s/?/policy.lua;', $apicast_src) . $lua_path; +$ENV{LUA_PATH} = sprintf('%1$s/?.lua;', $apicast_src) . $lua_path; $ENV{PWD} = $cwd; my $bin = "$apicast_bin/cli"; diff --git a/gateway/conf/nginx.conf.liquid b/gateway/conf/nginx.conf.liquid index 636a72044..baf5640af 100644 --- a/gateway/conf/nginx.conf.liquid +++ b/gateway/conf/nginx.conf.liquid @@ -42,8 +42,7 @@ http { log_format time '[$time_local] $host:$server_port $remote_addr:$remote_port "$request" $status $body_bytes_sent ($request_time) $post_action_impact'; access_log off; - # src/?/policy.lua allows us to require apicast.policy.apolicy - lua_package_path ";;{{prefix}}/?.lua;{{prefix}}/src/?.lua;{{prefix}}/src/?/policy.lua"; + lua_package_path ";;{{prefix}}/?.lua;{{prefix}}/src/?.lua;"; {% if nameservers %} resolver {{ nameservers | join: " " }}; diff --git a/gateway/http.d/init.conf b/gateway/http.d/init.conf index 6968f28c2..4095bf0ef 100644 --- a/gateway/http.d/init.conf +++ b/gateway/http.d/init.conf @@ -6,6 +6,8 @@ init_by_lua_block { require("resty.core") require('resty.resolver').init() + require('apicast.loader') + local env = require('apicast.cli.environment').load() local context = env:context() diff --git a/gateway/libexec/boot.lua b/gateway/libexec/boot.lua index feb1077c7..41764b7c6 100644 --- a/gateway/libexec/boot.lua +++ b/gateway/libexec/boot.lua @@ -1,7 +1,5 @@ -pcall(require, 'luarocks.loader') - --- src/?/policy.lua allows us to require apicast.policy.apolicy -package.path = package.path .. ";./src/?.lua;./src/?/policy.lua" +package.path = package.path .. ";./src/?.lua;" +require('apicast.loader') local configuration = require 'apicast.configuration_loader' diff --git a/gateway/src/apicast/loader.lua b/gateway/src/apicast/loader.lua index eed456a0a..f5009cd79 100644 --- a/gateway/src/apicast/loader.lua +++ b/gateway/src/apicast/loader.lua @@ -43,6 +43,14 @@ local function rename_loader(name, path) return found or err end +local function policy_namespace(name) + local policy = name .. '.policy' + + local found, err = loader(policy, package.path) + + return found or err +end + local function apicast_namespace(name) local path = package.path @@ -55,4 +63,5 @@ local function apicast_namespace(name) end end +table.insert(package.searchers, policy_namespace) table.insert(package.searchers, apicast_namespace) diff --git a/gateway/src/apicast/policy_chain.lua b/gateway/src/apicast/policy_chain.lua index b62e20741..442868527 100644 --- a/gateway/src/apicast/policy_chain.lua +++ b/gateway/src/apicast/policy_chain.lua @@ -13,6 +13,8 @@ local require = require local insert = table.insert local noop = function() end +require('apicast.loader') + local linked_list = require('apicast.linked_list') local policy_phases = require('apicast.policy').phases diff --git a/t/TestAPIcast.pm b/t/TestAPIcast.pm index 8d2233862..da0c1fccb 100644 --- a/t/TestAPIcast.pm +++ b/t/TestAPIcast.pm @@ -16,8 +16,7 @@ our $path = $ENV{TEST_NGINX_APICAST_PATH} ||= "$pwd/gateway"; our $spec = "$pwd/spec"; our $servroot = $Test::Nginx::Util::ServRoot; -# src/?/policy.lua allows us to require apicast.policy.apolicy -$ENV{TEST_NGINX_LUA_PATH} = "$path/src/?.lua;$path/src/?/policy.lua;;"; +$ENV{TEST_NGINX_LUA_PATH} = "$path/src/?.lua;;"; $ENV{TEST_NGINX_MANAGEMENT_CONFIG} = "$path/conf.d/management.conf"; $ENV{TEST_NGINX_UPSTREAM_CONFIG} = "$path/http.d/upstream.conf"; $ENV{TEST_NGINX_BACKEND_CONFIG} = "$path/conf.d/backend.conf";