Sai Ram's Blog

thoughts, ramblings and ideas of a geek

Making Code Cheatsheets

I used to be a Google / Stack Overflow junkie for syntax issues or finding configuration/compatability issues in the vast amount of dimensions software gives us.

An approach I used to solve this problem was to document snippets once I have solved the problem (don’t forget to upvote the answer on SO). This is especially useful when you start learning a language. When you start learning a language, there are

  1. Syntax
  2. Style Guide
  3. Bad Practices to avoid
  4. Good Practices to start with
  5. Test cases to cover
  6. Libraries to use
  7. Libraries which are not upto date
  8. Highly opiniated frameworks
  9. The philosophy of the community
  10. Tooling
  11. Vendoring
  12. Advanced Syntax
  13. Backward compatible changes
  14. Where to find help instantly (when stuck)
  15. How to maintain, build and deploy
  16. The Database interaction
  17. The Spec (may be)

As a curious person wanting to learn everything you can with tens of open tabs, believe that once you start doing a project, you will eventually get to all these.

I started learning Elixir and it took me a while to get over syntax itself. I thought I would make a different approach while starting Go. This is about in August 2016 that I started to write down (copy often) the code snippets and what they mean from the books I was reading.

Maintaining a cheatsheet in a 3 column layout for code snippets has been a huge time savor when I was searching on how was this done here. (well, may look better with Borders)


Example:

What Code Snippet Reference Link
H1 heading # heading 1 Ref H1 Link
Links [link](text) Ref Link

I use a table structure in Evernote for now which I will have to soon move to a Git Repo in markdown. Inspired by other cheatsheets I find, I have decided to make my own so that

  1. Help others work like me so that they dont have to search SO
  2. I can reference them from any where outside my private laptop
  3. Try to make the development world a better place

This approach has worked well for me when I was learning the Go language and resuming my work on Ruby has made this better.


You can track any organisation’s new open source repositories or code changes in public repos from Github or Gitlab using Gitnotify.com

GitNotify > Sign Up with Github > Track Organisations > sairam



Health Checks for Free

The Challenge

Spend least amount of recurring money to monitor your application.

Frugality, one of the core principal had almost kind of became a habit from my first job at Amazon).

The Goal

Get notified when I am online when my server goes down.

Backstory: I am running the Open Source Go application on a single $5 Digital Ocean server. No Load Balancers, No fancy hardware, no high end configuration, no Docker, just the application running on Caddy for self signed Certificates, no vault(https://www.vaultproject.io).

Search for the tools

Ideally having another server monitor this process is the basic idea, but I did not want to setup/maintain another DO server in a different location. Since pushing is not an option, polling is another

One of the options considered was Pingdom ~$7/month (free version is hidden which I found assuming while talking a friend later)

PagerDuty is one of the famous paying options of $9/month. Found few other solutions like Pingdom, but none of them had international calling or right integrations to notify me.

I finally I went down with was HealthChecks. The idea is to set an expectation of a cron which runs every X minutes etc., and meeting that expectation of cron finished running successfully or not. It has limits on the number of crons made per user and the history stored. I used Pushbullet extensively to pass websites from mobile to the desktop.

HealthChecks had integration with Pushbullet which helped me configure the notifications. A notification meant something went wrong with the server.

The script


while [ 1 ] ; do
t=$(curl -D - -s https://gitnotify.com/home | head -1 | egrep -o 200)
if [[ "$?" -eq "0" ]]; then
  curl --retry 3 https://hchk.io/12345678-abcd-1234-abcd-123456789012
fi
echo -ne " $t "
date
sleep 58
done

The False Positives:

The health check used to fail occasionally there used to be errors occasionally with the curl command informing that it cannot resolve the IP address of the domain or could not connect to the server (from the gateway). I choose not to spend time on this problem though since it was intermittent.


Today I learnt - Image Magick scripts

I was generating logos for Git Notify’s Chrome Extension with the help of GoSpaces Tools. I have got a generic one in a decent sized banner. I had to crop off in the centre.

imagemagick to the rescue. but the args? A simple Google search solved it.

convert input.jpg -gravity center -crop 25x25+0+0 output.jpg

source: Image Magick Forum

Fred Weinhaus has put together lots of Image Magick scripts. One I was looking was to crop in the middle is called squareup.


Look here if you want to generate your own logos