Install Kong on Ubuntu


Kong is a pretty good API Gateway, can be installed in many ways. Installing via apt will make it easier to update

$ sudo apt-get install -y apt-transport-https curl lsb-core
$ echo "deb https://kong.bintray.com/kong-deb `lsb_release -sc` main" | sudo tee -a /etc/apt/sources.list
$ curl -o bintray.key https://bintray.com/user/downloadSubjectPublicKey?username=bintray
$ sudo apt-key add bintray.key
$ sudo apt-get update
$ sudo apt-get install -y kong

Flask in Windows


I’m using Python 2.7.17 on Windows 8, virtualenv already installed (pip install virtualenv), here’s my simple step to create hello world style app

$ set FLASK_APP=app.py
$ set FLASK_ENV=development
$ virtualenv venv
$ cd venv/Scripts
$ activate
$ pip install flask
$ cd ..
$ touch app.py
$ subl app.py
-------------------------------
from flask import Flask

app = Flask(__name__)

@app.route('/')
def welcome():
return '<p>welcome to the jungle</p>'

if __name__ == '__main__':
app.run(debug=True)
-------------------------------
$ python app.py

or

$ flask run
Then, point browser to http://localhost:5000

 

Codeception – Create Helper for Reusing Token


create the file on tests/_support/Helper/ folder, to reuse the token you need to create hook _before

 

tests/ApiCest.php
-----------------
<?php 

class ApiCest 
{
    private $token;
    
    
    function createMeetingTest(ApiTester $I) {
        $data = array(
            "meeting_title" => "Meeting title",
            "meeting_date" => "2019-11-21 10:00:00"
        );
        
        $this->token = $I->getToken();
        
        $I->wantToTest('Create a meeting');
    	$I->amBearerAuthenticated($this->token);
		$I->sendPOST('/meetings', $data);
		$I->seeResponseCodeIs(200);
		$I->seeResponseIsJson();        
    }

    
}

tests/_support/Helper/Api.php
-----------------------------
<?php
namespace Helper;

class Api extends \Codeception\Module
{
	public $credentials = array(
		'username' => 'remo.harsono',
		'password' => '12345678'
	);
    
	private $jwt;


	public function getToken() {
		return $this->jwt;
	}

    public function _before(\Codeception\TestInterface $test) {
		$I = $this->getModule('REST');
		$I->haveHttpHeader('Content-Type', 'application/json');
		
		$I->sendPOST("/auth/login", $this->arrCredentials);

		$I->seeResponseCodeIs(200);
		$I->seeResponseIsJson();

		$response = $I->grabResponse();
		$json = json_decode((string)$jesponse);
		
		if ($json->result == 'OK') {
			$this->jwt = $json->jwt;
		} else {
			// ....
		}
	}
}


sample json output when calling auth/login
------------------------------------------
{
  "result": "OK",
  "jwt": "blablablabla",
}

Install VSFTPd on AWS EC2



sudo apt install vsftpd
sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.original
sudo nano /etc/vsftpd.conf

anonymous_enable=NO
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd.chroot_list
ls_recurse_enable=YES
local_root=/var/www
seccomp_sandbox=no
pam_service_name=ftp
pasv_enable=YES
pasv_min_port=13000
pasv_max_port=13100
port_enable=YES
pasv_address=__your_ec2_domain__
pasv_addr_resolve=YES

sudo useradd myftp
sudo passwd myftp
sudo usermod --home /var/www/ myftp

sudo addgroup ftpusers
sudo usermod -a -G ftpusers myftp
sudo usermod -a -G www-data myftp
sudo usermod -g ftpusers myftp

sudo nano /etc/vsftpd.chroot_list
ubuntu

sudo nano /etc/ssh/sshd_config

Match Group ftpusers
ForceCommand internal-sftp
ChrootDirectory /var/www/
PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no

sudo service ssh restart
sudo systemctl restart vsftpd

Set you EC2 Security Group to allow incoming tcp traffic to port 20-21, 13000-13100
Connect from you ftp client and set connection to passive mode

 

Nodejs – Error: ENOENT: no such file or directory, scandir ‘node_modules/node-sass/vendor’


I’ve experience weird moment today when trying to run my friend’s gulp script which call node-sass, it throw this error

Error: ENOENT: no such file or directory, scandir '**/node_modules/node-sass/vendor'

I call it weird because normally it has ‘vendor’ folder.

After Googling, visiting some stackoverflow pages, the solution is simple: just invoke these two commands

nodejs node_modules/node-sass/scripts/install.js
npm rebuild node-sass

then you’re good to go 🙂

 

PostgreSQL: Create Database, User and Grant Privileges


Open terminal and invoke this command:

psql -U postgres

to create new database, invoke this command:

CREATE DATABASE mydatabase;

to create new user, invoke this command:

CREATE USER dbuser WITH PASSWORD 'mypassword';

then, you can set your desired user-related preference like this

ALTER ROLE dbuser SET client_encoding TO 'utf8';
ALTER ROLE dbuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE dbuser SET timezone TO 'UTC';

to grant privileges, invoke this command:

GRANT ALL PRIVILEGES ON DATABASE mydatabase TO dbuser;

Quit from PostgreSQL console

\q

Python Date/Time Libraries


Maya: DateTimes for Humans
https://github.com/kennethreitz/maya

Arrow: Python library that offers a sensible, human-friendly approach to creating, manipulating, formatting and converting dates, times, and timestamps
http://arrow.readthedocs.io/en/latest/

Pendulum: Python Date/Time Made Easy
https://pendulum.eustace.io/

Delorean: Time Travel Made Easy
https://github.com/myusuf3/delorean

Moment: Dealing with dates in Python shouldn’t have to suck
https://github.com/zachwill/moment

udatetime: Fast RFC3339 compliant date-time library
https://github.com/freach/udatetime

Fast ISO8601 date time parser for Python written in C
https://github.com/closeio/ciso8601