Go + Heroku: Deploying a Web Application

    To host your web application in the cloud, there are already quite a few different services and hosting services, but only a few support working with Go. Among them, you can pay attention to the following:
    - Google App Engine
    - Heroku
    Some other services also offer Go support, but on a paid basis, which is not always beneficial for the developer, for example, if he conducts various experiments studying the features of the language. Choosing such selection criteria as ease of deployment, speed and convenience, I settled on Heroku.

    For one account Heroku offers up to 5 applications for free. For each application, the system allocates 750 free hours of work per month, it should also be borne in mind that after an hour of “downtime” the application goes into sleep mode (But it will automatically be “woken up” when you receive a request to it).

    1. Registration in the system and authorization

    If you don’t have a profile, create it by clicking on this link . Next, you need to download and install Heroku Toolbelt . After installing, make sure that the command works in your console heroku. If everything works, open the terminal and enter the following:
    $ heroku login
    Enter your Heroku credentials.
    Email: user@server.com
    Could not find an existing public key.
    Would you like to generate one? [Yn]
    Generating new SSH public key.
    Uploading ssh public key /Users/user/.ssh/id_rsa.pub

    2. Create an application

    The goal of the post is to show how to deploy the application in the cloud, so I will manage the simplest “Hello, World” using the martini framework:
    package main
    import "github.com/go-martini/martini"
    func main() {
    	m := martini.Classic()
    	m.Get("/", func() string {
    		return "Hello World"

    I placed the source code in a file $GOPATH/github.com/user/hello/server.go.

    3. Creating a Procfile File

    Heroku needs Procfile to know how to start the server. Let's place one small line there:
    web: hello

    Please note that if your source is located in a folder other than the hello folder, then the contents will be slightly different:
    web: <название папки, в которой расположен код приложения> 

    4. Creating a local repository

    In the folder, $GOPATH/github.com/user/hello/execute the following commands:
    $ git init
    $ git add -A .
    $ git commit -m "code"

    In the future, we will push from the local repository to the Heroku repository.

    5. Godep - saving dependencies

    godep is a special tool for managing package dependencies. It will allow you to save information about the packages that our project uses, and their source code.
    $ go get github.com/kr/godep

    Go to our folder $GOPATH/github.com/user/hello/and execute:
    $ godep save

    As a result, a folder will be created Godepin which you will find a file Godep.jsonwith a list of dependencies, as well as a folder _workspacewith the source codes of third-party packages.
    Make a commit:
    $ git add -A .
    $ git commit -m "dependencies"

    6. Creating an application on Heroku and deploying it

    Now the fun begins. If you left the folder $GOPATH/github.com/user/hello/, then return. Now in the terminal, do the following:
    $ heroku create -b https://github.com/kr/heroku-buildpack-go.git
    Creating secure-beyond-6735... done, stack is cedar
    http://secure-beyond-6735.herokuapp.com/ | git@heroku.com:secure-beyond-6735.git
    Git remote heroku added

    The team will create our application and, using Go Heroku Buildpack , will save information on how to build and deploy it.
    We do push:
    $ git push heroku master
    Initializing repository, done.
    Counting objects: 11, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (8/8), done.
    Writing objects: 100% (11/11), 1.29 KiB | 0 bytes/s, done.
    Total 11 (delta 0), reused 0 (delta 0)
    -----> Fetching custom git buildpack... done
    -----> Go app detected
    -----> Installing go1.3... done
    -----> Running: godep go install -tags heroku ./...
    -----> Discovering process types
           Procfile declares types -> web
    -----> Compressing... done, 1.7MB
    -----> Launching... done, v4
           http://secure-beyond-6735.herokuapp.com deployed to Heroku
    To git@heroku.com:secure-beyond-6735.git
     * [new branch]      master -> master

    Almost everything, we execute another command, Heroku will launch the application, then open the browser and go to the address of the working application:
    $ heroku open
    Opening secure-beyond-6735... done

    That's it, the application is running on Heroku. In the future, you will only need to fix the dependencies (if you start using new libraries), commit and push. In my opinion it is very fast, simple and convenient. A similar method is described here , but in my opinion it is a little more complicated.

    All links:
    - Heroku Signup
    - Heroku Toolbelt
    - Godep
    - Go Heroku Buildpack

    Also popular now: