Display current git branch in bash prompt(and get git/git-flow completion for free)

It is really helpful to know where you are currently within your project. Switching constantly between branches and typing ‘git branch’ or ‘git status’, just to get a branch name, could be a bit tiresome. But there is solution within git itself. You just need to do two things:

1) Download bash completion ‘extension’

It is very likely you installed git using brew. So, first get the latest git version:

brew upgrade git

and then install bash-completion:

brew install bash-completion

After install completes, you should see instruction how to modify your ~/.bash_profile file. You can always display this information using:

brew info bash-completion

brew info bash-completion

2) Modify your ~/.bash_profile file

If it is not already there in your home folder, create one:

mvim ~/.bash_profile

Add following lines at the end of the file:

if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi

PS1='u@h: W$(__git_ps1 "(%s)")$ '

Last line is most important one. It tells bash how to format prompt. Save your file with ‘:wq’ and then load it with ‘source ~/.bash_profile’. After all steps are successful you should see something like this in your terminal when you navigate to your code folder:

branch name

As a bonus you get git and git-flow completion for free!

Disclaimer: Your mileage may wary. Depending on your git installation and system settings, you might need to modify steps above in order to make it working on your computer.

Running Rails dev server on port 80

If you want to run your Rails development server on port 80, and you are using RVM, you would have to do something like this:

$rvmsudo rails s -p 80

It will ask you for your admin/root password if you have one, and that is it. Or if you are using passenger for development(I do), you
may find this ruby script usefull:

#!/usr/bin/env ruby

SERVER_PORT = 80
CMD_LINE = "rvmsudo passenger start -p #{SERVER_PORT} --user=#{ENV["USER"]}"

puts "Starting passenger server on port '#{SERVER_PORT}'...n#{CMD_LINE}"
system(CMD_LINE)

Copy/paste previous code, name ti something like start_passenger.rb, and give it exec privileges:

$chmod +x start_passenger.rb

Then you can simply start your development server by typing in root of your Rails app:

$ ./start_passenger.rb

wwwizer: Solve your naked domain problem. Easily put www in front of your domain name

I love services like wwwizer. It simple and solves real problem for so many users. It is so simple that you do not need user manual to use wwwizer. The best part is that service isfree!

Naked domain

Naked domain is simply a domain without ‘www’ prefix. In one of my previous posts you could read that there is a problem with naked domains. With most DNS providers you are not able to use myblog.com in CNAME record. What you can do to overcome this issue is to follow 2 easy steps:

  1. Create CNAME record pointing www.myblog.com to whatever your want.
  2. Create A record that points your naked domain to 174.129.25.170

Now all requests going to myblog.com will be redirected to www.myblog.comwww.myblog.com 
itself is CNAME. 174.129.25.170 is the IP address of wwwizer service. What wwwizer does is that it detects if domain starts with ‘www’. If not, it redirects request to URL with ‘www’ in it. Part of the URL after slash(“/”) is preserved, meaning service will work for URLs having different paths, parameters, and anchors.

wwwizer

If you have a ‘naked domain problem’ you should start using wwwizer right now. It is awesome service, and it just works.

High performance web apps with C++

It is Christmas over here, and it is time of giving. So I decided to give something back to the community. I guess this is as old as web programming itself. Trying to create C++ web framework. If you search on the web, you can find dozen of guys that had exactly the same idea. I am not different.

For one of the previous projects(discontinued now) I created simple web framework in C++. If you like to hack in C++, here it is. Code is provided as-is, as part of larger application. You can extract framework code itself, use only some portions of it, or use whole application, if you like. I do not offer any kind of support, so do not call me and do not find me liable for any damage or harm produced to you… But if you get rich and I find out that you were using my code, of course, I am gonna sue you :)

Idea with this framework is simple. All code, (including views) is compiled to one shared library(dll in windows parlance). Meaning whole application is contained within one .so file that is loaded to the Apache at the runtime. There is small glue layer, on top of the application itself, that acts as Apache module and calls code inside your module. Views itself, containing embedded c++, are precompiled to c++ source code, and finally included and compiled to final binary .so file. This used to work perfectly for me, but I found performance not to compensate for lack of flexibility of solutions like Rails(think that you have to compile whole code base, everytime you make some change). Here is the example of code in action:

#pragma once

#include 
#include "log/view.h"
#include "log/click.h"
#include "log/event.h"
#include "model/db_conf.h"

using namespace fenix::web::toolkit;

namespace LogController 
{ 
  FENIX_CONTROLLER(lg) 
  { 
    string site_id; 
    string event_type; 

    if( request.isRead() && get_param(request["_id"], site_id) && get_param(request["_e"], event_type)) 
    { 
      tables::Site::obj site = Site::get(get_database(request, "fenix"));

     //ID is 24 chars long 
    if(site_id.size() == 24 && site->exists(Query().add_cond("_id", site_id))) 
    { 
      if(event_type == "hit") 
      { 
        long last_view; //seconds ago 
        get_param(request["_tm"], last_view); 
        { 
          LogRequest log_request(site_id, event_type;
          log_request.load(request); 
          ScopedMiddleware mid; 
          log_page_view(log_request, last_view, request._timestamp, mid); mid.done(); 
        } 
        return render_(); 
      } 
      /* if(event_type == "cl") { log_click(); } if(event_type == "ev") { log_event(); }*/ 
    } 
  } 

  return render_(); 
  }
}

Or, for example, have a look at the router code here.

Source code

DNS 101 for developers

DNS stands for “Domain Name System”, but you could google that information yourself and find definition online. What you could not find online is very short and absolutely minimal DNS guide for developers. Here it is.

DNS Records

dns

Everything you should now about DNS boils down to one thing. DNS has records, and DNS server stores those records. Setting proper DNS record would ley you point your domain name(e.g. myblog.com) to your web application, your blog or your web service. Just think of DNS as one big lookup table. Domains names are keys, and server addresses are values.
Most likely you would deal with two types of records: A and CNAME record. learning how these two work, would be enough to successfully host your site or blog.

A record

‘A record’ points to the actual server on the Internet using its IP address. I guess this is the simplest form of DNS record, and it might look something like this:

A | myblog.com | 127.0.0.1

Domain name(myblog.com) is on the left side, and in case of A record, an IP address(127.0.0.1) is on the right side of the mapping. In real world example it would be an actual IP address of the server you are using to host your site, not the localhost’s address.
At this point you should begin to distinguish between domain name(myblog.com) and host/IP address(127.0.0.1). Domain name is just entry within DNS, and host is physical machine attached to the internet, identified by its IP address. Domain name associated with and IP address is called ‘hostname’. Typically there is one-to-many relationship between host(or its IP address) and domain name. One domain name points to one and only one address(if there is no Round-robin DNS in use), but one address could be associated with one or more different domain names. Take for the example two different blogs(myblog.com and someotherblog.com) hosted on save server having only one IP address.

CNAME record

CNAME stands for ‘canonical name’. To be honest I do knot know what that means(and do not care). What I care about is the fact that other than A record, CNAME is the most important type of DNS record, when it comes to hosting your blog. Basically it is an alias from your domain name to some another domain. For example:
me.myblog.com

CNAME | me.myblog.com | minime.someotherblog.com

Now your own domain name(me.myblog.com) points to completely different site and domain. For the end user this is 100% transparent. User sees me.myblog.com in the address bar, but actual content is fetched from minime.someotherblog.com.
But why is CNAME record that important? What would be the use case for this? If you want to host your blog on amazon S3, it is enough to create CNAME record pointing to your S3 bucket. Then you can use this new alias for your blog, not the original bucket’s name. Take for example this blog. It’s hosted on S3 and its bucket’s URL is http://www.brainfuck.rs.s3-website-us-east-1.amazonaws.com/. Yeah I know, it’s ugly. So I just created CNAME record that points www.brainfuck.rs => http://www.brainfuck.rs.s3-website-us-east-1.amazonaws.com/. Now, when you navigate to the www.brainfuck.rs what you actually see is the content of http://www.brainfuck.rs.s3-website-us-east-1.amazonaws.com/. Luckily for me, my business and amazon guys, browser in a no way acknowledges this :D Yes, you are reading this blog from Amazon S3!
But what about brainfuck.rs domain, one wihtout ‘www’ prefix. Unfortunately there is one slight issue with CNAME records. DNS providers usually won’t let you use CNAME with ‘naked’ domains. A naked domain is simply a domain name without the ‘www’ prefix. This means with CNAME record you can use www.myblog.com but not myblog.com. There is very simple and smart solution for this issue and it involves redirecting all request form myblog.com to www.myblog.com. In one the following posts I’ll talk about wwwizer, service that does this for free. Another rule with CNAME is to never use other CNAME as record’s value. This way you could create infinite ‘redirects’ or circular references that would take forever to resolve. Aliased domain name always has to resolve to an IP address.
Feel free to share this article with your colleagues and your team. There is still a lot of fuss about DNS among developers.

Caffeine to the people!

def sudo_get_me_a_coffee
  puts %q(
    88OOOOOZZZZZZZZZZOZZZZZZOOOZZOOOOO88888O
    OZZOZZZZZZZZZZZZZZZZZZZZZZZOOOOOZ7O8OO88
    ZZZZZZZ$$$$$ZZZZZZZZZZZZZZZZZZZO+OOOOOOO
    ZZZZZZZ$$$ZZZZZZ$$$$$$$ZZZZZZZ7IZOOOOOOO
    ZZZZZ$$$$$$Z$$$$$$$$+::::,,,O+ZZOOOOOOOO
    ZZZ$$$$$$$$$$$$$$$I?=~I?+=:,?:=OOOO888OO
    ZZ$$$$$$$$$$$$$77$:D88OZ77$OI=OOOOOO88OO
    ZZ$$$$$$$$$$$$$~,:,,,,,,::~+IZ88888888OO
    Z$$ZZZ$$$$$$$$7:~~,,::::~~=?7II?+8888OOO
    Z$ZZZZZ$$$$$77:==~::::~~~=?I8Z+=~=88OOOO
    $Z$ZZ$$$Z$$Z7$:=+=::::,::~?+++++?+D8OOOO
    ZZZOZ$Z$$$$$$$7Z:+=+:::::~~==?II?MND88D8
    ZOZZZZZ$$$$7$7I$$$$:,,::~~~~~+MNDND88OO8
    ZZ$Z$Z$$$$$7$7$7ZZ$Z$77$7ZZO8O88888O8OOO
    ZZZZZ$$$$$77$$777$7ZZZZZOOOOOO8888OO8O8O
    OZZZZ$$$$$$$7$777777$77$$ZZZEDKXCR77Z7AF
  )
end

P.S. If you stare for long enough, you could see a cup of coffee :D