Google App Engine: Setting up GAE Local Development Server

In this blog post I’m going to show you about install and setting up Google Cloud SDK in Local computer environment for Go / Golang web development, my current computer runs Ubuntu 14.04 as primary os but I’m quite sure that the following steps run on the latest Ubuntu as well.

Requirements :

  1. Ubuntu 14.04 or later
  2. Python 2.7.x or later
  3. Java 1.7 or later
  4. Git

Let’s start the journey :

Download Google Cloud SDK
Download Google Cloud SDK
Extract Google Cloud SDK to local folder
Extract Google Cloud SDK to local folder
Install Google Cloud SDK to local system
Install Google Cloud SDK to local system
Choose yes to improve Google Cloud SDK
Choose yes to improve Google Cloud SDK
Update environment variables / bashrc path
Update environment variables / bashrc path
Update environment variables / bashrc path
Update environment variables / bashrc path
Restart / Reload Bash
Restart / Reload Bash
GCLOUD Init and Login
GCLOUD Init and Login
GCLOUD Init and Login to browser
GCLOUD Init and Login to browser
GCLOUD Init and Login to browser
GCLOUD Init and Login to browser
GCLOUD Init and Login to browser
GCLOUD Init and Login to browser
Give Permission
Give Permission
Give Permission
Give Permission

22.WebLoggedIn

Terminal Logged in
Terminal Logged in
Select Project
Select Project
Check Development App Server
Check Development App Server
Generate ssh Public Key
Generate ssh Public Key
SSH Public Key
SSH Public Key
GitLab Settting
Gitlab Setting
GitLab Settting
GitLab Settting
GitLab SSH Pub key
GitLab SSH Pub key
Install GIT
Install GIT
Clone Code From Repository
Clone Code From Repository
Run development server
Run development server
Runs application
Runs application
Local Web Application
Local Web Application
Admin Server
Admin Server

Golang: Undertanding Basic of Concurrence Programming in the easiest way

In this blog post I’m gonna show you an easy example regarding basic concurrency in Golang programming, it will be an easy step-by-step approach to avoid misapprehend to the example (sample).

What is concurrency?

Concurrency is the composition of independently executing computations.

Concurrency is a way to structure software, particularly as a way to write clean code that interacts well with the real world.

It is not parallelism.

Rob Pike, Google

https://talks.golang.org/2012/concurrency.slide#6

Step 1:

package main

import (
	"fmt"
	"time"
)

func sayHello() {
	for x := 0; x < 10; x++ {
		fmt.Println("Hello ", x)
	}
}

func main() {
	start := time.Now()
	fmt.Println("Start at: ", start)
	fmt.Println("------------------------------------------------------")

	go sayHello()

	fmt.Println("------------------------------------------------------")
	fmt.Println("End at: ", time.Now())
	fmt.Println("Done at: ", time.Now().Sub(start))
}

Output:

basic1

In this  step we do not have output to be printed on the screen from sayHello() because the main process ends before the goroutine being executed. Golang program execution begins by initializing the main package and then invoking the function main. When the function main returns, the program exits. It does not wait for other (non-main) goroutines to complete.

A goroutine is a lightweight thread managed by the Go runtime.

Step 2

func main() {
	start := time.Now()
	fmt.Println("Start at: ", start)
	fmt.Println("------------------------------------------------------")

	go sayHello()

	time.Sleep(time.Second)

	fmt.Println("------------------------------------------------------")
	fmt.Println("End at: ", time.Now())
	fmt.Println("Done at: ", time.Now().Sub(start))
}

Output:

Basic2delay

The example above uses delay to make main function (process) waits the goroutine through 1 second before ends, 1 second is more than enough for Golang to print out all the lines on the screen. for the next step I’m gonna add  delay to the loop inside the sayHello() function so it will takes 9 seconds to be finished (and keep 1 second delay in the main function), let’s see what will take place.

Step 3

func sayHello() {
	for x := 0; x < 10; x++ {
		fmt.Println("Hello ", x)
		time.Sleep(time.Second)
	}
}

func main() {
	start := time.Now()
	fmt.Println("Start at: ", start)
	fmt.Println("------------------------------------------------------")

	go sayHello()

	time.Sleep(time.Second)

	fmt.Println("------------------------------------------------------")
	fmt.Println("End at: ", time.Now())
	fmt.Println("Done at: ", time.Now().Sub(start))
}

Output:

Basic3

Each loop waits for 1 second after printed the text, so in 1 minute it adequate to print 2 lines.

Step 4 Make main function wait goroutine to be done before it ends

We can make main function to wait all goroutines to be done before end it’s process without using primitive delay (time.Sleep(time.Second)) function.

package main

import (
	"fmt"
	"time"
	"sync"
)

func sayHello(wg *sync.WaitGroup) {
	for x := 0; x < 10; x++ {
		fmt.Println("Hello ", x)
		time.Sleep(time.Second)

		if x == 8 {
			wg.Done()
		}

	}
}

func main() {
	var wg sync.WaitGroup

	start := time.Now()
	fmt.Println("Start at: ", start)
	fmt.Println("------------------------------------------------------")

	wg.Add(1)
	go sayHello(&wg)

	wg.Wait()

	fmt.Println("------------------------------------------------------")
	fmt.Println("End at: ", time.Now())
	fmt.Println("Done at: ", time.Now().Sub(start))
}

Output:

basic5

Please pay attention to these lines:

var wg sync.WaitGroup
wg.Add(1) // Should be called before goroutine call
wg.Done()
wg.Wait()

Step 5 Go channel

Channels are the pipes that connect concurrent goroutines. You can send values into channels from one goroutine (ch<-) and receive those values into another goroutine (<-ch).

I don’t want to talk much about channel to avoid confusion, instead I will just show you several behavior of channel.

Receive operation <-ch in the main

package main

import (
	"fmt"
)

func sayHello(msg chan string) {

	msg <- "Hello Lorem ipsum1!"
	msg <- "Hello Lorem ipsum2!"
	msg <- "Hello Lorem ipsum3!"
	close(msg)
}

func main() {
	msg := make(chan string)

	go sayHello(msg)

	for value := range(msg) {
		fmt.Println(value);
	}

	// Or you could just uncomment following lines (but comments 'for' lines first)
	// fmt.Println("1:", <-msg)
	// fmt.Println("2:", <-msg)
	// fmt.Println("3:", <-msg)
	// fmt.Println("4:", <-msg) // empty or error if you did't close(msg)
}

Output:

channelx1

package main

import (
      "fmt"
)

func sayHello(msg chan string) {
      says := "Says: "
      for x := 0; x < 10; x++ {
          says += fmt.Sprintf("Hello-%d ,", x)
          fmt.Println("ok")
      }   

      msg <- says
}

func main() {
      msg := make(chan string)
      go sayHello(msg)
      fmt.Println(<-msg)
}

Output:

channel1

Receive operation (<-msg) in main() is a blocking operation, so the program won’t continue until a value is available.

Receive operation <-ch from goroutine to be printed by function

package main

import (
	"fmt"
)

func sayHello(msg chan string) {

	msg <- "hello world"

}

func printHello2(msg string) {
	fmt.Println(msg)

}

func main() {

	msg := make(chan string)

	go sayHello(msg)

	printHello2(<-msg)

}

Output:

exited1

Select

package main

import (
	"fmt"
	"time"
)

func main() {

	// For our example we'll select across two channels.
	c1 := make(chan string)
	c2 := make(chan string)

	go func() {
		time.Sleep(time.Second * 1)
		c1 <- "one"
	}()

	go func() {
		time.Sleep(time.Second * 2)
		c2 <- "two"
	}()

	for {

		select {
		case msg1, ok := <-c1:
			if ok {
				fmt.Println("received", msg1)
			}
		case msg2, ok := <-c2:
			if ok {
				fmt.Println("received", msg2)
			}
		case <-time.After(3000 * time.Millisecond):
			fmt.Printf("Timed out!")
			return
		}

	}
}

Output:

timeout

The select statement lets a goroutine wait on multiple communication operations.

A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.

Golang: Simple File Upload Using Go Languange

As a web programming language Go has capability to handle file upload from browser html form, in this post I will show you simple File Upload using Golang web programming.

  1. Create HTML Form upload src/webupload/upload.gtpl
    <html>
      <head>
             <title>Test Upload a File</title>
      </head>
      <body>
    <form enctype="multipart/form-data" action="http://127.0.0.1:9090/upload" method="post">
              {{/* 1. File input */}}
              <input type="file" name="uploadfile" />
    
              {{/* 2. Submit button */}}
              <input type="submit" value="upload file" />
          </form>
    
      </body>
      </html>
    
    

    1. GO Form
    *ignore unused token input
  2. Create go code to handle file upload src/webupload/upload.go
  3. package main
    
    import (
    	"crypto/md5"
    	"fmt"
    	"html/template"
    	"io"
    	"net/http"
    	"os"
    	"strconv"
    	"time"
    )
    
    func upload(w http.ResponseWriter, r *http.Request) {
    
    	if r.Method == "GET" {
    		// GET
    		t, _ := template.ParseFiles("upload.gtpl")
    
    		t.Execute(w, nil)
    
    	} else if r.Method == "POST" {
    		// Post
    		file, handler, err := r.FormFile("uploadfile")
    		if err != nil {
    			fmt.Println(err)
    			return
    		}
    		defer file.Close()
    
    		fmt.Fprintf(w, "%v", handler.Header)
    		f, err := os.OpenFile("./test/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
    		if err != nil {
    			fmt.Println(err)
    			return
    		}
    		defer f.Close()
    
    		io.Copy(f, file)
    
    	} else {
    		fmt.Println("Unknown HTTP " + r.Method + "  Method")
    	}
    }
    
    func main() {
    	http.HandleFunc("/upload", upload)
    	http.ListenAndServe(":9090", nil) // setting listening port
    }
    
    
  4. Create new folder for upload destination src/webupload/testfolder
  5. that’s all

Golang: The easy example of Go deferred function

Deferred function means that the function call is deferred after/until the surrounding function   done / returns.

Here is the basic and easy sample of defer uses in Golang (code):


package main

import "fmt"

var (
	a = 10
	b = 2
	c = 0
)

func multiply(aa int, bb int, c *int) {
	*c = aa * bb
	fmt.Printf("2. value of c: %v ", *c) // Print pointer value
}

func main() {
	defer multiply(a, b, &c) // Will be executed after main() finished

	fmt.Printf("1. value of c: %d \n", c)

}

Output:

1. value of c: 0 
2. value of c: 20 
Program exited.

Another Example about defer


package main

import "fmt"

func main() {
	defer fmt.Println("1")
	defer fmt.Println("2")
	defer fmt.Println("3")
	defer fmt.Println("4")
}

Output:

4
3
2
1

Deferred function calls are executed in Last In First Out order after the surrounding function returns.

Golang: Debuging Go code

In this post I will show you some easy (tricky) ways to debug go code (forget about gdb, it’s too hard to understand).

Print Struct as string

package main

import (
    "fmt"
)

type Person struct {
    Name, Address       string
    Age, Height, Weight int
}

func main() {
    Yusuf := Person{
        Name:    "Yoesoff",
        Address: "Bandung Barat",
        Age:     17,
        Height:  165,
        Weight:  70,
    }   

    fmt.Println(Yusuf) // Print struct as string
}

Output:

{Yoesoff Bandung Barat 17 165 70}
{Yoesoff Bandung Barat 17 165 70}

Use fmt.Printf to find Variable / Object type in Golang

fmt.Printf("%T\n",v)
package main

import (
    "fmt"
)

type Person struct {
    Name, Address       string
    Age, Height, Weight int
}

func main() {
    Yusuf := Person{
        Name:    "Yoesoff",
        Address: "Bandung Barat",
        Age:     17,
        Height:  165,
        Weight:  70,
    }   

     fmt.Printf("%T\n", A)
     fmt.Printf("%T\n", B)
     fmt.Printf("%T\n", Yusuf)
}

Output:

string
int
main.Person

Use reflect to find Variable / Object type in Golang

package main

import (
	"fmt"
	"reflect"
)

type Person struct {
	Name, Address       string
	Age, Height, Weight int
}

func main() {
	Yusuf := Person{
		Name:    "Yoesoff",
		Address: "Bandung Barat",
		Age:     17,
		Height:  165,
		Weight:  70,
	}

	Yuka := &Person{
		"Zha",
		"Bandung Barat",
		16,
		165,
		60,
	}

	A := 10
	B := 12
	C := A + B
	D := "I AM A BATMAN"

	fmt.Println(reflect.TypeOf(Yusuf))
	fmt.Println(reflect.TypeOf(Yuka))
	fmt.Println(reflect.TypeOf(*Yuka))
	fmt.Println(reflect.TypeOf(C))
	fmt.Println(reflect.TypeOf(D))
}

Output:

main.Person
*main.Person
main.Person
int
string

Using go-spew to debug Go data structures (Object and Variable)

Installation

$ go get -u github.com/davecgh/go-spew/spew

  package main

  import (
      "github.com/davecgh/go-spew/spew"
  )

  type Person struct {
      Name, Address       string
      Age, Height, Weight int
  }

  func main() {
      Yusuf := Person{
          Name:    "Yoesoff",
          Address: "Bandung Barat",
          Age:     17,
          Height:  165,
          Weight:  70,
      }

      A := 10
      B := 12
      C := A + B
      D := "Wakwau Golang si Golang"

      spew.Dump(Yusuf)
      spew.Dump(C)
      spew.Dump(D)
  }

Output:

(main.Person) {
Name: (string) (len=7) “Yoesoff”,
Address: (string) (len=13) “Bandung Barat”,
Age: (int) 17,
Height: (int) 165,
Weight: (int) 70
}
(int) 22

(string) (len=23) “Wakwau Golang si Golang”

Using godebug A cross-platform debugger for Go.

Installation:

$ go get github.com/mailgun/godebug

1. install-godebug

Setup Go executable binary path :

2. Setup Go Binari

3. Go-Debug-Running

Insert a breakpoint anywhere in a source file you want to debug:

_ = "breakpoint"

package main

  import "fmt"

  type Person struct {
      Name, Address       string
      Age, Height, Weight int
  }

  func main() {
      Yusuf := Person{
          Name:    "Yoesoff",
          Address: "Bandung Barat",
          Age:     17,
          Height:  165,
          Weight:  70,
      }

      _ = "breakpoint"

      A := 10
      B := 12
      C := A + B
      D := "Wakwau Golang si Golang"
      _ = "breakpoint"

      fmt.Println(A)
      fmt.Println(B)
      fmt.Println(C)
      fmt.Println(D)
      fmt.Println(Yusuf)
  }

Output:

6.Godebugx

PHP Vim error: zsh:1: command not found: phpmd (PHPMD)

I got following error on my PHP Vim text editor (error occurred due to zsh installation on my Ubuntu terminal):

MyZSH-PHPMD-Error

zsh:1: command not found: phpmd (PHPMD) 

And here is the easy way to fix the error:

  1.  vim ~/.zshrc
  2. Add following line to your .zshrc file

    PATH=~/.config/composer/vendor/bin:$PATH

    SolveMyZshError

  3. Reload your .zshrc configurationReloadZSHRC
  4. It’s fixed nowITSFIXEDNOW

Task Scheduling using Laravel 5

In this blog post I will show you an easy example about how to create a simple Laravel 5 Command-Line program to be executed regularly by particular time interval or specified time.

Laravel’s command scheduler allows you to fluently and expressively define your command schedule within Laravel itself. When using the scheduler, only a single Cron entry is needed on your server. Your task schedule is defined in the app/Console/Kernel.php file’s schedule method. To help you get started, a simple example is defined within the method.

Create Simple Laravel Command-Line

we will use two log files to store output text (I show you my real path to avoid confuse ) :

Cron log file :  /home/yusuf/general.log

Scheduler output file: /home/yusuf/htdocs/blog/storage/logs/scheduler.log

 

  • Create app/Console/Commands/SayHello.php.

    1. CreateSayHelloDotPHP
    Create a new PHP file
  • Let’s write the simple short php code

    vim app/Console/Commands/SayHello.php

    namespace App\Console\Commands;
    
      use Illuminate\Console\Command;
    
      class SayHello extends Command
      {
          protected $signature = 'sayhello';
          protected $description = 'Say Hello User';
    
          public function __construct()
          {
              parent::__construct();
          }
    
          public function handle()
          {
              $this->info('Hello user...! ('. date("Y-m-d H:i:s") .') ');
          }
      }
    

    vim app/Console/Commands/Kernel.php

     /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
     protected $commands = [
         Commands\SayHello::class
     ];
    
  • Check out the new registered Laravel 5 Command-Line in the command list

    3. checkNewCommand
    New registered Laravel Command-line
  • Execute SayHello as Laravel 5 Command-Line program

    4. ExecuteIt
    Execute command line on terminal

Create Scheduler to Execute Command-Line

The next important step is about how to execute our Laravel Command-Line regularly. By using the Laravel scheduler you only need to add the following short line Cron entry to your server an then Laravel Forge will manage the Cron entries for you:

  • Create a log file for our Laravel scheduler storage/logs/scheduler.log:5. Laravel-Logs
  • Edit app/Console/Commands/Kernel.php   

    vim app/Console/Commands/Kernel.php

  • /**
     * Define the application's command schedule.
     *
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     * @return void
     */
     protected function schedule(Schedule $schedule)
     {
         $filePath = '/home/yusuf/htdocs/blog/storage/logs/scheduler.log';
         $schedule->command('sayhello')->everyMinute()->sendOutputTo($filePath);
     }
    
  • Test execute the scheduler

    ./artisan schedule:run6.scheduler-registered

  • Register schedule command as cron entry

    $ crontab -e7.registerCronEntry.png

     

    CronTabBlog1

  • Save the file and quit text editor
  • Tail the log file to see the result

    tail -f ~/general.log

    tail -f /home/yusuf/htdocs/blog/storage/logs/scheduler.log

    CrontabOutput2

  • That’s all

PGAdmin: Installing pgAdmin4 on Ubuntu Linux using Docker

logo

In this post I am going to share my experience regarding pgAdmin4 installation using existing Docker image from https://github.com/thaJeztah/pgadmin4-docker on my laptop Ubuntu Linux 14.04, I am sure that this sample is working for latest Ubuntu as well as 14.04 since I use docker to run the application, I consider docker is the easiest and the fastest way to install pgAdmin4 that can be done in seconds (thanks for pgadmin4-docker).

Welcome to pgAdmin 4. pgAdmin is the leading Open Source management tool for Postgres, the world’s most advanced Open Source database. pgAdmin 4 is designed to meet the needs of both novice and experienced Postgres users alike, providing a powerful graphical interface that simplifies the creation, maintenance and use of database objects.

  1. Follow docker installation in here
  2. Check your docker installation

    check-docker

  3. And then make sure that you have proper access to the docker commands, if you got following errors below so you need to do this additional steps :

    Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.26/containers/json: dial unix /var/run/docker.sock: connect: permission denied

    docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.26/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
    See ‘docker run –help’.

  4. If your docker is successfully installed and configured then we can just move to the next step to run pgAdmin4 on Ubuntu 14.04 using following docker image.

    docker run –rm -p 5050:5050 thajeztah/pgadmin4

    consolle-installedresult

Docker: Run Docker without sudo

In this post I will show you about how to use Docker without using sudo command.

by the way, these are errors you may have if you run Docker commands without enough permission access:

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.26/containers/json: dial unix /var/run/docker.sock: connect: permission denied

or an error like this:

docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.26/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See ‘docker run –help’.

Snapshot of docker permission Errors :

Error1Error2

To fix this kind of issue is fairly easy, you just need to add your user to docker group so your current user account will have access to the docker command, here is step by step instruction to add your user to docker group:

  1. Make sure that your docker service is running

    sudo /etc/init.d/docker status

  2. Create new docker group (in case  the previous one is not exist)

    sudo groupadd docker

    groupadd: group ‘docker’ already exists

  3. add your current logged in user t the docker group

     sudo usermod -aG docker $USER

  4. Restart docker daemon / service

    sudo service docker restart

  5. re-login your current user

    su ${USER}

    error3.png

  6. That’s all, have fun…

Golang: Simple Golang Web Application

In this post I’d like to show you a simple web application developed using Go / Golang that demonstrate you several basic web  functionalities such as http request and http response.

The result should be something like this that runs in your web browser:

golang-x1golang-x2

main.go

package main

  import (
      "fmt"
      "html/template"
      "net/http"
      "os"
  )

  type FormData struct {
      Name    string
      Country string
  }

  func form(w http.ResponseWriter, r *http.Request) {
      folderpath, _ := os.Getwd() // Get current folder location
      form, _ := template.ParseFiles(folderpath + "/form.gtpl")
      form.Execute(w, nil)
  }

  func view(w http.ResponseWriter, r *http.Request) {
      if r.Method == "POST" {

          // Process form data
          r.ParseForm()
          name := r.Form["name"][0] // Yes, Go able to receive more than one name
          country := r.Form["country"][0]
          //fmt.Println(name)
          //fmt.Println(country)

          data := FormData{
              Name:    name,
              Country: country,
          }   

          folderpath, _ := os.Getwd() // Get current folder location
          view, _ := template.ParseFiles(folderpath + "/view.gtpl")
          view.Execute(w, data)
      } else {
         fmt.Fprintf(w, "Method not allowed!") // write data to response
      }
  }

  func main() {
      // Routes
      http.HandleFunc("/", form)
      http.HandleFunc("/view", view)

      http.ListenAndServe(":8000", nil) // setting listening port
  }

form.gtpl

<html>

      <body>
<h1>Hallo World</h1>
<form method="POST" action="/view">
              Name : <input name="name" />
              Coutry : <input name="country" />

              <input type="submit" />
          </form>

      </body>

  </html>

view.gtpl

 <html>
  <head>
  <title></title>
  </head>
      <body>
          Hello World 

          Welcome -{{.Name}}- from -{{.Country}}-
      </body>
  </html>

Build and Run The code

golang x3

Code Snapshot

VIMX1VIMX2VIMX3