caffeine addicts' code shots

Tags: du
Categories: cli

You can use du(disk usage) command line tool:

Dushans-MacBook-Air:projects dushan$ du -hs s3cms/
921M s3cms/

'h' is for human readable output, eg. M for megabytes, and 's' is for summarized output(do not display size of subdirectories or specific files within directory).

Categories: programming

Ok, this one is gonna be a short one. Short but tricky. Pay attention to repeated backslashes(four of them) in replace pattern: 

"string containing 'single quotes'".gsub(/'/, "\\\\'")

Or, if you are on Rails, you can use one of its helpers: escape_javascript

Tags: ruby, rails
Categories: programming

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


Tags: dns, wwwizer
Categories: hosting

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 is free!

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.com and www.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. 

Categories: programming

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 < fenix.h >
#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_< TrackingPixel >();
      }
      /* if(event_type == "cl") { log_click(); } if(event_type == "ev") { log_event(); }*/
    }
  }
   
  return render_< BadRequestResponse >();
  }
}

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

Source repo: https://bitbucket.org/dushan01/fenix