Install Kong Api Gateway on Docker Windows

Today i want to explain how to install Kong Api Gateway on your Windows Docker Desktop.

This is the official guide but there are some error ( https://hub.docker.com/_/kong )

I have used PostreSQL. In the official guide there is not a “Password” for database and so it do error.

Open a command line and insert this three command.

On your docker desktop client than you must to see the 2 container for database and api gateway.

1. Link Kong to either a Cassandra or PostgreSQL container

docker run -d --name kong-database ^
                -p 5432:5432 ^
                -e "POSTGRES_USER=kong" ^
                -e "POSTGRES_DB=kong" ^
		-e "POSTGRES_PASSWORD=kong" ^
                postgres:9.6

2. Prepare your database

docker run --rm ^
    --link kong-database:kong-database ^
    -e "KONG_DATABASE=postgres" ^
    -e "KONG_PG_HOST=kong-database" ^
    -e "KONG_PG_PASSWORD=kong" ^
    kong kong migrations bootstrap
docker run -d --name kong ^
    --link kong-database:kong-database ^
    -e "KONG_DATABASE=postgres" ^
    -e "KONG_PG_HOST=kong-database" ^
    -e "KONG_PG_PASSWORD=kong" ^
    -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" ^
    -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" ^
    -e "KONG_PROXY_ERROR_LOG=/dev/stderr" ^
    -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" ^
    -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" ^
    -p 8000:8000 ^
    -p 8443:8443 ^
    -p 8001:8001 ^
    -p 8444:8444 ^
    kong

Have fun!

Extend Log4J and evitate the error Repository is null

I have seen that on the web there are few guide on how to extend Log4j.
Now i show to you how to create an extension with a timer function for Log4J.

If you have extended yet a class but have error on “repository nulla” like:
NullPointerException when extending Log4j Logger class

You must to see this extension and resolve that problem. Because you must to use the LogManager istance.

import org.apache.log4j.Logger;

public class LogHelper extends Logger {
    final int _whitespace = 20;
    protected Logger target;

    public LogHelper(Logger target) {
        super(target.getName());
        this.target = target;
    }

    public void timer(String msg, Long timerMillisecond) {
        _timer(msg, timerMillisecond);
    }

    public void timer(Long timerMillisecond) {
        _timer("", timerMillisecond);
    }

    private void _timer(String msg, Long timerMillisecond)
    {
        StringBuilder builder = new StringBuilder(_whitespace);
        String strLog = "";
        strLog = "TIMER: "+timerMillisecond+"ms";
        builder.append(strLog);

        int whiteSpace = _whitespace-builder.length();
        for(int i = 0; i <whiteSpace ; i++) {
            builder.append(' ');
        }
        strLog = builder.toString();

        if(msg != null &amp;&amp; msg != "")  strLog = strLog.concat(" || "+msg);

        if(timerMillisecond < 2000) {
            target.info(strLog);
        } else {
            target.warn(strLog);
        }
    }

    public void error(String message){
        if (target != null)
            target.error(message);
    }

    public void error(Object message, Throwable t) {
        if (target != null)
            target.error(message, t);
    }
}

Refer Logger

The important was to set the refer to a Logger

public LogHelper(Logger target) {
        super(target.getName());
        this.target = target;
    }

Wrap the log error to avoid the NullPointerException on Repository

public void error(String message){
    if (target != null)
    target.error(message);
}

public void error(Object message, Throwable t) {
    if (target != null)
    target.error(message, t);
}