Skip to content

Commit

Permalink
Merge pull request #532 from 3scale/apicast-luarocks-load-path
Browse files Browse the repository at this point in the history
use apicast.loader to load policies
  • Loading branch information
davidor authored Dec 21, 2017
2 parents d104e6f + aeb6614 commit 056428b
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 13 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Consolidate apicast-0.1-0.rockspec into apicast-scm-1.rockspec [PR #526](https://github.com/3scale/apicast/pull/526)
- Deprecated `Configuration.extract_usage` in favor of `Service.get_usage` [PR #531](https://github.com/3scale/apicast/pull/531)
- Extract Test::APIcast to own package on CPAN [PR #528](https://github.com/3scale/apicast/pull/528)
- Load policies by the APIcast loader instead of changing load path [PR #532](https://github.com/3scale/apicast/pull/532)

## [3.2.0-alpha2] - 2017-11-30

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
2 changes: 1 addition & 1 deletion gateway/conf.d/apicast.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set_by_lua $user_agent 'return require("apicast.user_agent")()';
set_by_lua_block $deployment {
local user_agent = require('apicast.user_agent')
return user_agent.platform() .. '+' .. user_agent.deployment()
return string.format('%s+%s', user_agent.platform(), user_agent.deployment())
}

# TODO: enable in the future when we support SSL
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
22 changes: 21 additions & 1 deletion gateway/src/apicast/loader.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
--- APIcast source loader
-- Loading this module will add a new source code loader to package.searchers.
-- Loading this module will add a new source code loaders to package.searchers.
-- The searcher is going to print deprecation warnings when apicast source is loaded
-- through old or non prefixed paths.
-- We can rename files and set up an alias here so we don't break customer's code and
-- print a deprecation warning.
-- Another searcher is going to look for policies with `.policy` suffix.
-- Policies can be packaged as `some_name/policy.lua` so the directory also contains the JSON spec.

local loadfile = loadfile

Expand All @@ -21,10 +23,23 @@ local function loader(name, path)
return file, err
end

--- Try to load a policy. Policies can have a `.policy` suffix.
local function policy_namespace(name, path)
local policy = name .. '.policy'

local found, err = loader(policy, path or package.path)

return found or err
end

local function prefix_loader(name, path)
local prefixed = 'apicast.' .. name
local found, err = loader(prefixed, path)

if not found then
found = policy_namespace(prefixed, path)
end

if found then
ngx.log(ngx.STDERR, 'DEPRECATION: when loading apicast code use correct prefix: require("', prefixed, '")')
end
Expand All @@ -36,6 +51,10 @@ local function rename_loader(name, path)
local new = map[name]
local found, err = loader(new, path)

if not found then
found = policy_namespace(new, path)
end

if found then
ngx.log(ngx.WARN, 'DEPRECATION: file renamed - change: require("', name, '")' ,' to: require("', new, '")')
end
Expand All @@ -55,4 +74,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/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ local require = require
local dofile = dofile
local type = type

require('apicast.loader')

local env = require 'resty.env'

local function error_message(error)
Expand Down
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: 2 additions & 1 deletion gateway/src/apicast/user_agent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local ffi = require 'ffi'
local env = require 'resty.env'

local setmetatable = setmetatable
local format = string.format

local _M = {
_VERSION = require('apicast.version')
Expand All @@ -15,7 +16,7 @@ end
-- User-Agent: Mozilla/<version> (<system-information>) <platform> (<platform-details>) <extensions>

function _M.call()
return 'APIcast/' .. _M._VERSION .. ' (' .. _M.system_information() .. ') ' .. (_M.platform() or '')
return format('APIcast/%s (%s) %s', _M._VERSION, _M.system_information(), _M.platform() or 'Unknown')
end

function _M.system_information()
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require 'ngx_helper'
require 'luassert_helper'
require 'jwt_helper'

require('apicast.loader')

local busted = require('busted')
local env = require('resty.env')

Expand Down

0 comments on commit 056428b

Please sign in to comment.