Skip to content

Commit

Permalink
use apicast.loader to load policies
Browse files Browse the repository at this point in the history
instead of using a package.path
  • Loading branch information
mikz committed Dec 18, 2017
1 parent 38aac81 commit c2a891e
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 12 deletions.
3 changes: 1 addition & 2 deletions .busted
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions gateway/bin/apicast
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 1 addition & 2 deletions gateway/conf/nginx.conf.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -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: " " }};
Expand Down
2 changes: 2 additions & 0 deletions gateway/http.d/init.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 2 additions & 4 deletions gateway/libexec/boot.lua
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
9 changes: 9 additions & 0 deletions gateway/src/apicast/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -55,4 +63,5 @@ local function apicast_namespace(name)
end
end

table.insert(package.searchers, policy_namespace)
table.insert(package.searchers, apicast_namespace)
2 changes: 2 additions & 0 deletions gateway/src/apicast/policy_chain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions t/TestAPIcast.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit c2a891e

Please sign in to comment.