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/CHANGELOG.md b/CHANGELOG.md index 46680f702..25f2f650c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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.d/apicast.conf b/gateway/conf.d/apicast.conf index 83d7fca4d..9c9fd5d47 100644 --- a/gateway/conf.d/apicast.conf +++ b/gateway/conf.d/apicast.conf @@ -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 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..ac8a5aefe 100644 --- a/gateway/src/apicast/loader.lua +++ b/gateway/src/apicast/loader.lua @@ -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 @@ -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 @@ -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 @@ -55,4 +74,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/module.lua b/gateway/src/apicast/module.lua index 4b12cc369..9c0b79cb9 100644 --- a/gateway/src/apicast/module.lua +++ b/gateway/src/apicast/module.lua @@ -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) 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/gateway/src/apicast/user_agent.lua b/gateway/src/apicast/user_agent.lua index 21decf353..bb170867b 100644 --- a/gateway/src/apicast/user_agent.lua +++ b/gateway/src/apicast/user_agent.lua @@ -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') @@ -15,7 +16,7 @@ end -- User-Agent: Mozilla/ () () 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() diff --git a/spec/spec_helper.lua b/spec/spec_helper.lua index 8d32067ec..a336f7bc4 100644 --- a/spec/spec_helper.lua +++ b/spec/spec_helper.lua @@ -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')