Heroku, golang and martini

Just a note to myself. When deploying go + martini project to Heroku, there was an error:

remote: -----> Running: godep go install -tags heroku ./...
remote: www.go:3:8: cannot find package "github.com/go-martini/martini" in any of:
remote: /app/tmp/cache/go1.4.2/go/src/github.com/go-martini/martini (from $GOROOT)
remote: /tmp/build_ea6105408e12a560374800681cccb8e2/.heroku/g/src/gggggg/Godeps/_workspace/src/github.com/go-martini/martini (from $GOPATH)
remote: /tmp/build_ea6105408e12a560374800681cccb8e2/.heroku/g/src/github.com/go-martini/martini
remote: godep: go exit status 1

To fix it I had to force Godeps/_workspace folder into git repository, since Heroku buildpack expects dependencies to be delivered with app code.

Mailcatcher to test your email subsystem

You have probably heard of Mailcatcher already. It’s a great piece of software that implements neat idea.
I am using Vagrant and Apache on Debian, so my development box is build using puphpet.com. It allows mailcatcher installation btw, but we’ll do it manually and with a step forward:

  1. Get sqlite3 development libs: sudo apt-get install libsqlite3-dev;
  2. Got ruby? Install mailcatcher: sudo gem install mailcatcher;
  3. Now, let’s make it running after box restart:
    • Create /etc/init.d/mailcatcher script with this code:
      #! /bin/sh
      # Provides:          mailcatcher
      # Required-Start:    $all
      # Required-Stop:     $network
      # Default-Start:     2 3 4 5
      # Default-Stop:      0 1 6
      # Short-Description: Start daemon at boot time
      # Description:       Enable service provided by daemon.
      ### END INIT INFO
      start() {
      	echo -n "Starting MailCatcher"
      	if start-stop-daemon --stop --quiet --pidfile $PID_FILE --signal 0
      		echo " already running."
      	start-stop-daemon \
      		--start \
      		--pidfile $PID_FILE \
      		--make-pidfile \
      		--background \
      		--exec $PROG \
      		--user $USER \
      		--group $GROUP \
      		--chuid $USER \
      		-- \
      	echo "."
      	return $?
      stop() {
      	echo -n "Stopping MailCatcher"
      	start-stop-daemon \
      		--stop \
      		--oknodo \
      		--pidfile $PID_FILE
      	echo "."
      	return $?
      restart() {
      case "$1" in
      		echo "Usage: $0 {start|stop|restart}"
      		exit 1
    • Make the script executable: sudo chmod +x /etc/init.d/mailcatcher;
    • Create the user: sudo adduser mailcatecher;
    • Add the script for upstart: update-rc.d mailcatcher enable;

Ok, so mailcatcher will run at startup, but will not be visible at host machine. Now, you can do it just by forwarding guest’s 1080 port to host’s 1080 port using Vagrant config. But it didn’t work for me for no apparent reason. And i am not a big fan of typing in port numbers into browser address bar. So next steps will solve that:

  1. Create a new Apache “site”, let’s say “/etc/apache2/sites-available/35-mailcatcher.conf“;
  2. Let’s use Apache proxy module to let us access to mailcatcher (on port 80). Add this to the newly created “site” file:
    <VirtualHost *:80>
         ServerName mailcatcher.local
         ProxyPass /
         ProxyPassReverse /
  3. On your host machine update /etc/hosts file with this line (replace %GUEST_IP% with your Vagrant box IP address):
    %GUEST_IP% mailcatcher.local
  4. Enable mailcatcher site with a2ensite command, enable proxy module with a2enmod proxy_http command and restart apache;
  5. Point your browser to http://mailcatcher.local and your should see mailcatcher interface.
  6. If you see 503 error, it’s probably because mailcatcher did not start.

    Oh, yes, refer to the manual on how to add support to your language of choice. For PHP it’s simple:

    ; php.ini
    sendmail_path = /usr/bin/env catchmail -f [email protected]

The Stanley Parable

Oh i have written code today, alright. Whole day. Cracked the nut just two minutes before six. What a joy, when you do something that actually works, despite all odds and… inconveniences (i don’t want to rattle again about how “good” open source products sometimes are, there are too much complains in this blog already, and besides, i could just go and fix it instead of mumbling, it is free after all).

So instead, i would rather tell about the game. It’s called The Stanley Parable. And it is simply brilliant. The problem i face is that this is all i can tell you about it. Go play.

Recursive C #2

All day today i’ve been thinking how to implement reverse() function from yesterday post using no helper code. Still no result though. Except the realization, that this whole helper is very slow. Let’s make a test: reverse 103 character string 106 times, shall we?

time ./test2

real	0m26.846s
user	0m26.810s
sys	0m0.016s

Wow, that’s a lot. The reason for that is obviously the fact, that we call strlen() each time _rev() is called and strlen() has linear running time, so we are just adding that running time ever _rev() invocation without any gain because string length do not change during reverse() call. Not cool. We can add another constant parameter to carry initial string length:

static void _rev(char s[], int i, const int l) {
	int k = l - 1 - i;
	if (i > k) return;

	char c = s[i];
	s[i] = s[k];
	s[k] = c;

	_rev(s, ++i, l);

And then:

void reverse(char s[]) {
	_rev(s, 0, strlen(s));

Quite simple. Here’s how it performs:

time ./test1

real	0m2.830s
user	0m2.825s
sys	0m0.002s

Fastaaaa! Mind you, that’s recursive solution which has function call overhead (tail recursion, anyone?) and is just an exercise.

So yeah. Optimize-shmoptimize.

Just started the preprocessor chapters. That should be even funnier, because of all the bugs possibilities. Really looking forward to it.

That’s it for today.

Recursive C

Continuing with C. Last chapter i’ve finished is Recursion. There is an exercise which is to implement recursive function for reversing string in place. Here’s my solution:

void reverse(char s[]) {
	static int i;
	int k = strlen(s) - 1 - i;
	if (i > k) {
		i = 0;

	char c = s[i];
	s[i] = s[k];
	s[k] = c;


Which is a bit… cheating. And that’s due to the fact, that function is not supposed to keep any kind of state. Look at static int i; which i use to keep track of which position is currently exchanging. This is not very good. Is there a better way? If i would be using Scala, i could just create nested function. In C i have to use static function, which is essentially private.

Here is goes:

void reverse(char s[]) {
	_rev(s, 0);

static void _rev(char s[], int i) {
	int k = strlen(s) - 1 - i;

	if (i > k) return;

	char c = s[i];
	s[i] = s[k];
	s[k] = c;

	_rev(s, ++i);

Funny enough changing ++i in _rev()‘s last line, to i++ will lead to Segfault because recursion never ends. Why not stack overflow? Here is why.

That concludes recursion for now. Source file here: reverse.c.

That’s it for today.

Playing with Unity

Today i was playing with Unity. This is a game engine, which is simple to use and it can run on almost any platform (yeah, even WP8 and Wii). Sure enough this comes with it’s own price, both dollar and performance, though i don’t have any hard numbers on latter. But just the ability do “write once, run anywhere” is head-lightening. Especially because Windows Phone.

Just for the sake of learning something new i have followed a simple tutorial on Unity site on how to build collecting game. You control the ball with arrow keys and collect spinning boxes. Got them all – you win. It’s rather primitive, i mean, i can’t even play with friends or take payments from those players who have stuck. Anyway, here is the link to the game itself: Rollerball (needs UnityPlayer). Just don’t get too addicted.

Code is on the github. It uses C# since that what they have used in tutorial, but Javascript will do too (i guess they can be mixed in one project, however i don’t see why anyone would do that).

Building games is really hard task, even having tools like Unity, which does tons of work for you. Design, arts and testing will take years, unless you are doing time killer game, in which case it will take only months.

That’s it for today.

One day one code #17

Here we go again, skipped 5 days. Had a very nice trip to California: San Francisco, Mountain View, Santa Cruz and Carmel. I’ll have to write a post with photos later.

Today is ODOC time. I am done with the reverse Polish calculator from the last update. Here is it: calc.c. It wasn’t too hard. The main problem is lack of free time, frankly. As soon as you understand how things should work, it’s easy to extend existing solution.

Historical note: Reverse Polish notation

Switched from Vim to MacVim, which has all the same great features plus:

  • point and click;
  • mouse scroll;
  • font size adjustment;
  • “usual” copy-paste shortcuts.

That’s it for today.

One day one code #16

Time machine update: i’ve managed to get almost all my data back. Right now all 45Gb of music is downloading. Also in this time of cloud based services, like password managers and Dropbox, there is not many things you can actually loose. So computers are pretty much disposable (like Apple wants you to think about iPhone), it’s so easy to upgrade and safe. Almost always.

Back to the main topic of this blog. Coding. Bunch of stuff going on with Sonata. I am still pretty serious about writing a post about that later someday. On the other hand C learning is not going too fast. But i have managed to make progress on the calculator. Added modulus operation, negative numbers support. They also have added stack operations as a part of this exercise, so get(), clear(), swap() etc. are done too. The code is not thread safe by the way. Which is something i recon is hard to do (and is out of scope of current chapter). I will post the code in this blog when all things are done.

Tomorrow i am visiting San Francisco. So most probably will not have too much time to spend on coding. So i will post code-related photos.