Skip to content

Commit

Permalink
tests: spec_helpers now has environments by config
Browse files Browse the repository at this point in the history
+ test for startup migration

feat: CLI outputs nicer startup error messages

- Nicer error messages in stdout from init failure
- cleanup bin/kong

refactor: structure

- core renamed 'resolver'
- 'resolver' doesn't inherit from Base_plugin anymore (it is not a
  plugin)
- 'resolver' has nicer method names make it clearer to understand what
  is going on in nginx.conf
- kong code is not nested in src/kong anymore, rather directly put under
  src/
  • Loading branch information
thibaultcha committed Mar 18, 2015
1 parent 3d59f53 commit 031ec56
Show file tree
Hide file tree
Showing 48 changed files with 314 additions and 231 deletions.
88 changes: 45 additions & 43 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ fi

PID=$NGINX_TMP/nginx.pid

######################
# Utility function #
######################
#####################
# Utility functions #
#####################

function check_file_exists {
if [ ! -f $1 ]; then
Expand All @@ -45,9 +45,12 @@ function real_path_func {
fi
}

function print_start_error {
printf "Starting Kong"
printf "$(tput setaf 1) [$1]\n$(tput sgr 0)"
function print_error {
printf "$1 $(tput setaf 1)[$2]\n$(tput sgr 0)"
}

function print_success {
printf "$1 $(tput setaf 2)[$2]\n$(tput sgr 0)"
}

##############
Expand All @@ -56,16 +59,13 @@ function print_start_error {

function show_help {
printf "Usage: kong [OPTION]... {start|stop|restart}\n
\t-c specify the path to a custom Kong configuration file
\t default is: '$KONG_HOME/kong.yml'
\t-v output version informations and exit
\t-c path to a kong configuration file. Default is: $KONG_CONF
\t-v output version and exit
\t-h show this message
\nCommands:\n
\tstart start Kong
\tstop stop a running Kong
\trestart restart Kong
\t it is equivalent to executing 'stop' and 'start' in succession
\tversion output version informations and exit
\tstart start kong
\tstop stop a running kong
\trestart restart kong. Equivalent to executing 'stop' and 'start' in succession
\n"
}

Expand All @@ -77,17 +77,17 @@ function start {
if [ -f $PID ]; then
if ps -p $(cat $PID) > /dev/null
then
print_start_error "ALREADY RUNNING"
print_error "Starting Kong" "ALREADY RUNNING"
exit 1
fi
fi

printf "Kong (configuration at "$KONG_CONF" output at "$NGINX_TMP")\n"
printf "configuration: $KONG_CONF\nnginx container: $NGINX_TMP\n\n"

mkdir -p $NGINX_TMP/logs &> /dev/null
if [ $? -ne 0 ]; then
printf "Cannot operate on $NGINX_TMP - Make sure you have the right permissions.\n\n"
print_start_error "ERROR"
print_error "\nStarting Kong:" "ERROR"
exit 1
fi

Expand All @@ -97,26 +97,30 @@ function start {
nginx -p $NGINX_TMP -c $NGINX_TMP/nginx.conf

if [ $? -eq 0 ]; then
printf "Starting Kong"
printf "$(tput setaf 2) [OK]\n$(tput sgr 0)"
print_success "Starting Kong" "OK"
else
printf "\nYou can get more information about this error by checking the error log file.\n\n"
print_start_error "ERROR"
print_error "\nStarting Kong" "ERROR"
error_file=`grep -e '^error_log' $NGINX_TMP/nginx.conf | grep -v syslog:server | grep -v stderr | awk '{print $2}'`
if [ -n "$error_file" ]; then
if [[ "$error_file" = /* ]]; then
printf "Error logs: $error_file\n"
else
printf "Error logs: $NGINX_TMP/$error_file\n"
fi
fi
exit 1
fi
}

function stop {
printf "Stopping Kong"

if [ ! -f $PID ]; then
printf "$(tput setaf 1) [NOT RUNNING]\n$(tput sgr 0)"
print_error "Stopping Kong" "NOT RUNNING"
if [ "$1" = false ] ; then # $1 is true when it's part of a restart
exit 1
fi
else
kill $(cat $PID)
printf "$(tput setaf 2) [OK]\n$(tput sgr 0)"
print_success "Stopping Kong" "OK"
fi
}

Expand All @@ -135,15 +139,16 @@ cmd=""
while getopts "h?vc:n:" opt; do
case "$opt" in
h|\?)
show_help
exit 0
;;
v) show_version
exit 0
;;
show_help
exit 0
;;
v)
show_version
exit 0
;;
c)
KONG_CONF=$(real_path_func $OPTARG)
;;
KONG_CONF=$(real_path_func $OPTARG)
;;
esac
done

Expand All @@ -155,19 +160,16 @@ check_file_exists $KONG_CONF

case "$@" in
start)
start
;;
start
;;
stop)
stop
;;
stop
;;
restart)
restart
;;
version)
show_version
;;
restart
;;
*)
show_help
show_help

esac

Expand Down
74 changes: 37 additions & 37 deletions kong-0.0.1beta-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,53 +35,53 @@ dependencies = {
build = {
type = "builtin",
modules = {
["kong"] = "src/main.lua",
["kong"] = "src/kong.lua",
["classic"] = "src/classic.lua",

["kong.constants"] = "src/kong/constants.lua",
["kong.constants"] = "src/constants.lua",

["kong.tools.utils"] = "src/kong/tools/utils.lua",
["kong.tools.timestamp"] = "src/kong/tools/timestamp.lua",
["kong.tools.cache"] = "src/kong/tools/cache.lua",
["kong.tools.http_client"] = "src/kong/tools/http_client.lua",
["kong.tools.faker"] = "src/kong/tools/faker.lua",
["kong.tools.migrations"] = "src/kong/tools/migrations.lua",
["kong.tools.utils"] = "src/tools/utils.lua",
["kong.tools.timestamp"] = "src/tools/timestamp.lua",
["kong.tools.cache"] = "src/tools/cache.lua",
["kong.tools.http_client"] = "src/tools/http_client.lua",
["kong.tools.faker"] = "src/tools/faker.lua",
["kong.tools.migrations"] = "src/tools/migrations.lua",

["kong.base_plugin"] = "src/kong/base_plugin.lua",
["kong.plugins.base_plugin"] = "src/plugins/base_plugin.lua",

["kong.core.handler"] = "src/kong/core/handler.lua",
["kong.core.access"] = "src/kong/core/access.lua",
["kong.core.header_filter"] = "src/kong/core/header_filter.lua",
["kong.resolver.handler"] = "src/resolver/handler.lua",
["kong.resolver.access"] = "src/resolver/access.lua",
["kong.resolver.header_filter"] = "src/resolver/header_filter.lua",

["kong.dao.schemas"] = "src/kong/dao/schemas.lua",
["kong.dao.schemas"] = "src/dao/schemas.lua",

["kong.dao.error"] = "src/kong/dao/error.lua",
["kong.dao.cassandra.factory"] = "src/kong/dao/cassandra/factory.lua",
["kong.dao.cassandra.base_dao"] = "src/kong/dao/cassandra/base_dao.lua",
["kong.dao.cassandra.apis"] = "src/kong/dao/cassandra/apis.lua",
["kong.dao.cassandra.metrics"] = "src/kong/dao/cassandra/metrics.lua",
["kong.dao.cassandra.plugins"] = "src/kong/dao/cassandra/plugins.lua",
["kong.dao.cassandra.accounts"] = "src/kong/dao/cassandra/accounts.lua",
["kong.dao.cassandra.applications"] = "src/kong/dao/cassandra/applications.lua",
["kong.dao.error"] = "src/dao/error.lua",
["kong.dao.cassandra.factory"] = "src/dao/cassandra/factory.lua",
["kong.dao.cassandra.base_dao"] = "src/dao/cassandra/base_dao.lua",
["kong.dao.cassandra.apis"] = "src/dao/cassandra/apis.lua",
["kong.dao.cassandra.metrics"] = "src/dao/cassandra/metrics.lua",
["kong.dao.cassandra.plugins"] = "src/dao/cassandra/plugins.lua",
["kong.dao.cassandra.accounts"] = "src/dao/cassandra/accounts.lua",
["kong.dao.cassandra.applications"] = "src/dao/cassandra/applications.lua",

["kong.plugins.authentication.handler"] = "src/kong/plugins/authentication/handler.lua",
["kong.plugins.authentication.access"] = "src/kong/plugins/authentication/access.lua",
["kong.plugins.authentication.schema"] = "src/kong/plugins/authentication/schema.lua",
["kong.plugins.authentication.handler"] = "src/plugins/authentication/handler.lua",
["kong.plugins.authentication.access"] = "src/plugins/authentication/access.lua",
["kong.plugins.authentication.schema"] = "src/plugins/authentication/schema.lua",

["kong.plugins.networklog.handler"] = "src/kong/plugins/networklog/handler.lua",
["kong.plugins.networklog.log"] = "src/kong/plugins/networklog/log.lua",
["kong.plugins.networklog.schema"] = "src/kong/plugins/networklog/schema.lua",
["kong.plugins.networklog.handler"] = "src/plugins/networklog/handler.lua",
["kong.plugins.networklog.log"] = "src/plugins/networklog/log.lua",
["kong.plugins.networklog.schema"] = "src/plugins/networklog/schema.lua",

["kong.plugins.ratelimiting.handler"] = "src/kong/plugins/ratelimiting/handler.lua",
["kong.plugins.ratelimiting.access"] = "src/kong/plugins/ratelimiting/access.lua",
["kong.plugins.ratelimiting.schema"] = "src/kong/plugins/ratelimiting/schema.lua",
["kong.plugins.ratelimiting.handler"] = "src/plugins/ratelimiting/handler.lua",
["kong.plugins.ratelimiting.access"] = "src/plugins/ratelimiting/access.lua",
["kong.plugins.ratelimiting.schema"] = "src/plugins/ratelimiting/schema.lua",

["kong.web.app"] = "src/kong/web/app.lua",
["kong.web.routes.accounts"] = "src/kong/web/routes/accounts.lua",
["kong.web.routes.apis"] = "src/kong/web/routes/apis.lua",
["kong.web.routes.applications"] = "src/kong/web/routes/applications.lua",
["kong.web.routes.plugins"] = "src/kong/web/routes/plugins.lua",
["kong.web.routes.base_controller"] = "src/kong/web/routes/base_controller.lua"
["kong.web.app"] = "src/web/app.lua",
["kong.web.routes.accounts"] = "src/web/routes/accounts.lua",
["kong.web.routes.apis"] = "src/web/routes/apis.lua",
["kong.web.routes.applications"] = "src/web/routes/applications.lua",
["kong.web.routes.plugins"] = "src/web/routes/plugins.lua",
["kong.web.routes.base_controller"] = "src/web/routes/base_controller.lua"
},
copy_directories = { "src/kong/web/admin", "src/kong/web/static" }
copy_directories = { "src/web/admin", "src/web/static" }
}
24 changes: 16 additions & 8 deletions kong.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ nginx: |
}
http {
lua_package_path ";;";
lua_package_path ';;';
lua_code_cache on;
access_log logs/access.log;
Expand Down Expand Up @@ -80,13 +80,20 @@ nginx: |
resolver 8.8.8.8;
charset UTF-8;
init_by_lua "kong = require 'kong'; kong.init()";
init_by_lua '
kong = require "kong"
local status, err = pcall(kong.init)
if not status then
ngx.log(ngx.ERR, "Startup error: "..err)
os.exit(1)
end
';
server {
listen 8000;
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
return 200 'User-agent: *\nDisallow: /';
}
location / {
Expand All @@ -99,29 +106,30 @@ nginx: |
set $querystring nil;
# Authenticate the user and load the API info
access_by_lua "kong.access()";
access_by_lua 'kong.exec_plugins_access()';
# Proxy the request
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass $backend_url;
# Add additional response headers
header_filter_by_lua "kong.header_filter()";
header_filter_by_lua 'kong.exec_plugins_header_filter()';
# Change the response body
body_filter_by_lua "kong.body_filter()";
body_filter_by_lua 'kong.exec_plugins_body_filter()';
# Log the request
log_by_lua "kong.log()";
log_by_lua 'kong.exec_plugins_log()';
}
error_page 500 /500.html;
location = /500.html {
internal;
content_by_lua '
local utils = require "kong.tools.utils"
utils.show_error(ngx.status, "Ops, an unexpected error occurred!")';
utils.show_error(ngx.status, "Oops, an unexpected error occurred!")
';
}
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ if args.COMMAND == "create" then
TEST = {
["send_anonymous_reports: true"] = "send_anonymous_reports: false",
["keyspace: kong"] = "keyspace: kong_tests",
["lua_package_path \";;\""] = "lua_package_path \""..args.kong.."/src/?.lua;;\"",
["lua_package_path ';;'"] = "lua_package_path '"..args.kong.."/src/?.lua;;'",
["error_log logs/error.log info"] = "error_log logs/error.log debug",
["listen 8000"] = "listen 8100",
["listen 8001"] = "listen 8101"
},
DEVELOPMENT = {
["send_anonymous_reports: true"] = "send_anonymous_reports: false",
["keyspace: kong"] = "keyspace: kong_development",
["lua_package_path \";;\""] = "lua_package_path \""..args.kong.."/src/?.lua;;\"",
["lua_package_path ';;'"] = "lua_package_path '"..args.kong.."/src/?.lua;;'",
["error_log logs/error.log info"] = "error_log logs/error.log debug",
["lua_code_cache on"] = "lua_code_cache off",
["daemon on"] = "daemon off"
Expand Down
9 changes: 5 additions & 4 deletions spec/integration/api/api_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ local spec_helper = require "spec.spec_helpers"
local http_client = require "kong.tools.http_client"
local cjson = require "cjson"

local env = spec_helper.get_env()
local created_ids = {}

local kWebURL = spec_helper.API_URL
local ENDPOINTS = {
{
collection = "apis",
total = table.getn(spec_helper.faker.FIXTURES.api) + 1,
total = table.getn(env.faker.FIXTURES.api) + 1,
entity = {
public_dns = "api.mockbin.com",
name = "mockbin",
Expand All @@ -21,7 +22,7 @@ local ENDPOINTS = {
},
{
collection = "accounts",
total = table.getn(spec_helper.faker.FIXTURES.account) + 1,
total = table.getn(env.faker.FIXTURES.account) + 1,
entity = {
provider_id = "123456789"
},
Expand All @@ -32,7 +33,7 @@ local ENDPOINTS = {
},
{
collection = "applications",
total = table.getn(spec_helper.faker.FIXTURES.application) + 1,
total = table.getn(env.faker.FIXTURES.application) + 1,
entity = {
public_key = "PUB_key",
secret_key = "SEC_key",
Expand All @@ -48,7 +49,7 @@ local ENDPOINTS = {
},
{
collection = "plugins",
total = table.getn(spec_helper.faker.FIXTURES.plugin) + 1,
total = table.getn(env.faker.FIXTURES.plugin) + 1,
entity = {
name = "ratelimiting",
api_id = function()
Expand Down
Loading

0 comments on commit 031ec56

Please sign in to comment.