2019-04-11 16:53:21 +08:00
|
|
|
package t::APIMeta;
|
|
|
|
|
|
|
|
use lib 'lib';
|
|
|
|
use Cwd qw(cwd);
|
|
|
|
use Test::Nginx::Socket::Lua::Stream -Base;
|
|
|
|
|
|
|
|
my $pwd = cwd();
|
|
|
|
|
|
|
|
sub read_file($) {
|
|
|
|
my $infile = shift;
|
|
|
|
open my $in, $infile
|
|
|
|
or die "cannot open $infile for reading: $!";
|
|
|
|
my $cert = do { local $/; <$in> };
|
|
|
|
close $in;
|
|
|
|
$cert;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $yaml_config = read_file("conf/config.yaml");
|
|
|
|
|
|
|
|
add_block_preprocessor(sub {
|
|
|
|
my ($block) = @_;
|
|
|
|
|
|
|
|
my $http_config = $block->http_config // '';
|
|
|
|
$http_config .= <<_EOC_;
|
|
|
|
lua_package_path "$pwd/lua/?.lua;;";
|
|
|
|
|
|
|
|
init_by_lua_block {
|
|
|
|
require "resty.core"
|
2019-05-08 10:38:45 +08:00
|
|
|
apimeta = require("apimeta")
|
|
|
|
apimeta.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
init_worker_by_lua_block {
|
|
|
|
require("apimeta").init_worker()
|
2019-04-11 16:53:21 +08:00
|
|
|
}
|
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("http_config", $http_config);
|
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
my $config = $block->config;
|
|
|
|
if (!$config) {
|
|
|
|
$config .= <<_EOC_;
|
|
|
|
location / {
|
|
|
|
rewrite_by_lua_block {
|
2019-05-08 11:30:16 +08:00
|
|
|
apimeta.rewrite_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
access_by_lua_block {
|
2019-05-08 11:30:16 +08:00
|
|
|
apimeta.access_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
header_filter_by_lua_block {
|
2019-05-08 11:30:16 +08:00
|
|
|
apimeta.header_filter_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
log_by_lua_block {
|
2019-05-08 11:30:16 +08:00
|
|
|
apimeta.log_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_EOC_
|
|
|
|
}
|
|
|
|
|
|
|
|
$block->set_value("config", $config);
|
|
|
|
|
|
|
|
my $user_yaml_config = $block->yaml_config;
|
|
|
|
if ($user_yaml_config) {
|
|
|
|
$yaml_config = $user_yaml_config;
|
|
|
|
}
|
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
my $user_files = $block->user_files;
|
|
|
|
$user_files .= <<_EOC_;
|
|
|
|
>>> ../conf/config.yaml
|
|
|
|
$yaml_config
|
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("user_files", $user_files);
|
|
|
|
|
|
|
|
$block;
|
|
|
|
});
|
|
|
|
|
|
|
|
1;
|