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) }
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!