Prasenjit Kumar Nag a.k.a Joy

A Developers Adventure in Coding

Using MAMP Pro’s VHosts Feature to Run Rails App in a Fancy Domain Name

I have been playing with ROR(Ruby On Rails) from last few days. Those who of you have idea about the the nature of Rails apps know that, to run a rails app you have to run the following command in the terminal from inside the apps directory.

1
rails server

And the app will start running on http://0.0.0.0:3000 or http://localhost:3000 WEBrick LightWeight Web Server that Rails ships with by default.

As I have left http://localhost/ long ago and been using fancy local domain names for all my projects, so I was searching for a solution on how can I do that with rails apps to. You know it doesn’t feel good to type in some thing like http://0.0.0.0:3000 in browser . Instead I would love some domain name like http://d.rails.dev  or something like that. It’s just feels good typing that.

And I found a solution. :) Here’s how to do it. In Mamp pro’s control panel in the host tab create a new host. Write http://d.rails.dev as the server name. The disk location isn’t important here but I have set it to my rails app directory. Now switch over to the advanced tab in right configuration panel and paste in the following code block in Customized Virtual Host General Settings option box:

1
2
3
4
5
6
ServerName rails
ServerAlias *.dev

ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
ProxyPreserveHost On

As you can see from the above code it’s actually forwarding all traffic in / which is http://d.rails.dev into http://localhost:3000 (Where our actual rails app is running). Click apply and you are done.

Now start the rails app using rails server  command from terminal from within the rails app directory and point your browser into http://d.rails.dev and see your rails app is running there.

Pretty easy huh! But it provides a good amount of self satisfaction, well, at least for me. I like seeing fancy domain names for my web apps.

I haven’t been able to test the process for MAMP but it shouldn’t be much different, though you have to do everything manually from adding vhost entry into httpd.conf  and then add the host name entry into /etc/hosts file. (I will be writing another post on how to create a virtual host in mamp/lamp sometime soon).

Thanks for taking the time to read up to this point. :)

Using MAMP’s Mysql Installation With Rails App

Being primarily a PHP developer I always have one AMP package installed in my machine no matter what platform I work with. Now-a-days as I work on my macbook I have MAMP installed.

Few days back I was trying to install mysql2 gem to use in Rails app which acts as the interface between MySql engine and Rails(or Ruby) apps. But it was constantly failing every time I tried to. I even tried to install previous versions of mysql2 gem but no luck. Then I tried to have a closer look at the error the bundle install command was spitting out into the terminal.

It was not being able to find mysql header files which are required to compile mysql2 gem. After searching in the internet I found out MAMP doesn’t ship mysql headers by default.

So The options I had was either recompile Mysql from source and add headers into proper MAMP Directory. Or manage another mysql installation only for rails which is a whole lot of pain. So I had to go for one, I searched through Stack Overflow and found some tutorials on how to do that. After studying some of those I finally took a backup of mamp and started the recompilation process. Here are the steps I followed:

Take a backup of your whole MAMP directory if by any chance you mess it up. :) It’s always safe.

  1. First you need to download the MAMP open source components from their source forge page here

http://sourceforge.net/projects/mamp/files/mamp/ download the components for mamp 2.0.2 that is the latest now.

  1. Recently MAMP have switched from MySql 5.1 to to MySQL 5.5. And MySQL, in turn, has switched from GNU Make (with ./configure) to CMake (with cmake .). So you need to have cmake installed in you Mac. You can do that easily using brew or macport using the following command
1
2
3
4
5
brew install cmake

or

port install cmake
  1. Suppose You have downloaded it into your desktop then Extract and put the mysql-5.5.9.tar.gz file into your desktop and type in the following command in your terminal.
1
2
3
4
5
6
7
8
9
10
$ cd /tmp
 $ mv /Users/yourname/Desktop/mysql-5.5.9.tar.gz .
 $ tar xf mysql-5.5.9.tar.gz
 $ cd mysql-5.5.9
 $ cmake . -DMYSQL_UNIX_ADDR=/Applications/MAMP/tmp/mysql/mysql.sock -DCMAKE_INSTALL_PREFIX=/Applications/MAMP/Library
 $ make -j  3
 $ cp libmysql/*.dylib /Applications/MAMP/Library/lib/
 $ mkdir -p /Applications/MAMP/Library/include/mysql
 $ cp include/* /Applications/MAMP/Library/include/mysql
 $ sudo env ARCHFLAGS="-arch x86_64" gem install mysql2 -- --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config
  1. after this you should see 1 Gem installed successfully message into the termnal and you gem has been install successfully. Now you need to symlink a bundle from your mysql Library into /user/lib. Run the following command in the terminal.
1
2
3
sudo ln -s /Applications/MAMP/Library/lib/libmysqlclient.16.dylib /usr/lib/libmysqlclient.16.dylib [/bash]
<div></div>
<div>5. Now clean up your temp directory</div>

bash

rm -rf /tmp/mysql-5.5.9 /tmp/mysql-5.5.9.tar.gz

“`

and you are done. Now you can try bundle install or  rake db:create
and they should work fine. :)
Happy reusing MAMP’s mysql with your rails applications :)

How to Use the Same Config File Both in Localhost and in Live Server

Every projects have some kind of configuration such as database username, database password, database name blah blah no matter whats language it’s been written in.

But i will be primarily talking about php here.

In past what I used to change the config file every time I had to push updates into the live server either by FTP or SFTP or  VCS(Version Control System) and that was painful. But thanks to the internet I found some tricks somewhere in the internet that I dont remember right now which I have been using since then which allows me to use the same config file both in localhost and in live server.

Lets say we have the  following config values which are different when the code is running in Localhost and in server. Say for localhost they have the following values

1
2
3
4
5
var $dbUsername = 'local';

var $dbPassword = 'localpassword';

var $dbname = 'local_db_name';

And in server the value is different as follows

1
2
3
4
5
var $dbUsername = 'server';

var $dbPassword = 'serverpassword';

var $dbname = 'server_db_name';

Now as I have already said changing those before every push is a pain in the a**. The way we can have them both at the same time and both environment runs properly is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if($_SERVER['REMOTE_ADDR']=='127.0.0.1'){

//we are in localhost as the user's IP is 127.0.0.1

var $dbUsername = 'local';

var $dbPassword = 'localpassword';

var $dbname = 'local_db_name';

}else{

// we are in server

var $dbUsername = 'server';

var $dbPassword = 'serverpassword';

var $dbname = 'server_db_name';

}

FYI, $_SERVER[‘REMOTE_ADDR’] returns users IP address if the user is not using Proxy.

If you are using Virtual host then you can use $_SERVER[‘SERVER_NAME’]. Suppose I have a virtual host setup for all my office projects as d.noobis.com then we can use the following conditionals to determine the correct environment

1
2
3
4
5
6
7
8
9
10
11
12
13
if($_SERVER['SERVER_NAME'] == 'd.noobis.com'){

// my local config goes here

}else if($_SERVER['SERVER_NAME'] == 'noobis.com'){

// Live servers config

}else if($_SERVER['SERVER_NAME'] == 'local.noobis.com'){

//someone else's config who is working with me in the same project have a VHost set up as local.noobis.com

}

Using this we can remove the burden of editing config file before every commit/upload of codebase into live server. This same technique can be used for frameworks which uses pure php variables for configuration options like Code Igniter.

But if your framework uses special formats for config files like YML then there might be different ways to separate development and production environment settings.

Hope you enjoyed the post. :)

UPDATE: 

As Zaman-A-Piri-Pasa suggested in the comment, if you are on windows using XAMPP use

1
if($_SERVER['REMOTE_ADDR']==::1)

instead to check for localhost.

Responding to Orientation Changes in Iphone Application Using Titanium

I am learning Titanium and Just Loving it. Earlier I thought i wont be able to do many things using titanium and started learning Obj-C and Cocoa-touch framework. And that path needs lots of study and learning before someone can build something really productive.

But the Scenario is different in case of Titanium. If you have basic programming concepts and have some experience with JavaScript you can start right away after a couple hours of study. And Titanium creates the native Obj-C codes for you. So there is basically no difference in an Iphone App made with Obj-C using UIKit framework with one built using Titanium. And even better is that you can deploy your Iphone App code In Andriod Platform with few changes. Supports for Blackberry is coming. So Titanium is a very good Parser itself, it takes you code written in JavaScript And Creates the Obj-C code for Iphone and Java Code for Android for you. As Titanium acquired Aptana few days ago We can expect that we will get a very good IDE for developing Titanium Apps with code Sense, debugging features and who know what else, lets wait and see what they brings.

And the amazing thing is that it’s open source. They have an example app which have all the features Available in Iphone ranging from simple TableViews to 3D animations.

Anyway, let’s talk about the Title of this post. You know Iphone,Ipod Touch, IPad have accelerometers built-in. That means it can detect orientation changes. Now responding to orientation changes Is a very important feature that every Iphone Apps should have. Doing it is not tough with Titanium. Here is a code block, which will respond to device orientation changes by changing the UI orientation. Place it anywhere in you app.js file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Ti.Gesture.addEventListener(
'orientationchange', function(e) {
// alert("Called");
win.orientationModes = [
Titanium.UI.LANDSCAPE_LEFT,
Titanium.UI.LANDSCAPE_RIGHT,
Titanium.UI.PORTRAIT,
Titanium.UI.UPSIDE_PORTRAIT
];
if (e.orientation == Titanium.UI.LANDSCAPE_LEFT ||
e.orientation == Titanium.UI.LANDSCAPE_RIGHT) {
// Do anything you wanna do when user changes the orientation to LandScape
Ti.UI.orientation = Titanium.UI.LANDSCAPE_RIGHT;

} else {
// Revert the changes you made, the user is back to portrait mode
Titanium.UI.orientation = Titanium.UI.PORTRAIT;
}
});

The code is pretty straightforward. We are attaching an event listener into the Ti.Gesture and once the orientation of the device changes this callback is fired and we can do anything we want into that callback. I will try to write small posts on titanium as i go along learning Titanium.

Got a New Apple Macbook

Hi Guys, I got my new macbook a few days ago. It’s new but refurbished. One of my friend bought me this from the US. And those who of you are planning to buy a laptop or macbook it’s better to buy it from abroad if you have the chance. Because It costs much less then it will usually cost here in Bangladesh. Let me tell you something about the Refurbished apple products. I bought this model. [caption id=“attachment_22” align=“aligncenter” width=“200” caption=“Macbook”]Macbook[/caption] This is originally $999 now. But the refurb is only $849 now. And it changes depending on the availabiliy of refurbs. SO a product is considered refurbished when it’s back to apple store in either of these ways: The shippers don’t get anyone’s order. Someone return the products as there is returning facilities. The products had some very simple problem and the buyer returned it

And the list goes on………..

After a product is back Apple engineers, the test it fully replace(no repair) the defective parts and run the product cycle and after fully ensuring Apples quality it makes the product available in the refurb section. Though there are also bad experience about refurb mac products and you can find those if you google it for a while.

Anyway it’s your game whether u will buy refurb or a brand new product. But the refurb can save u as much as $200 in some models.

The one i bought from US is avaiable in Bangladesh in ComputerSource and they costs 120000BDT. So it’s only around 60000BDT more. My macbook costs $849 and US charged $74 maybe as TAX.

So if you think u need a macbook with a affordable price , i would suggest you to buy macbooks from abroad and you can risk refurbished (Hasin vie is using a refurb iMac for one year, not a single problem ) and he suggested me to a refurb mac. And it’s better if you buy it from a country other than US, they chargesd me almost a $100 tax.You can search for a country where the tax is less.

Anyway, I am happy with my macbook and enjoying time with it. Whatever guys says Apple is this Apple is that there are some special things about Apple products and you will feel that if you use those.

That’s all for now. if you need to know anything more you can drop a comment and you can also bug in IM anytime.

Boot Slax From Your USB Flash Drive

Slax is a lightweight linux based distro with some important features. It’s possible to boot this from a USB flash drive. Just follow the steps bellow and enjoy your time with Slax 6.0.4. 1. Download the package for flash drive from here
2. Unzip the .tar file to the root directory of your Flash drive. 3. Navigate to the boot directory of your flash drive. 4. Double click on the file bootinst.bat and follow the instruction. 5. Reboot the system and enter the boot menu to boot from the flash drive. 6. Enter the live session and install to your HDD if you like. Enjoy your time with slax.

My First Post About ACM Problem

Hi, Guys this is my first post about acm problems. As this is my first post I am going to publish one of my AC code. It’s a very easy problem. But the title seems to be horrible. You just calculate the difference between the input and print the output.

The solution of problem No. 10055

The code:

1
2
3
4
5
6
7
8
9
10
11
12
13
//10055.cpp

#include <iostream.h>
#include<stdio.h>

int main() {
double a,b;
while (cin<<a<<b) {
if (a>b) printf("%.0fn",a-b);
else printf("%.0fn",b-a);
}
return 0;
}

Just copy n paste it. You will get accepted.