在 heroku 上部署 golang 应用程序,构建成功但应用程序错误
Deploying a golang app on heroku, build succeed but application error
我的 golang 应用程序在本地主机的 9000 端口上运行。在使用 godep 支持在 heroku 上部署它之后,我能够在 heroku 上推送和部署它。但是,当我尝试访问端点(例如“/”)时,它显示应用程序错误。在 heroku
部署时,您可以在下面看到我的代码和日志
package main
import (
"log"
"fmt"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/gorilla/context"
"gopkg.in/paytm/grace.v1"
// utilhttp "bitbucket.org/michaelchandrag/chit/pkg/util/http"
// util "bitbucket.org/michalechandrag/chit/pkg/util"
)
func main() {
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", Articles)
http.Handle("/", muxRouter)
// err = grace.Serve(":"+cfg.Server.Port, context.ClearHandler(http.DefaultServeMux))
err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
if err != nil {
log.Println("[ERROR GRACEFUL]", err)
os.Exit(1)
}
os.Exit(0)
}
func Articles(w http.ResponseWriter, r *http.Request) {
// vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Hello")
// r.Close = true
// w.Header().Set("Content-Type", "application/json")
// w.Header().Set("Access-Control-Allow-Origin", "*")
/*if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
if err := fn(w, r); err != nil {
log.Println(err)
apiObject := ConstructAPIError(http.StatusInternalServerError, ErrGeneral, SysMsgErrGeneral, MsgErrGeneral)
SendAPIObject(w, apiObject)
return
}*/
}
heroku 在部署时记录日志
-----> Go app detected
-----> Fetching jq... done
-----> Fetching stdlib.sh.v8... done
-----> Checking Godeps/Godeps.json file.
-----> New Go Version, clearing old cache
-----> Installing go1.12.6
-----> Fetching go1.12.6.linux-amd64.tar.gz... done
-----> Running: go install -v -tags heroku ./...
bitbucket.org/michaelchandrag/chit/pkg
bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/context
bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/mux
bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/tylerb/graceful.v1
bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/paytm/grace.v1
bitbucket.org/michaelchandrag/chit/pkg/util
bitbucket.org/michaelchandrag/chit
Installed the following binaries:
./bin/chit
-----> Discovering process types
Procfile declares types -> web
-----> Compressing...
Done: 7.5M
-----> Launching...
Released v3
https://michaelchandrag-project.herokuapp.com/ deployed to Heroku
访问端点前后的应用日志
2:47.954106+00:00 heroku[web.1]: Starting process with command `chit`
2019-07-08T05:02:49.413453+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T05:02:49.413476+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T05:02:49.413647+00:00 app[web.1]: 2019/07/08 05:02:49 starting serve on :9000
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
2019-07-08T05:03:48.131595+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-07-08T05:03:48.214979+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-08T05:03:48.193205+00:00 heroku[web.1]: Process exited with status 137
2019-07-08T10:38:59.721224+00:00 heroku[web.1]: State changed from crashed to starting
2019-07-08T10:39:00.359017+00:00 heroku[web.1]: Starting process with command `chit`
2019-07-08T10:39:02.232435+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T10:39:02.232458+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T10:39:02.232583+00:00 app[web.1]: 2019/07/08 10:39:02 starting serve on :9000
2019-07-08T10:40:00.462841+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-07-08T10:40:00.462974+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-07-08T10:40:00.555959+00:00 heroku[web.1]: Process exited with status 137
2019-07-08T10:40:00.573427+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-08T13:33:25.021835+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=michaelchandrag-project.herokuapp.com request_id=e6b5c93a-7177-4080-9858-ed925a20c513 fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:33:25.566036+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=michaelchandrag-project.herokuapp.com request_id=35c741a9-be4d-48f6-ba99-ba24ddc58b9b fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:48:38.294082+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=michaelchandrag-project.herokuapp.com request_id=57a3a243-58f4-4efe-b478-eb660d84fe1f fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:48:38.616077+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=michaelchandrag-project.herokuapp.com request_id=0687b8a8-86ae-4c49-a37f-9cbf9e0f75de fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
您的应用启动但被终止,因为您没有将网络服务器绑定到指定端口。从日志消息中可以清楚地看出这一点:
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
在 Heroku 上,您必须将 HTTP 服务器绑定到 PORT
环境变量指定的端口。在 Heroku 网关的帮助下,您的应用程序将在默认 HTTP 和 HTTPS 端口上公开可用。
所以在预期的端口上启动您的服务器。而不是这个:
err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
这样做:
port := os.Getenv("PORT")
if port == "" {
port = "9000" // Default port if not specified
}
err := grace.Serve(":" + port, context.ClearHandler(http.DefaultServeMux))
我的 golang 应用程序在本地主机的 9000 端口上运行。在使用 godep 支持在 heroku 上部署它之后,我能够在 heroku 上推送和部署它。但是,当我尝试访问端点(例如“/”)时,它显示应用程序错误。在 heroku
部署时,您可以在下面看到我的代码和日志package main
import (
"log"
"fmt"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/gorilla/context"
"gopkg.in/paytm/grace.v1"
// utilhttp "bitbucket.org/michaelchandrag/chit/pkg/util/http"
// util "bitbucket.org/michalechandrag/chit/pkg/util"
)
func main() {
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", Articles)
http.Handle("/", muxRouter)
// err = grace.Serve(":"+cfg.Server.Port, context.ClearHandler(http.DefaultServeMux))
err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
if err != nil {
log.Println("[ERROR GRACEFUL]", err)
os.Exit(1)
}
os.Exit(0)
}
func Articles(w http.ResponseWriter, r *http.Request) {
// vars := mux.Vars(r)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Hello")
// r.Close = true
// w.Header().Set("Content-Type", "application/json")
// w.Header().Set("Access-Control-Allow-Origin", "*")
/*if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
if err := fn(w, r); err != nil {
log.Println(err)
apiObject := ConstructAPIError(http.StatusInternalServerError, ErrGeneral, SysMsgErrGeneral, MsgErrGeneral)
SendAPIObject(w, apiObject)
return
}*/
}
heroku 在部署时记录日志
-----> Go app detected
-----> Fetching jq... done
-----> Fetching stdlib.sh.v8... done
-----> Checking Godeps/Godeps.json file.
-----> New Go Version, clearing old cache
-----> Installing go1.12.6
-----> Fetching go1.12.6.linux-amd64.tar.gz... done
-----> Running: go install -v -tags heroku ./...
bitbucket.org/michaelchandrag/chit/pkg
bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/context
bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/mux
bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/tylerb/graceful.v1
bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/paytm/grace.v1
bitbucket.org/michaelchandrag/chit/pkg/util
bitbucket.org/michaelchandrag/chit
Installed the following binaries:
./bin/chit
-----> Discovering process types
Procfile declares types -> web
-----> Compressing...
Done: 7.5M
-----> Launching...
Released v3
https://michaelchandrag-project.herokuapp.com/ deployed to Heroku
访问端点前后的应用日志
2:47.954106+00:00 heroku[web.1]: Starting process with command `chit`
2019-07-08T05:02:49.413453+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T05:02:49.413476+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T05:02:49.413647+00:00 app[web.1]: 2019/07/08 05:02:49 starting serve on :9000
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
2019-07-08T05:03:48.131595+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-07-08T05:03:48.214979+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-08T05:03:48.193205+00:00 heroku[web.1]: Process exited with status 137
2019-07-08T10:38:59.721224+00:00 heroku[web.1]: State changed from crashed to starting
2019-07-08T10:39:00.359017+00:00 heroku[web.1]: Starting process with command `chit`
2019-07-08T10:39:02.232435+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T10:39:02.232458+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
2019-07-08T10:39:02.232583+00:00 app[web.1]: 2019/07/08 10:39:02 starting serve on :9000
2019-07-08T10:40:00.462841+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-07-08T10:40:00.462974+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-07-08T10:40:00.555959+00:00 heroku[web.1]: Process exited with status 137
2019-07-08T10:40:00.573427+00:00 heroku[web.1]: State changed from starting to crashed
2019-07-08T13:33:25.021835+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=michaelchandrag-project.herokuapp.com request_id=e6b5c93a-7177-4080-9858-ed925a20c513 fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:33:25.566036+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=michaelchandrag-project.herokuapp.com request_id=35c741a9-be4d-48f6-ba99-ba24ddc58b9b fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:48:38.294082+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=michaelchandrag-project.herokuapp.com request_id=57a3a243-58f4-4efe-b478-eb660d84fe1f fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
2019-07-08T13:48:38.616077+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=michaelchandrag-project.herokuapp.com request_id=0687b8a8-86ae-4c49-a37f-9cbf9e0f75de fwd="36.74.35.162" dyno= connect= service= status=503 bytes= protocol=https
您的应用启动但被终止,因为您没有将网络服务器绑定到指定端口。从日志消息中可以清楚地看出这一点:
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
在 Heroku 上,您必须将 HTTP 服务器绑定到 PORT
环境变量指定的端口。在 Heroku 网关的帮助下,您的应用程序将在默认 HTTP 和 HTTPS 端口上公开可用。
所以在预期的端口上启动您的服务器。而不是这个:
err := grace.Serve(":9000", context.ClearHandler(http.DefaultServeMux))
这样做:
port := os.Getenv("PORT")
if port == "" {
port = "9000" // Default port if not specified
}
err := grace.Serve(":" + port, context.ClearHandler(http.DefaultServeMux))