To host your web application in the cloud, there are already quite a few different services and hosts, 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 performs various experiments studying the features of the language. Having chosen such selection criteria as ease of deployment, speed and convenience, I settled on Heroku.
For a single account, Heroku offers up to 5 applications for free. The system allocates 750 free hours of work per month to each application, it should also be taken into account that after an hour of “idle time” the application goes into sleep mode (but it will automatically be “awakened” when it receives a request).
1. Registration in the system and authorization
If you do not have a profile, create it by clicking on
this link . Next you need to download and install the
Heroku Toolbelt . Once installed, make sure that the
heroku
command works in your console. If everything works, open the terminal and enter the following:
$ heroku login Enter your Heroku credentials. Email: user@server.com Password: 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. Creating an application
The purpose 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" }) m.Run() }
I placed the source code in the
$GOPATH/github.com/user/hello/server.go
.
')
3. Creating a Procfile File
Procfile needs Heroku in order to know how to run the server. Let's place there one small line:
web: hello
Please note that if your source is located in a folder other than the hello folder, then the content will be slightly different:
web: < , >
4. Creating a local repository
In the
$GOPATH/github.com/user/hello/
folder, 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.
Install:
$ go get github.com/kr/godep
Go to our
$GOPATH/github.com/user/hello/
folder and execute:
$ godep save
As a result, a
Godep
folder will be created in which you will find a
Godep.json
file with a list of dependencies, as well as a
_workspace
folder with 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
Now the fun begins. If you left the
$GOPATH/github.com/user/hello/
folder, then go back. 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 BUILDPACK_URL=https://github.com/kr/heroku-buildpack-go.git 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 , save information about how it should be collected and deployed.
Making a 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 running application:
$ heroku open Opening secure-beyond-6735... done
All, the application is running on Heroku. In the future, you only need to fix the dependencies (if you start using new libraries), commit and push. In my opinion, very quickly, simply and conveniently. Here is described a similar way, but in my opinion it is a bit more complicated.
All links:
-
Heroku Signup-
Heroku Toolbelt-
Godep-
Go Heroku Buildpack