Home > Backend Development > Golang > Why Does My GoLang Heroku App Return an 'Application Error' Despite a Successful Build?

Why Does My GoLang Heroku App Return an 'Application Error' Despite a Successful Build?

Susan Sarandon
Release: 2024-12-05 10:00:20
Original
937 people have browsed it

Why Does My GoLang Heroku App Return an

Deploying a GoLang App on Heroku: Troubleshooting "Application Error"

Question:

Deploying a GoLang app on Heroku using GoDep support, the build succeeds but accessing the endpoint returns an "Application Error." The app runs on port 9000 on localhost, but grace.Serve seems to be listening on a different port.

Heroku Log Output:

2019-07-08T05:03:48.131507+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
Copy after login

Application Code:

err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
Copy after login

Answer:

The issue is that the application is explicitly binding its webserver to port 9000, which conflicts with Heroku's assigned port. Heroku assigns a dynamic port through the environment variable PORT.

To resolve this, the application should dynamically assign the port based on the PORT environment variable instead of hardcoding port 9000.

Revised Application Code:

port := os.Getenv("PORT")
if port == "" {
    port = "9000" // Default port if not specified
}
err := grace.Serve(":" + port, context.ClearHandler(http.DefaultServeMux))
Copy after login

The above is the detailed content of Why Does My GoLang Heroku App Return an 'Application Error' Despite 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