Archive

Archive for July, 2012

Install node.js on Raspberry PI in two simple steps

July 24, 2012 13 comments

[ Updated Jan 20, 2013 ] Check out my git repository https://github.com/midnightcodr/rpi_node_install if you are interested in putting newer version of node to raspberry pi (tested with 0.8.14, os runs raspbian)

1) Install the newest wheezy image onto the sd card (currently 2012-07-15-wheezy-raspbian.zip) from http://www.raspberrypi.org/downloads

2)

sudo apt-get install nodejs npm

, press y, Enter when prompted, you will get node.js (v0.6.19) and npm (1.1.4) installed in a few minutes, which should be the easiest way to get node.js and npm up and running on RP.

Note: If you want newer version of node.js you might want to check this out: http://www.raspberrypi.org/phpBB3//viewtopic.php?f=34&t=9929, I have tested the method described in this post in the previous release of wheezy (http://www.raspberrypi.org/archives/1435) and I assume it should work with the current image as well.

Categories: node.js, raspberry pi

Install Postgres on Mac OS X Lion with homebrew howto

July 20, 2012 3 comments

1) This is the easiest part, simply

$ brew install postgres

2) this step is not obvious but you can’t move on without it (would be nice if brew can automate this step too :D)

$ initdb /usr/local/var/postgres

3) add /usr/local/bin to $PATH if it’s not already there, one way to do that is

$ echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile && . ~/.bash_profile

without this step you will end up using /usr/bin/psql, which is installed by default in Lion, while trying to access db, which would give you the following error:

psql: could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket “/var/pgsql_socket/.s.PGSQL.5432”?

[added 7/26/2012]
3.5) start postgres server

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

4) access postgres db

psql postgres your_username

or

psql postgres `whoami`

assuming your_username is the one who runs step 2

5) if everything’s ok you should be able to see

psql (9.1.3)
Type “help” for help.

postgres=#

type \q to exit

6) if you want to create your first db, do (under system shell, not postgres) a

createdb mydb

and access it through

psql mydb your_username

Note: your_username in command psql is optional if the shell user is the creator of the db to be accessed, that is, you can simply type psql postgres or psql mydb to access the dbs.

Enjoy.

References:
http://archives.postgresql.org/pgsql-general/2009-09/msg00990.php
http://nextmarvel.net/blog/2011/09/brew-install-postgresql-on-os-x-lion/ (didn’t use the method describe in this post but use the PATH method instead)
http://www.postgresql.org/docs/9.0/static/tutorial-createdb.html

Categories: lion, mac osx, postgresql

PHP templating with Smarty

July 4, 2012 Leave a comment

I found using Smarty in PHP the most effective way to avoid spaghetti codes (codes that mix programming logic and html output that rely heavily on string concatenation), especially in the following two use cases
1) Generate html output in a structured, organized way
Let’s look at an example:

cat test.php

<?php
    require_once '/usr/share/php/smarty3/Smarty.class.php';
    // stock path installed with apt-get install smarty3 in ubuntu, you might need to adjust the require_once path
    // if you install smarty in other distros or through other means
    $smarty=new Smarty();
    $smarty->assign('label', 'my favorite tools');
    $smarty->assign('friends', array('node.js', 'underscore.js', 'backbone.js', 'Smarty', 'swift mailer', 'express'));
    $smarty->display('test.tpl');

cat templates/layout.tpl

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>{block name=title}{/block}</title>
        <link rel="stylesheet" href="/css/master.css" type="text/css" media="screen" charset="utf-8">
        {block name=morecss}{/block}
    </head>
    <body>
    {block name=body}{/block}   
    <script type="text/javascript" src="path/to/jquery.min.js" charset="utf-8"></script>
    {block name=morejs}{/block} 
    </body>
</html>

cat templates/test.tpl

{extends file='layout.tpl'}
{block name=title}{$label|capitalize}{/block}
{block name=morejs}
	<script type="text/javascript" src="path/to/my.main.js" charset="utf-8"></script>
{/block}
{block name=body}
Say hello to {$label}:
<ul>
	{foreach $friends as $friend}
	<li>{$friend}</li>
	{/foreach}
</ul>
{/block}

2) Prepare html (or plain text) email message to send out
cat email.php

<?php
    require_once '/usr/share/php/smarty3/Smarty.class.php';
    $smarty=new Smarty();
    $smarty->assign('url', 'http://a.link.to.reset/your/password');
    $message=$smarty->fetch('email/forget.tpl');
    // send_email_to($someaddress, $somesuject, $message);
    // you can echo it out as well
    // echo $message;

cat templates/email/master.tpl

<html>
	<body>
		<div id="content" style="width:400px;margin:40px auto"> 
			{block name=body}{/block}
		</div>
	</body>
</html>

cat templates/email/forget.tpl

{extends file='email/master.tpl'}
{block name=body}
<p>
Dear User,
</p><p>
Here's the url to reset your password:<br />
<a href="{$url}">{$url}</a>
</p><p>
&nbsp;
</p></p>
Sincerely,
</p></p>
Blah Blah
{/block}

Notes on directory structure:
1) in the above examples, the file/folder structure is as follows (generated through this little powerful tool called tree, installed via ‘apt-get install tree‘)

2) folders cache and templates_c are given the following ownership/perimssion:
ls -ld cache templates_c/
drwxr-xr-x 2 www-data www-data 4096 2012-07-03 18:23 cache/
drwxr-xr-x 2 www-data www-data 4096 2012-07-03 22:35 templates_c/

Categories: php