Home > Backend Development > Golang > Why is my Golang app on Heroku showing an 'Application Error' after a successful build?

Why is my Golang app on Heroku showing an 'Application Error' after a successful build?

Linda Hamilton
Release: 2024-12-08 01:33:12
Original
816 people have browsed it

Why is my Golang app on Heroku showing an

Deploying a golang app on heroku, build succeed but application error

When deploying a golang application on Heroku, it's important to ensure the application is bound to the correct port. Failure to do so can result in the application failing to start and displaying an "Application Error" message when accessing endpoints.

In your case, the logs indicate that the application is being killed within 60 seconds of launch due to the web process being unable to bind to the specified port. To resolve this issue, you need to bind your web server to the port specified by the $PORT environment variable. Heroku automatically sets this environment variable to the default HTTP and HTTPS ports, allowing your application to be publicly accessible.

To bind your HTTP server to the expected port, modify your code as follows:

import (
    "os"
    
    "github.com/gorilla/mux"
    "github.com/gorilla/context"

    "gopkg.in/paytm/grace.v1"

)

func main() {
    log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
    log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
    
    muxRouter := mux.NewRouter()
    
    muxRouter.HandleFunc("/", Articles)
    http.Handle("/", muxRouter)
    
    port := os.Getenv("PORT")
    if port == "" {
        port = "9000" // Default port if not specified
    }
    err := grace.Serve(":" + port, context.ClearHandler(http.DefaultServeMux))
    if err != nil {
        log.Println("[ERROR GRACEFUL]", err)
        os.Exit(1)
    }
    
    os.Exit(0)
}
Copy after login

By making this change, your application will now bind to the port specified by Heroku and be publicly accessible on default HTTP and HTTPS ports, resolving the "Application Error" issue.

The above is the detailed content of Why is my Golang app on Heroku showing an 'Application Error' after a successful build?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template