1
votes

I'm new to Golang and is trying to deploy a simple rest api to Heroku.

The code works locally after I build the code and run heroku local. However, the code does not work when I push it onto Heroku. In my browser, I see heroku's Application Error

I get following error logs using heroku logs --tail

the error code is "cannot execute binary file: Exec format error". I did a google search and from my reading, it says the compiled version is incompatible. If so, what should I do to get the correct binary to run on heroku? Thanks

2020-08-14T11:48:37.863717+00:00 app[web.1]: bash: bin/golang-gin-poc: cannot execute binary file: Exec format error
2020-08-14T11:48:37.909304+00:00 heroku[web.1]: Process exited with status 126
2020-08-14T11:48:37.946300+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-14T11:48:59.000000+00:00 app[api]: Build succeeded
2020-08-14T11:52:47.670135+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=go-hendry.herokuapp.com request_id=06309354-6516-4520-8800-5fa12b8b421b fwd="39.109.230.237" dyno= connect= service= status=503 bytes= protocol=https
2020-08-14T11:52:49.872040+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=go-hendry.herokuapp.com request_id=b8f0a6a6-06cc-4bef-a346-21b91ce024d2 fwd="39.109.230.237" dyno= connect= service= status=503 bytes= protocol=https
2020-08-14T12:03:31.503635+00:00 heroku[web.1]: State changed from crashed to starting
2020-08-14T12:03:32.679883+00:00 heroku[web.1]: Starting process with command `bin/golang-gin-poc`
2020-08-14T12:03:35.223469+00:00 app[web.1]: bash: bin/golang-gin-poc: cannot execute binary file: Exec format error
2020-08-14T12:03:35.263368+00:00 heroku[web.1]: Process exited with status 126
2020-08-14T12:03:35.306058+00:00 heroku[web.1]: State changed from starting to crashed

Below is my server.go code in case needed for reference

package main

import (
    "io"
    "net/http"
    "os"

    "github.com/gin-gonic/gin"
    "github.com/myhendry/goapi/controller"
    "github.com/myhendry/goapi/service"
)

var (
    videoService    service.VideoService       = service.New()
    videoController controller.VideoController = controller.New(videoService)
)

func setupLogOutput() {
    f, _ := os.Create("gin.log")
    gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
}

func main() {
    setupLogOutput()

    server := gin.New()
    server.Use(gin.Logger())

    apiRoutes := server.Group("/api")
    {
        apiRoutes.GET("/videos", func(ctx *gin.Context) {
            ctx.JSON(200, videoController.FindAll())
        })

        apiRoutes.POST("/videos", func(ctx *gin.Context) {
            err := videoController.Save(ctx)
            if err != nil {
                ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
            } else {
                ctx.JSON(http.StatusOK, gin.H{"message": "Video Input is Valid!!"})
            }

        })
    }

    port := os.Getenv("PORT")

    if port == "" {
        port = "5000"
    }

    server.Run(":" + port)
}
1
What platform are you building the binary on? If it's OSX, you'll need to cross-compile for linux. Or better yet, have heroku do the build step itself, don't push binaries. - Marc
yes. i m using OSX. i have a Procfile web: bin/golang-gin-poc. does that do the build on heroku? or i need to do something else? Thanks - Hendry Lim
I don't know. What are the contents of the procfile? - Marc
web: bin/golang-gin-poc - Hendry Lim
Then you're just pushing your binary to heroku. You need to either build a cross-compiled binary, or have heroku build from source. I'm sure their docs has details. - Marc

1 Answers

0
votes

Compile on local development machine Mac OS in terminal

env GOOS=linux GOARCH=amd64 go build -o bin/golang-gin-poc -v .

then push onto Heroku

i'm still looking for solution to do post-build on heroku