2019-05-13 14:28:54 +08:00
|
|
|
package t::APISix;
|
2019-04-11 16:53:21 +08:00
|
|
|
|
|
|
|
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) = @_;
|
|
|
|
|
2019-06-11 12:20:57 +08:00
|
|
|
my $init_by_lua_block = $block->init_by_lua_block // <<_EOC_;
|
|
|
|
require "resty.core"
|
|
|
|
apisix = require("apisix")
|
|
|
|
apisix.init()
|
|
|
|
_EOC_
|
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
my $http_config = $block->http_config // '';
|
|
|
|
$http_config .= <<_EOC_;
|
2019-06-13 12:01:36 +08:00
|
|
|
lua_package_path "$pwd/lua/?.lua;$pwd/t/?.lua;/usr/share/lua/5.1/?.lua;;";
|
2019-05-17 15:58:15 +08:00
|
|
|
lua_package_cpath '/usr/lib64/lua/5.1/?.so;;';
|
2019-04-11 16:53:21 +08:00
|
|
|
|
2019-06-10 14:20:24 +08:00
|
|
|
lua_shared_dict plugin-limit-req 10m;
|
|
|
|
lua_shared_dict plugin-limit-count 10m;
|
2019-06-11 17:31:40 +08:00
|
|
|
lua_shared_dict plugin-limit-conn 10m;
|
2019-06-10 14:20:24 +08:00
|
|
|
lua_shared_dict prometheus-metrics 10m;
|
|
|
|
|
2019-06-10 14:57:11 +08:00
|
|
|
resolver ipv6=off local=on;
|
|
|
|
resolver_timeout 5;
|
|
|
|
|
2019-06-11 23:47:00 +08:00
|
|
|
lua_socket_log_errors off;
|
|
|
|
|
2019-06-14 10:27:45 +08:00
|
|
|
upstream apisix_backend {
|
|
|
|
server 0.0.0.1;
|
|
|
|
balancer_by_lua_block {
|
|
|
|
apisix.balancer_phase()
|
|
|
|
}
|
|
|
|
|
|
|
|
keepalive 32;
|
|
|
|
}
|
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
init_by_lua_block {
|
2019-06-11 12:20:57 +08:00
|
|
|
$init_by_lua_block
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
init_worker_by_lua_block {
|
2019-05-13 14:28:54 +08:00
|
|
|
require("apisix").init_worker()
|
2019-04-11 16:53:21 +08:00
|
|
|
}
|
2019-06-14 10:27:45 +08:00
|
|
|
|
|
|
|
server {
|
|
|
|
listen 1980;
|
|
|
|
|
|
|
|
location /hello {
|
|
|
|
echo "hello world";
|
|
|
|
}
|
|
|
|
}
|
2019-04-11 16:53:21 +08:00
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("http_config", $http_config);
|
|
|
|
|
2019-06-15 22:34:26 +08:00
|
|
|
my $wait_etcd_sync = $block->wait_etcd_sync // 0.1;
|
|
|
|
|
2019-06-13 12:01:36 +08:00
|
|
|
my $config = $block->config // '';
|
2019-06-15 22:34:26 +08:00
|
|
|
$config .= <<_EOC_;
|
2019-06-13 12:01:36 +08:00
|
|
|
location /apisix/admin {
|
|
|
|
content_by_lua_block {
|
|
|
|
apisix.admin()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
location / {
|
2019-06-15 22:34:26 +08:00
|
|
|
set \$upstream_scheme 'http';
|
|
|
|
set \$upstream_host \$host;
|
|
|
|
set \$upstream_upgrade '';
|
|
|
|
set \$upstream_connection '';
|
|
|
|
set \$upstream_uri '';
|
2019-06-14 10:27:45 +08:00
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
access_by_lua_block {
|
2019-06-14 10:27:45 +08:00
|
|
|
-- wait for etcd sync
|
2019-06-15 22:34:26 +08:00
|
|
|
ngx.sleep($wait_etcd_sync)
|
2019-05-13 14:28:54 +08:00
|
|
|
apisix.access_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
2019-06-14 10:27:45 +08:00
|
|
|
proxy_http_version 1.1;
|
2019-06-15 22:34:26 +08:00
|
|
|
proxy_set_header Host \$upstream_host;
|
|
|
|
proxy_set_header Upgrade \$upstream_upgrade;
|
|
|
|
proxy_set_header Connection \$upstream_connection;
|
|
|
|
proxy_set_header X-Real-IP \$remote_addr;
|
2019-06-14 10:27:45 +08:00
|
|
|
proxy_pass_header Server;
|
|
|
|
proxy_pass_header Date;
|
2019-06-15 22:34:26 +08:00
|
|
|
proxy_pass \$upstream_scheme://apisix_backend\$upstream_uri;
|
2019-06-14 10:27:45 +08:00
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
header_filter_by_lua_block {
|
2019-05-13 14:28:54 +08:00
|
|
|
apisix.header_filter_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
log_by_lua_block {
|
2019-05-13 14:28:54 +08:00
|
|
|
apisix.log_phase()
|
2019-05-08 10:38:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("config", $config);
|
|
|
|
|
2019-06-16 07:32:25 +08:00
|
|
|
my $user_yaml_config = $block->yaml_config // $yaml_config;
|
2019-05-08 10:38:45 +08:00
|
|
|
|
2019-04-11 16:53:21 +08:00
|
|
|
my $user_files = $block->user_files;
|
|
|
|
$user_files .= <<_EOC_;
|
|
|
|
>>> ../conf/config.yaml
|
2019-06-16 07:32:25 +08:00
|
|
|
$user_yaml_config
|
2019-04-11 16:53:21 +08:00
|
|
|
_EOC_
|
|
|
|
|
|
|
|
$block->set_value("user_files", $user_files);
|
|
|
|
|
|
|
|
$block;
|
|
|
|
});
|
|
|
|
|
|
|
|
1;
|