Showing posts with label heroku. Show all posts
Showing posts with label heroku. Show all posts

Friday, June 9, 2017

Deploy Golang Web Application to Heroku cloud Platform

Deploy Golang Web Application to Heroku cloud Platform


Here I will provide you the details to deploy simple golang web application on Heroku. 

I hope reader of this post has already installed Golang package and have basic knowledge about golang and basic skill about git not mandatory.


I am using go1.6 version on Linux-Ubuntu machine for this post.


sujin@sujin:~$ lsb_release -d

Description: Ubuntu 14.04.4 LTS

sujin@sujin:~$ go version

go version go1.6 linux/amd64

Setup Go directory structure and set GOPATH env variable. add GOPATH/bin to PATH env variable.


set working directory in GOPATH


sujin@sujin:~$ echo $GOPATH
/home/sujin/godir/work

create a simple web app in golang. For this example i create a folder called HerokuTest, inside a file called HerokuTest.go that contain program below

package main



import (

"fmt"

"net/http"

"os"
)

func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, " Hello World From Golang on Heroku")
}

func main() {
http.HandleFunc("/", homeHandler)

fmt.Println("Server Listening...")
err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)
if err != nil {
panic("ListenAndServe")
}
}

Compile and test the program locally

sujin@sujin:~/godir/work/src/blog/HerokuTest$ go build
sujin@sujin:~/godir/work/src/blog/HerokuTest$ ls
HerokuTest  HerokuTest.go
sujin@sujin:~/godir/work/src/blog/HerokuTest$ PORT=7070 HerokuTest 
Server Listening...

Go to the url http://127.0.0.1:7070/ from web browser it will print Hello World From Golang on Heroku in web page.

We should manage dependency package using Godep

install godep

go get github.com/kr/godep

save all golang dependencies

sujin@sujin:~/godir/work/src/blog/HerokuTest$ godep savesujin@sujin:~/godir/work/src/blog/HerokuTest$ ls Godeps HerokuTest HerokuTest.go

We should create Procfile to tell Heroku which command to execute on web

sujin@sujin:~/godir/work/src/blog/HerokuTest$ echo web: HerokTest > Procfile

Now its time to configure git for the project.

git init
git add .
git git commit -m "Heroku Test Project Initial Commit"
git remote add origin https://github.com/sujinsr/HerokuTest.git
git push origin master


Register in heroku website to get free account https://signup.heroku.com/?c=70130000001x9jFAAQ


install heroku tool belt

sujin@sujin:~/godir/work/src/blog$ heroku login
Enter your Heroku credentials.
Email: sujinsr@gmail.com
Password (typing will be hidden): 
Logged in as sujinsr@gmail.com


Pull the HerokuTest project from git

go get github.com/sujinsr/HerokuTest/...


Move to project directory and create heroku instance for your application




sujin@sujin:~/godir/work/src/github.com/sujinsr/HerokuTest$ heroku create heroku-test-app-blog -b https://github.com/kr/heroku-buildpack-go.git
Creating heroku-test-app-blog... done, stack is cedar-14
Setting buildpack to https://github.com/kr/heroku-buildpack-go.git... done
https://heroku-test-app-blog.herokuapp.com/ | https://git.heroku.com/heroku-test-app-blog.git

Now push your application to git heroku master

sujin@sujin:~/godir/work/src/github.com/sujinsr/HerokuTest$ git push heroku master
Counting objects: 14, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (14/14), 1.48 KiB | 0 bytes/s, done.
Total 14 (delta 2), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Fetching set buildpack https://github.com/kr/heroku-buildpack-go.git... done
remote: -----> Go app detected
remote: -----> Checking Godeps/Godeps.json file.
remote: -----> Installing go1.6... done
remote: -----> Running: godep go install -tags heroku ./...
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote: 
remote: -----> Compressing...
remote:        Done: 2.2M
remote: -----> Launching...
remote:        Released v3
remote:        https://heroku-test-app-blog.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy.... done.
To https://git.heroku.com/heroku-test-app-blog.git
 * [new branch]      master -> master

Check whether application running

sujin@sujin:~/godir/work/src/github.com/sujinsr/HerokuTest$ heroku ps
=== web (Free): HerokuTest (1)
web.1: up 2016/02/28 14:09:45 +0530 (~ 37s ago)

Check the aplication by running heroku open

sujin@sujin:~/godir/work/src/github.com/sujinsr/HerokuTest$ heroku open
Opening heroku-test-app-blog... done
Created new window in existing browser session.


Check the logs 

sujin@sujin:~/godir/work/src/github.com/sujinsr/HerokuTest$ heroku logs --tail
2016-02-28T08:38:42.408669+00:00 heroku[api]: Enable Logplex by sujinsr@gmail.com
2016-02-28T08:38:42.408669+00:00 heroku[api]: Release v2 created by sujinsr@gmail.com
2016-02-28T08:39:43.323594+00:00 heroku[api]: Scale to web=1 by sujinsr@gmail.com
2016-02-28T08:39:43.566013+00:00 heroku[slug-compiler]: Slug compilation started
2016-02-28T08:39:43.566018+00:00 heroku[slug-compiler]: Slug compilation finished
2016-02-28T08:39:43.414709+00:00 heroku[api]: Deploy 51ae268 by sujinsr@gmail.com
2016-02-28T08:39:43.414779+00:00 heroku[api]: Release v3 created by sujinsr@gmail.com
2016-02-28T08:39:44.250976+00:00 heroku[web.1]: Starting process with command `HerokuTest`
2016-02-28T08:39:45.250323+00:00 app[web.1]: Server Listening...
2016-02-28T08:39:45.844980+00:00 heroku[web.1]: State changed from starting to up

Read more »