May 3, 2012

It has been a while since I've updated this site (including any new blog posts!) so I thought I would redesign the entire thing using Node.js. You are looking at the result.

It is a very simple app which can parse the static posts from the original Octopress site. It uses Express for the routing and Stylus for the styling and most of it is written in Coffeescript.

It integrates with github to pull all the activity for a specified user and displays it in the activity list in the footer and with mailgun to send emails when interesting things happen. In the future, I would like to add twitter integration as well.

October 17, 2011

After upgrading to OS X 10.7.2 - the latest version of Lion - I started seeing corruption when I moved windows. It looked like images behind the window where bleeding into the window I was moving. When I clicked on a different window the corruption would disappear.

Very irritating.

Some quick googling suggested that repairing my disk permissions might fix the issue.

So reboot into the "Recovery HD" (reboot and hold down the alt/option key as the boot process starts to display bootable drives), run "Disk Utitity" and run "Repair Permissions". Reboot.

Everything seems to be fixed...

October 2, 2011

I recently was complaining about how complicated socket programming was in C - so in an effort to help my future self, I created a little wrapper around connecting and binding sockets in C.

To bind a socket to all interfaces via TCP on port 8080:

int s = connection_bind("tcp://:8080");

To bind a socket to the local interface via TCP on port 8080:

int s = connection_bind("tcp://localhost:8080");

To connect to www.codefall.com on port 80:

int s = connection_connect("tcp://www.codefall.com:80");

UDP works too:

int s = connection_bind("udp://:8080");
October 1, 2011

I am always amazed at how complex network programming is in C. I've been programming with bare sockets for over a decade and I need to look up how to do it every time.

As an example of how easy it can be, here is a snippet of code to connect to a website and download the main page using python:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('www.codefall.com', 80))
s.send('GET / HTTP/1.1\r\nHost: www.codefall.com\r\n\r\n')
s.recv(1024)

In contrast, here is the same snippet in C:

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>

int main()
{
  struct addrinfo hints, *res;
  int st, sock = -1;

  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;

  st = getaddrinfo("www.codefall.com", "80", &hints, &res);
  assert(st == 0);

  sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  assert(sock >= 0);

  st = connect(sock, res->ai_addr, res->ai_addrlen);
  assert(st == 0);

  freeaddrinfo(res);

  const char *msg = "GET / HTTP1.1\r\nHost: www.codefall.com\r\n\r\n";
  st = send(sock, msg, strlen(msg), 0);
  assert(st == strlen(msg));

  char buffer[1024];
  st = recv(sock, buffer, 1024, 0);
  assert(st > 0);

  close(sock);
  return 0;
}

Based on this, I can definitely see why people don't program in C unless they really, really need to...

September 22, 2011

I have been looking for a simple blogging engine to host my new Codefall blog for the last couple of months. Most of the contenders required me to install application servers (e.g. PHP, Java, etc) and an SQL server on my little Linode. That didn't seem like a great use of resources.

So I wrote my own... It was very simple - it consisted of text files with a header describing the post's title and date and the post content was markdown. That worked but I really didn't want to maintain it nor did I want to add more functionality since I am spending all my waking hours working on my startup.

Then I found Octopress. It did what my simple blogging engine did but it did it well. Lots of functionality and options and a nice default theme.

And I didn't need to run any servers on my little Linode since Octopress generates a static site! I just installed the Octopress framework on my laptop, generated the blog and deployed to my Linode using rsync. Awesome, right?

I'm running Mac OS X 10.7.1 (which is a steaming pile but that's another story) - here is what I did to get Octopress up and running:

brew install ruby     # requires 1.9.2 but OS X only has 1.8.7
gem install bundler   # the "bundle" command deals with ruby dependencies

git clone git://github.com/imathis/octopress.git octopress

cd octopress
bundle install  # install octopress dependencies
rake install    # install the default theme

vi Rakefile     # change the first 2 options - ssh_user & document_root
vi _config.yml  # change the top few options as necessary

rake generate   # create the site
rake deploy     # deploy the site

This requires your OS X box to have some applications already installed (e.g. brew, git, gcc, etc).

On the server, I just added another nginx server configuration like the following example and I was finished.

server {
listen   80; ## listen for ipv4
listen   [::]:80 default ipv6only=on; ## listen for ipv6

server_name  example.com www.example.com;
access_log  /var/log/nginx/example.access.log;

location / {
    root   /path/to/deployed/files;
}
}

The posts are added under "source/_posts" with the format described here. The useful "rake preview" command allowed me to test my changes locally at http://localhost:4000.

Exactly the solution I was looking for.

September 10, 2011

To configure a Tornado server to run behind an nginx proxy is relatively easy. The following steps were using to configure a Ubuntu 10.10 server:

  • Start the Tornado server on some non-standard port - e.g. 8888
  • Tell nginx to proxy requests to your Tornado server on port 8888
  • Optionally you can have nginx handle all the static content (e.g. images, css, etc.) to reduce the load on the Tornado server

Configuring the Tornado server to run on a port is as simple as modifying the parameter passed to the listen function of the application object.

application = tornado.web.Application(routes, **settings)
application.listen(8888)

And then configure nginx to pass requests to your Tornado server. The following configuration snippet is meant to be included from the default Ubuntu 10.10 nginx server configuration. It includes the optional third step which tries to serve a static page from the filesystem before passing the request off the the upstream tornado server.

upstream example_servers {
  server 127.0.0.1:8888;
}

server {
  listen 80;
  server_name www.example.com;
  access_log /var/log/nginx/example.access.log;

  location / {
    root /opt/example/static;
    try_files $uri @example;
  }

  location @example {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass       http://example_servers;
  }
}

This snippet assumes the name of your site is "example" and it is located in /opt. To use it replace the "example" string with the actual name of your project.