Recently I’ve searched go web app frameworks to create hobby project. I’ve been found this article https://nordicapis.com/7-frameworks-to-build-a-rest-api-in-go. Then I’ve looked all seven project github repos. I like https://github.com/astaxie/beego project and start to develop app. I’ve been created API project and tried to deploy it to heroku. But there is no documention for it. So I’ve tried do that. This articles contains information about this process.

In order to deploy beego app to heroku you should use tools like https://github.com/kardianos/govendor.

1. After install govendor try following steps in your project folder;

$ govendor init
This command will create ./vendor/vendor.json file in current directory.

{
  "comment": "https://github.com/ismailakbudak/beego-api-example",
  "heroku": {
    "install" : [ "./..." ],
    "goVersion": "go1.11"
  },
  "ignore": "test",
  "package": [],
  "rootPath": "reporting-api"
}

Add heroku tag like above example. There is a information about this configuration on heroku docs in here https://devcenter.heroku.com/articles/go-dependencies-via-govendor#build-configuration

2. After this add beego package dependency with this command

$ govendor fetch github.com/astaxie/beego It will download the beego packages to ./vendor directory

3. Lastly you should configure listen ports and beego runmode as prod for heroku in main.go file
To deploy your app without problem, default config must be runmode = prod in conf/app.conf. I tried to set it default as dev and change it from heroku config vars as below, but it compiles packages before the set runmode and gives exception with this message panic: you are running in dev mode.

func main() {
    log.Println("Env $PORT :", os.Getenv("PORT"))
    if os.Getenv("PORT") != "" {
        port, err := strconv.Atoi(os.Getenv("PORT"))
        if err != nil {
            log.Fatal(err)
            log.Fatal("$PORT must be set")
        }
        log.Println("port : ", port)
        beego.BConfig.Listen.HTTPPort = port
        beego.BConfig.Listen.HTTPSPort = port
    }
    if os.Getenv("BEEGO_ENV") != "" {
        log.Println("Env $BEEGO_ENV :", os.Getenv("BEEGO_ENV"))
        beego.BConfig.RunMode = os.Getenv("BEEGO_ENV")
    }

    beego.BConfig.WebConfig.DirectoryIndex = true
    beego.BConfig.WebConfig.StaticDir["/"] = "swagger"

    beego.Run()
}

You can use BEEGO_ENV=dev bee run to continue develop your app without change it again.

You can find example app in here https://github.com/ismailakbudak/beego-api-example.

Leave a Reply

Your email address will not be published. Required fields are marked *