Tuan Anh

container nerd. k8s || GTFO

How to connect to remote Postgres DB through ssh tunnel

Open a SSH tunnel on your local machine. Here I’m openning a tunnel on my local port 5555, mapping it to the remote 5432 default port of Postgres.

If you have already changed the default port of Postgres, change it accordingly here as well

ssh -fNg -L 5555:localhost:5432 username@host

I’m going to use pgAdmin 3 app to connect to Postgres db. Just follow the new connection instruction as you’re connecting a local database.

If you’re using IDENT authentication, your local username has to be matched with the username you’re trying to log on to. Either switch to PASSWORD authentication or create a new username on remote db that match your local username as below and you’re good to go.

CREATE USER myuser;
ALTER USER myuser WITH PASSWORD 'password';
ALTER USER myuser WITH CREATEUSER CREATEDB;

Speed up jekyll site generation with rsync

Jekyll doesn’t support incremental generation so if your site contains loads of static assets like image, audios,.. jekyll is gonna stroke everytime you re-generate your site by deleting and re-generate/re-coppying everything. This little trick below will help you speeding up jekyll generation process by skipping all the static assets that you specified, until jekyll comes up with a better solution.

  • Rename your static asset folder with underscore in front. jekyll is going to ignore this when generating site. Assume this folder is named images. You will rename it to be _images.

  • In your _config.yml, add the line below. This line basically means that when re-generating, the folder called images in generated folder is not getting touched.

keep_files: ["images"]
  • Keeping in the source and generated folder in sync with a little plugin and rsync. Created a file name rsync_image_generator.rb in _plugins folder with the content below.
module Jekyll    
  class RsyncImageGenerator < Generator
    def generate(site)
      system('mkdir -p _site');
      system('rsync --archive --delete _img/ _site/img/');
    end
  end
end  

From now on, jekyll will ignore the _images folder in your source and images in _sites folder when generating your site. We keep it in sync by using rsync, which is much better/faster than delete/copy process jekyll is using.

Github Pages does support jekyll gems now but it’s kinda limited.


Setup custom error page for jekyll on nginx

First of all, create your custom error page. You can either use static html or jekyll template for this. If you decide to use one of your custom layout, the path will be a bit different when jekyll generates static pages.

I created a 404.html file that looks like this.

---
layout: default
title: "404: Page not found"
---

Olps.

Now if you create one like mine above at root, it will be generated as 404/index.html so use this path instead of just 404.html when configure nginx.

Edit your nginx config file to add these lines inside server block.

error_page 404 /404/index.html;
error_page 500 502 503 504 /50x/index.html;

Test config your new nginx conf, reload (or restart) and you’re good to go.


Hydra - an open-source window manager for OS X written in Lua

Open-source, written in Lua

Gotta try this.

Update: Hydra was renamed to mjolnir due to trademark issues

link bài gốc

Clean code/dirty code

I like to think of myself as a perfectionist. I like going back and forth re- evaluate all the possible options and come up with the cleanest, best, perfect alternative (I can think of) out there. If you are like me and you got to maintain someone else’s bad code, this is gonna be a real nightmare. You just want to scratch everything and start over again. Unfortunately, management people doesn’t think so and you don’t get the luxury of times you need. All they want is you get it fixed and ship the product as soon as possible.

I thought it is what makes me a better developer but in the end, getting a shippable product is what matters.

Sometimes, it’s just better to use a bit of dirty, tricky code to make it works. Sometimes, you just need a bit use of dirty code if it is what it takes to get the product out of the door.


How to use SSH tunnel to access blocked websites

Requirements

  • An UNIX server/VPS (since it’s cheaper than Windows server/VPS). Lowest package would probably do. Location: not in the the same area/country as you.
  • A little copy/paste skill. No Unix commands knowledge required.

Howto

  • If you’re on Unix/OS X, open Terminal app and issue the command
ssh -D 1080 username@ip_or_domain

This command will connect to your server/VPS using ssh and open a ssh tunnel on port 1080 of your computer. Setup your browser/OS to use localhost:1080 as socks5.

All the traffic will be sent over ssh tunnel hence your ISP will only see it as you’re accessing your server/VPS.

If you’re on Windows, you can grab Tunnelier (free). The setup is pretty straight forward.


How to code like a dick in JS

The goal of this post is to make simple things look complicated.

1. Invert logic

Let’s start with something easy.

Instead of

if (x && y) { ... } // easy

Write this instead

if (!(!x || !y)) { ... } // good enough

2. Extended unicode chars for variable names

Use of extended unicode chars, especially those that are not visible in console like \r, \t, etc…

3. Use = instead of ==

if (x=true) { ... } // always executed

Do this when you want to always excute that code block. Someone may see this and will attempt to “fix” it on their own, changing the original logic. Goodluck debugging it later : )

4. Use 8-base or n-base instead of decimal base

Because why not?

var x = 19;   // 19
var y = 019;  // 17

Programming is like writing a book

Programming is like writing a book… except if you miss out a single comma, the whole thing makes no damn sense.

On why programming is hard.


Matias Laptop Pro review

I love my HHKB. I love its form factor. It’s perfect, except that it requires a mini-USB cable to connect to my Macbook which isn’t very convenient.

The Matias Laptop Pro is the perfect addition to my keyboards family: it’s wireless, small and handy.

Matias Laptop Pro

Design

Let’s take a closer look at the Matias Laptop Pro.

There isn’t much change to the case compare with other Matias keyboards. It’s quite solid but the the feeling is not as good as the HHKB or Filco.

The case is made from Injected Policarbonate (used in football helmets) which is more resistant than other commonly used, cheap ABS plastic. It’s bit too glossy for my taste and a horrible fingerprint magnet (it’s not deal breaker to me though).

The Laptop Pro is a bit bigger in size, much less handy and quite heavier when compare with the HHKB Pro 2. It’s the smallest form Matias is offering for now but I heard they mentioned about working on a 60% board.

The keyboard has 2 rubber feet but still a bit slippy and easily get slided on the table.

Switches

It’s very quiet. Much quieter than any switches I’ve ever owned or tried before. The tactile bump is quite nice but not as noticable as the Tactile Pro. This is a trade off for being quiet I guess.

I’ve tried Brown MX keyboards and I have to say Matias quiet switches are definitely quieter, even if you do bottoming out everytime.

Layout

A bit hard for me as I get used to the HHKB layout with the Ctrl key at the Caps lock position instead. I may have to reassign the key as I keep mistakenly holding Caps lock + Tab for tab switching in Chrome.

Also, the Fn key next to arrow keys choice is an interesting choice, though I do not need to use Fn as often as I do to on my old HHKB.

Keycaps

Unfortunately I haven’t found a single place that sell custom keycaps for Matias quite switches yet. You’re stucked with the default one.


If you are already spoiled by the mechanical keyboards and don’t want to settle for less when you’re on the go, this keyboard is for you. It’s small, quiet, lightweight enough to carry around plus no more cable, thanks to the Bluetooth connection.

  • A side note: I expect the keyboard to continue functioning if the charging cable is used and Bluetooth is off. Well, it doesn’t. I don’t know about the technical difficulty to make it work though.*

Cakebrew

A Mac app for Homebrew

Not sure who’re the target users for this app as most of those who want to tinker with Homebrew formulas are probably not afraid of learning a few terminal commnads.

Still useful nonetheseless.

link bài gốc