Skip to content

Commit

Permalink
[policy] rename policy.lua to init.lua
Browse files Browse the repository at this point in the history
* and let policy to decide own file name for the implementation
  • Loading branch information
mikz committed Feb 7, 2018
1 parent af4e8a0 commit 342d634
Show file tree
Hide file tree
Showing 30 changed files with 27 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Renamed `apicast/policy/policy.lua` to `apicast/policy.lua` [PR #569](https://github.com/3scale/apicast/pull/569)
- Sandbox loading policies [PR #566](https://github.com/3scale/apicast/pull/566)
- Extracted `usage` and `mapping_rules_matcher` modules so they can be used from policies [PR #580](https://github.com/3scale/apicast/pull/580)
- Renamed all `apicast/policy/*/policy.lua` to `apicast/policy/*/init.lua` to match Lua naming [PR #579](https://github.com/3scale/apicast/pull/579)

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

Expand Down
2 changes: 1 addition & 1 deletion examples/custom-module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ There is another example of custom module implementation for IP blacklisting in
APICAST_MODULE=$(pwd)/verbose.lua ../../bin/apicast -c $(pwd)/../configuration/local.json
```

This starts APIcast with module `verbose.lua` instead of the default [`apicast.lua`](../../gateway/src/apicast/policy/apicast/policy.lua). Local configuration file is used, so no 3scale account is needed.
This starts APIcast with module `verbose.lua` instead of the default [`apicast.lua`](../../gateway/src/apicast/policy/apicast/apicast.lua). Local configuration file is used, so no 3scale account is needed.

## Testing

Expand Down
25 changes: 10 additions & 15 deletions gateway/src/apicast/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
-- Policies can be packaged as `some_name/policy.lua` so the directory also contains the JSON spec.

local loadfile = loadfile
local find = string.find
local sub = string.sub

local policy_loader = require 'apicast.policy_loader'

local map = {
['apicast'] = 'apicast.policy.apicast'
Expand All @@ -24,28 +26,21 @@ local function loader(name, path)
return file, err
end

--- Try to load a policy. Policies can have a `.policy` suffix.
local function policy_loader(name, path)
if not find(name, '.policy.', 1, true) then return end

local policy = name .. '.policy'

return loader(policy, path or package.path)
end

--- Searcher has to return the loader or an error message.
local function policy_searcher(name, path)
local found, err = policy_loader(name, path)
local function policy_searcher(name)
if sub(name, 1, 15) == 'apicast.policy.' then
local mod = policy_loader:pcall(sub(name, 16), 'builtin')

return found or err
if mod then return function () return mod end end
end
end

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

if not found then
found = policy_loader(prefixed, path)
found = policy_searcher(prefixed)
end

if found then
Expand All @@ -60,7 +55,7 @@ local function rename_loader(name, path)
local found, err = loader(new, path)

if not found then
found = policy_loader(new, path)
found = policy_searcher(new)
end

if found then
Expand Down
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/apicast/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('apicast')
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/caching/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('caching')
File renamed without changes.
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/cors/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('cors')
File renamed without changes.
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/echo/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('echo')
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/find_service/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('find_service')
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/headers/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('headers')
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/load_configuration/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('load_configuration')
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/local_chain/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('local_chain')
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/phase_logger/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('phase_logger')
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/upstream/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('upstream')
1 change: 1 addition & 0 deletions gateway/src/apicast/policy/url_rewriting/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('url_rewriting')
2 changes: 1 addition & 1 deletion gateway/src/apicast/policy_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ function _M:call(name, version, dir)

-- passing the "exclusive" flag for the require so it does not fallback to native require
-- it should load only policies and not other code and fail if there is no such policy
return loader('policy', true)
return loader('init', true)
end

function _M:pcall(name, version, dir)
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/policies/test/1.0.0-0/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return require('test')
4 changes: 2 additions & 2 deletions spec/policy_loader_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('APIcast Policy Loader', function()
local ok, ret = pcall(_M.call, _M, 'unknown', '0.1')

assert.falsy(ok)
assert.match([[module 'policy' not found:
%s+no file '%g+/gateway/policies/unknown/0.1/policy.lua']], ret)
assert.match([[module 'init' not found:
%s+no file '%g+/gateway/policies/unknown/0.1/init.lua']], ret)
end)

it('loads two instances of the same policy', function()
Expand Down

0 comments on commit 342d634

Please sign in to comment.