Archive for the ‘node’ Category

git hook to reject bad emails on the server.

Posted on February 19th, 2012 in git, node | 2 Comments »

In my git workflow I use git-commit-notifier to email beautiful, color-coded, diff emails out to the dev group anytime there’s a commit. Problem is, if a new engineer didn’t set his git author email properly that email will get silently rejected from the email group.

I finally got around to creating a git commit hook to reject the commit from the start, and inform the developer to update his or her git author email.

To use, simply create a pre-receive file in your remote repos hooks folder and make it executable.
I wrote this in Node so you’ll need to have Node installed on your server if it’s not there already:

#! /bin/env node
 
var fs = require('fs'),
arguments = fs.readFileSync('/dev/stdin').toString().split(' ');
exec = require('child_process').exec;
var email = 'yourcompany.com';
 
exec('git log -1 ' + arguments[1] + ' --format=%ae', function(err, stdout, stderr){
    if (stdout !== '' && stdout.indexOf(email) == -1){
            console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@');
            console.log('Author email must contain ' + email + '! Your email is currently set to: ' + stdout);
            console.log('Please update by running: git config --global user.email "name@' + email + '"');
            console.log('And then: git commit --amend --reset-author');
            console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@');
            process.exit(1);
        }
        process.exit(0);
});

Nginx, Apache, and Node all living harmony.

Posted on July 15th, 2011 in Apache, nginx, node | 3 Comments »

So here’s two problems you want to solve:

You want to optimize static content

You have an Apache install that’s hosting a bunch of sites and friend’s sites through vhosts. One of your blogs is getting a lot of hits and you want to optimize it’s static content – or even the static content of all sites. You’re not quite ready for a CDN-type deal, just to place the content outside of Apache and into something more lighter-weight so it’s not running through WordPress / Apache and unnecessarily using up threads (at roughly 2mb a thread).

You want all your web apps on port 80!

You started really getting into Node (or Ruby on Rails or Django) but every web app needs to be binded to it’s own port and port 80 is taken by your Apache which is hosting a lot. You don’t want to be giving out the url: http://mycoolnewapp.com:81.

Sure you can use Apache’s proxypass but you’re gaining overhead and, in case of Node (or Ruby’s EventMachine or Python’s Twisted), you’re losing the whole point of having an optimized, non-blocking / non-threaded, web app.

The solution

Nginx! Nginx is a web server, reverse-proxy, load balancer, and mail server all in one. Like Node, it was built with the concept of the event loop, not threads so it’s highly optimized for high concurrency. The idea is to setup Nginx to be a reverse-proxy for all your other services.

I’ll skip over how to install Nginx as it’s pretty straightforward and you can google it. I’ll go over the main steps to getting Apache to work through Nginx – it’s truly easy, I did it on my first try. The only problem that I encountered is that since the user only interfaces with Nginx – it’s Nginx that is making the requests to Apache / Node. So from Apache’s perspective, all requests are coming from 127.0.0.1. We also fix this in the steps below.

  1. Once Nginx is installed, edit your /etc/httpd/conf/httpd.conf so that it listens on another port. Say 127.0.0.1:8080.
  2. Switch all your vhosts to also listen on this port.
  3. Edit your /etc/nginx/nginx.conf file. Make sure it’s set to listen on port 80 and add a server entry per each apache vhost. Here’s an example:
server {
           listen       80;
           server_name  mysite.com;

       location / {
                    proxy_pass http://127.0.0.1:8080;
                    include /etc/nginx/conf.d/proxy.conf;
           }
    }
  1. Next we’ll add that proxy.conf reference by creating /etc/nginx/conf.d/proxy.conf:
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 8m;
client_body_buffer_size 256k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 32 256k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 256k;
  1. At his point you need to install mod-rpaf for Apache. This enable Apache to use the extra headers Nginx is passing in the request. If you’re using a flavor of Linux that uses apt-get you’re in luck. Just run: sudo apt-get install libapache2-mod-rpaf. If you’re using a system that uses Yum, you’ll have to compile it yourself. Just follow the steps here.

As for serving static content. Inside each “server” declaration you can add the following (modified to your taste):

location ~* ^.+\.(jpg|jpeg|gif|png|ico|tgz|gz|pdf|rar|bz2|exe|ppt|txt|tar|mid|midi|wav|bmp|rtf) {
            root /folder/to/static/files;
            expires 90d;
       }
       location ~* ^.+\.(css|js)$ {
            root /folder/to/static/files;
            expires 30d;
      }

Ad that’s it you’re done!

Additional reading

Nginx Primer

Nginx Primer 2: From Apache to Nginx

Apache with Nginx

Really solid config samples

Event-driven programming vs traditional programming

Posted on May 2nd, 2011 in node, Uncategorized | No Comments »

Great post explaining the hype around Node and what exactly is event-driven programming and this business of non-blocking I/O. This kind of system has been around for years (see Python’s Twisted and Ruby’s Event Machine), but never has it been so easy to do with JavaScript’s easy syntax and support for first class functions. As JS dev’s we’re already used to the whole asynchronous mindset of not-blocking the page from the user.