Deployment

Heroku makes deploying applications easy. It is a perfect platform for small tomedium size web applications that are willing to sacrifice a little bit offlexibility in infrastructure to gain a fairly pain-free environment fordeploying and maintaining web applications.

I am choosing to deploy our web application to Heroku for the sake of thistutorial because in my experience it has been the fastest way to get a webapplication up and running in no time. Remember that the focus of this tutorialis how to build web applications in Go and not getting caught up in allof the distraction of provisioning, configuring, deploying, and maintaining themachines that our Go code will be run on.

Getting setup

If you don't already have a Heroku account, sign up atid.heroku.com/signup. It's quick, easy and free.

Application management and configuration is done through the Heroku toolbelt,which is a free command line tool maintained by Heroku. We will be using it tocreate our application on Heroku. You can get it fromtoolbelt.heroku.com.

Changing the Code

To make sure the application from our last chapter will work on Heroku, we willneed to make a few changes. Heroku gives us a PORT environment variableand expects our web application to bind to it. Let's start by importing the"os" package so we can grab that PORT environment variable:

  1. import (
  2. "net/http"
  3. "os"
  4. "github.com/russross/blackfriday"
  5. )

Next, we need to grab the PORT environment variable, check if it is set, andif it is we should bind to that instead of our hardcoded port (8080).

  1. port := os.Getenv("PORT")
  2. if port == "" {
  3. port = "8080"
  4. }

Lastly, we want to bind to that port in our http.ListenAndServe call:

  1. http.ListenAndServe(":"+port, nil)

The final code should look like this:

  1. package main
  2. import (
  3. "net/http"
  4. "os"
  5. "github.com/russross/blackfriday"
  6. )
  7. func main() {
  8. port := os.Getenv("PORT")
  9. if port == "" {
  10. port = "8080"
  11. }
  12. http.HandleFunc("/markdown", GenerateMarkdown)
  13. http.Handle("/", http.FileServer(http.Dir("public")))
  14. http.ListenAndServe(":"+port, nil)
  15. }
  16. func GenerateMarkdown(rw http.ResponseWriter, r *http.Request) {
  17. markdown := blackfriday.MarkdownCommon([]byte(r.FormValue("body")))
  18. rw.Write(markdown)
  19. }

Configuration

We need a couple small configuration files to tell Heroku how it should run ourapplication. The first one is the Procfile, which allows us to define whichprocesses should be run for our application. By default, Go will name theexecutable after the containing directory of your main package. For instance,if my web application lived in GOPATH/github.com/codegangsta/bwag/deployment, myProcfile will look like this:

  1. web: deployment

Specifically to run Go applications, we need to also specify a .godir file totell Heroku which dir is in fact our package directory.

  1. deployment

Deployment

Once all these things in place, Heroku makes it easy to deploy.

Initialize the project as a Git repository:

  1. git init
  2. git add -A
  3. git commit -m "Initial Commit"

Create your Heroku application (specifying the Go buildpack):

  1. heroku create -b https://github.com/kr/heroku-buildpack-go.git

Push it to Heroku and watch your application be deployed!

  1. git push heroku master

View your application in your browser:

  1. heroku open

原文: https://codegangsta.gitbooks.io/building-web-apps-with-go/content/deployment/index.html