2014-11-04

How to start Revel, GORM with PostgreSQL project on Archlinux

Revel is one of many Go Web Application Framework that uses Go programming language. After installing Archlinux, first of all you'll need to install yaourt, the easiest way to install AUR packages. After that you'll need to run this command:

yaourt -Sy --noconfirm go postgresql postgresql-libs liteide chromium google-chrome-stable

Then setup your GOPATH and GOROOT environment variables on your .bashrc

export GOROOT=/usr/lib/go
export GOPATH=$HOME/Dropbox/go
export PATH="$PATH:$GOPATH/bin"


Change the permission of this folder:

sudo chmod -Rv a+rwx /usr/share/liteide/liteenv

Then open LiteIDE, select menu View > Edit Environment, then add this line and save:

GOROOT=/usr/lib/go

Add also a GOPATH directory on menu View > Manage GOPATH...
That is ~/Dropbox/go on my computer.

To test if your Go and LiteIDE working correctly, create a new .go file and press Build > FileRun to run, for example:

package main
import "fmt"
func main() {
  fmt.Println("Hello my PC <3")
}

The code above should give an output on Build output window.

To install revel, type this on shell:

go get -u -v github.com/revel/cmd/revel
go get -u -v github.com/revel/revel


To create a new project (for example, the name would be "puis"), type:

cd $GOPATH
revel new puis

Then you could start the app using this command:

revel run puis

And visit http://localhost:9000 to view your app.

Next, we should install PostgreSQL driver and GORM, type:

go get -u -v github.com/lib/pq
go get -u -v github.com/jinzhu/gorm

Then initialize and start your database server:

sudo su - postgres
initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/postgres/data'
exit
sudo systemctl enable postgresql
sudo systemctl start postgresql
sudo su - postgres
createuser puis
createdb puis
psql 
GRANT ALL PRIVILEGES ON DATABASE puis TO puis;
\q
exit

Then create a model, for example in app/models/user.go

package models
import "time"
type User struct {
  Id                int64
  Name              string
  Gmail             string
  PuMail            string
  Password          string
  CreatedAt         time.Time
  UpdatedAt         time.Time
  DeletedAt         time.Time
  LastLoginAt       time.Time
  PasswordUpdatedAt time.Time
}

And create a controller for GORM in app/controllers/gorm.go

package controllers
import (
  "database/sql"
  "github.com/jinzhu/gorm"
  _ "github.com/lib/pq"
  "github.com/revel/revel"
  "puis/app/models"
)
type GormController struct {
  *revel.Controller
  Tx *gorm.DB
}
var Db gorm.DB
func InitDB() {
  var err error
  Db, err = gorm.Open("postgres", "user=puis sslmode=disable")
  if err != nil {
    revel.ERROR.Println("FATAL", err)
    panic(err)
  }
  tab := &models.User{}
  Db.AutoMigrate(tab)
  Db.Model(tab).AddUniqueIndex("idx_user__gmail", "gmail")
  Db.Model(tab).AddUniqueIndex("idx_user__pu_mail", "pu_mail")
}
func (c *GormController) Begin() revel.Result {
  txn := Db.Begin()
  if txn.Error != nil {
    panic(txn.Error)
  }
  c.Tx = txn
  revel.INFO.Println("c.Tx init", c.Tx)
  return nil
}
func (c *GormController) Commit() revel.Result {
  if c.Tx == nil {
    return nil
  }
  c.Tx.Commit()
  if err := c.Tx.Error; err != nil && err != sql.ErrTxDone {
    panic(err)
  }
  c.Tx = nil
  revel.INFO.Println("c.Tx commited (nil)")
  return nil
}

func (c *GormController) Rollback() revel.Result {
  if c.Tx == nil {
    return nil
  }
  c.Tx.Rollback()
  if err := c.Tx.Error; err != nil && err != sql.ErrTxDone {
    panic(err)
  }
  c.Tx = nil
  return nil
}

Also call those functions when application starts also before and after controller calling, add these lines in app/init.go on func init()

revel.OnAppStart(controllers.InitDB)
revel.InterceptMethod((*controllers.GormController).Begin, revel.BEFORE)
revel.InterceptMethod((*controllers.GormController).Commit, revel.AFTER)
revel.InterceptMethod((*controllers.GormController).Rollback, revel.FINALLY)

Two more things, add a testing code, for example in app/controllers/app.go on func Index()

user := &models.User{Name: "Kiswono Prayogo"}
c.Tx.NewRecord(user)
c.Tx.Create(user)
return c.Render(user) 

Last thing, just add one more line anywhere on Index.html

{{.user.Name}}

That's it, this is how to connect Revel to PostgreSQL using GORM. This tutorial adapted from Ivan Black's stackoverflow answer.

To solve slow Revel hot-reload or Go build/install, just use this command:

go get -u -v github.com/mattn/go-sqlite3

2014-08-23

How to change resolution that doesn't exists on display settings on Linux

Sometimes we need to change the resolution of our monitor, but the resolution doesn't show up on the xfce4-display-settings, what should we do? First you'll need a terminal or command prompt, then type cvt or gtf command with your desired resolution, for example:

cvt 1920 1080 

it would show something like this:

# 1920x1080 59.96 Hz (CVT 2.07M9) hsync: 67.16 kHz; pclk: 173.00 MHz Modeline "1920x1080_60.00" 173.00 1920 2048 2248 2576 1080 1083 1088 1120 -hsync +vsync

now, you should add it using xrandr command:

xrandr --newmode "1920x1080"  173.00  1920 2048 2248 2576  1080 1083 1088 1120 -hsync +vsync

now, assign it the desired display port from the graphics card, first let see our graphic card available ports, type xrandr again without parameter, it would show something like this:

Screen 0: minimum 8 x 8, current 1600 x 1200, maximum 32767 x 32767
VGA1 connected primary 1600x1200+0+0 (normal left inverted right x axis y axis) 0mm x 0mm
   1024x768      70.07 +  85.00    75.08    60.00 
   1600x1200     60.00* 
   1280x1024     85.02    75.02 
   1440x900      59.89
   1280x960      85.00
   1280x800      59.81
   1152x864      75.00 
   1024x768      86.96 
   832x624       74.55
   800x600       85.06    72.19    75.00    60.32    56.25 
   640x480       85.01    75.00    72.81    66.67    60.00
   720x400       87.85    70.08
HDMI1 disconnected (normal left inverted right x axis y axis)
VIRTUAL1 disconnected (normal left inverted right x axis y axis)
  1920x1080 (0x2ee) 173.000MHz
    h: width  1920 start 2048 end 2248 total 2576 skew 0 clock  67.16KHz
    v: height 1080 start 1083 end 1088 total 1120 clock  59.96Hz

Now we know that we have 3 ports: VGA1, HDMI1 and VIRTUAL1, of course the current monitor was plugged into VGA1 because it shows "connected", now we could assign using this command:

xrandr --addmode VGA1 1920x1080 

and set the display resolution using this command:

xrandr --output VGA1 --mode 1920x1080

Voila, now your resolution changed!

Note: if you're using VGA Video Splitter, make sure that the monitor plugged into the first port, so it would send correct EDID.

SLock simplest screen locker ever!

SLock is the simplest screen locker ever, it works without hassle or configuration. If you havent isntall any of screensaver (gnome-screensaver, xscreensaver), you could use it as the default screensaver. B-b-b-but how to unlock the screen? there's no password box to type '____' ?

You just need to type your password (screen would change color as you type first letter) and press enter to unlock the screen.

What if I have inputted wrong password? just press backspace or enter to restart inputting the password.

How to lock the screen? just type slock on terminal or run-dialog.

Well.. that's all for now~

Where to put fonts on Linux

To install fonts on Linux (especially Arch Linux), you must copy the fonts on the ~/.local/share/fonts folder (for old distribution, use ~/.fonts directory). To view fonts, install gnome-font-viewer, that program could also be used to install fonts locally. I have selected some fonts that good for programming imho. Contact me (if you can ^^ myahahah!) if the server is down or expired, or just google for these fonts:

BPmono.ttf BPmonoBold.ttf BPmonoItalics.ttf DejaVuSans-Bold.ttf DejaVuSans-BoldOblique.ttf
DejaVuSans-ExtraLight.ttf DejaVuSans-Oblique.ttf DejaVuSans.ttf DejaVuSansCondensed-Bold.ttf DejaVuSansCondensed-BoldOblique.ttf
DejaVuSansCondensed-Oblique.ttf DejaVuSansCondensed.ttf DejaVuSansMono-Bold.ttf DejaVuSansMono-BoldOblique.ttf DejaVuSansMono-Oblique.ttf
DejaVuSansMono.ttf DejaVuSerif-Bold.ttf DejaVuSerif-BoldItalic.ttf DejaVuSerif-Italic.ttf DejaVuSerif.ttf
DejaVuSerifCondensed-Bold.ttf DejaVuSerifCondensed-BoldItalic.ttf DejaVuSerifCondensed-Italic.ttf DejaVuSerifCondensed.ttf DroidSansMono.ttf
Envy Code B 10pt.ttf Envy Code R Bold.ttf Envy Code R Italic.ttf Envy Code R VS Italic-as-bold.ttf Envy Code R VS.ttf
Envy Code R.ttf FantasqueSansMono-Bold.ttf FantasqueSansMono-BoldItalic.ttf FantasqueSansMono-RegItalic.ttf FantasqueSansMono-Regular.ttf
FiraMono-Bold.ttf FiraMono-Medium.ttf FiraMono-Regular.ttf FiraSans-Bold.ttf FiraSans-BoldItalic.ttf
FiraSans-Book.ttf FiraSans-BookItalic.ttf FiraSans-Eight.ttf FiraSans-EightItalic.ttf FiraSans-ExtraBold.ttf
FiraSans-ExtraBoldItalic.ttf FiraSans-ExtraLight.ttf FiraSans-ExtraLightItalic.ttf FiraSans-Four.ttf FiraSans-FourItalic.ttf
FiraSans-Hair.ttf FiraSans-HairItalic.ttf FiraSans-Heavy.ttf FiraSans-HeavyItalic.ttf FiraSans-Italic.ttf
FiraSans-Light.ttf FiraSans-LightItalic.ttf FiraSans-Medium.ttf FiraSans-MediumItalic.ttf FiraSans-Regular.ttf
FiraSans-SemiBold.ttf FiraSans-SemiBoldItalic.ttf FiraSans-Thin.ttf FiraSans-ThinItalic.ttf FiraSans-Two.ttf
FiraSans-TwoItalic.ttf FiraSans-Ultra.ttf FiraSans-UltraItalic.ttf FiraSans-UltraLight.ttf FiraSans-UltraLightItalic.ttf
LiberationMono-Bold.ttf LiberationMono-BoldItalic.ttf LiberationMono-Italic.ttf LiberationMono-Regular.ttf LiberationSans-Bold.ttf
LiberationSans-BoldItalic.ttf LiberationSans-Italic.ttf LiberationSans-Regular.ttf LiberationSerif-Bold.ttf LiberationSerif-BoldItalic.ttf
LiberationSerif-Italic.ttf LiberationSerif-Regular.ttf MesloLGLDZ-Bold.ttf MesloLGLDZ-BoldItalic.ttf MesloLGLDZ-Italic.ttf
MesloLGLDZ-Regular.ttf MesloLGMDZ-Bold.ttf MesloLGMDZ-BoldItalic.ttf MesloLGMDZ-Italic.ttf MesloLGMDZ-Regular.ttf
MesloLGSDZ-Bold.ttf MesloLGSDZ-BoldItalic.ttf MesloLGSDZ-Italic.ttf MesloLGSDZ-Regular.ttf Monaco.ttf
PTM55F.ttf PTM75F.ttf VeraMono-Bold-Italic.ttf VeraMono-Bold.ttf VeraMono-Italic.ttf
VeraMono.ttf drucifer_0.ttf drucifer_i.ttf edlo.ttf mplus-1c-black.ttf
mplus-1c-bold.ttf mplus-1c-heavy.ttf mplus-1c-light.ttf mplus-1c-medium.ttf mplus-1c-regular.ttf
mplus-1c-thin.ttf mplus-1m-bold.ttf mplus-1m-light.ttf mplus-1m-medium.ttf mplus-1m-regular.ttf
mplus-1m-thin.ttf mplus-1mn-bold.ttf mplus-1mn-light.ttf mplus-1mn-medium.ttf mplus-1mn-regular.ttf
mplus-1mn-thin.ttf mplus-1p-black.ttf mplus-1p-bold.ttf mplus-1p-heavy.ttf mplus-1p-light.ttf
mplus-1p-medium.ttf mplus-1p-regular.ttf mplus-1p-thin.ttf mplus-2c-black.ttf mplus-2c-bold.ttf
mplus-2c-heavy.ttf mplus-2c-light.ttf mplus-2c-medium.ttf mplus-2c-regular.ttf mplus-2c-thin.ttf
mplus-2m-bold.ttf mplus-2m-light.ttf mplus-2m-medium.ttf mplus-2m-regular.ttf mplus-2m-thin.ttf
mplus-2p-black.ttf mplus-2p-bold.ttf mplus-2p-heavy.ttf mplus-2p-light.ttf mplus-2p-medium.ttf
mplus-2p-regular.ttf mplus-2p-thin.ttf

How to Install VirtualBox 4.3.14 on ArchLinux

VirtualBox is one of many virtualization software that could be run on Linux, one with slowest cpu performance according to this article. Installing VirtualBox is quite simple, but to make it run you must also install certain package. To install VirtualBox, type:

yaourt --needed --noconfirm -S --force virtualbox virtualbox-host-modules linux-headers

To make it able to run, you must create the kernel module first, the easiest way is using dkms:

sudo dkms autoinstall

or

sudo dkms vboxhost/4.3.14

then enable it using:

sudo modprobe vboxdrv

Now you can start and run VirtualBox images without error.
if you want to auto-recompile when installing new kernel, use this command:

sudo systemctl enable dkms


Installing Go 1.3.1 with LiteIDE x23.2 on Arch Linux

So I installing new computer on my company, using Arch Linux as usual.

How to install Go? it's really easy when using yaourt, just type:

yaourt --needed --noconfirm -S --force go mercurial git bzr subversion liteide gdb

Go took about 40.76 MB
Mercurial, Git, Bzr and Subversion requires about 13.12 MB
LiteIDE took about 13.14 MB
Gdb took about 3.06 MB

Set your working directory:

mkdir ~/go
export GOPATH=~/go
export PATH=$PATH:~/go/bin


I suggest that you should put those environment variables on .bashrc

Then you can test the installation, create a file named test1.go

package main
import "fmt"
func main() {
  fmt.Println("Hello my lovely PC :3")
}


Run it using:

go run test1.go 

or

go build test1.go && ./test1

or press Ctrl+R on LiteIDE.

2014-08-21

Which program that uses the bandwidth the most?

So, my boarding house internet connection has been down for 4 days now, and I decided to find alternate internet connection. On Indonesia it's really hard to find mobile internet provider that has unlimited bandwidth without FUP. I decided to use one with limited quota (8GB/month) T__T seriously.. 8GB it's my daily usage, not monthly. So I buy a router with modem support (TP-Link Wireless N Router TL-MR3420) a modem (Huawei E173) and GSM card for internet (Three). Long story short, I want to know which program that uses so much bandwidth since the beginning, so I install nethogs). That program should be used as root, and the first argument is default to eth0. Nethogs will show the list of process that uses most bandwith.




2014-08-11

How to make git ignore changes on commited files?

Sometimes there are some file that we want to ignore some file on git, of course we could use .gitignore, but that only take effect on untracked files. When the files already tracked/commited, we could make that file changes ignored using git update-index command, for example:

git update-index --assume-unchanged file-to-be-ignored

From now on, that file would not appear on git status or git diff when changed. When you want to track back the changes, use --no-assume-unchanged flag.

What to do when git pull conflict before commit?

Sometimes we want to pull from git without committing current changes, and yes, the correct way is to commit our changes first before pulling, so we could merge the conflict. But if you doesn't want to do that, there are some trick that you could use, that is git stash, the usage example:

# to store all changes on the stash
git stash
# this should no longer conflict
git pull
# to overwrite from previous stash
git stash pop
# you could see the difference using
git diff

2014-08-05

How to setup SSH Tunneling (SOCKS) Proxy

Sometimes we need to connect to some site via a very secure way without our nearby computers able to see (or sniff) which sites we are visiting, or to prevent any blocking from our LAN's firewall. One easy solution to solve this, but you'll need a VPS (Virtual Private Server) with public IP address of course, all you have to do is start a SSH connection to your VPS, for example:

sudo ssh -D my_local_port my_vps_user@my_vps_public_ip

for example:

ssh -D 8081 aurora@w.x.y.z

Then, setup your browser to use SOCKS to localhost:my_local_port, for example:


Voila, now everything that you browse will encrypted through to w.x.y.z.