web - Heroku Go Web 应用程序崩溃
问题描述
我有一个用 Go 编写的 Web 应用程序,我可以让它在我的本地主机机器上完美运行。将其上传到 Heroku 后会出现此问题。这也是我的第一个 Heroku 应用程序。我把它推到heroku没有问题,但是当我尝试运行它时出现以下错误。
2018-11-27T18:00:45.614798+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=boiling-eyrie-61189.herokuapp.com request_id=7ff8feb8-bc03-4aff-a0ce-42474fcf35e9 fwd="65.183.104.130" dyno= connect= service= status=503 bytes= protocol=https
2018-11-27T18:00:45.811042+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=boiling-eyrie-61189.herokuapp.com request_id=5fe9555a-e8df-4f09-be37-be8bd4e66af6 fwd="65.183.104.130" dyno= connect= service= status=503 bytes= protocol=https
这是我发布到heroku的代码。感谢您提供的任何见解!
应用程序.go
package main
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/julienschmidt/httprouter"
)
type Results struct {
Results []Result `json:"results"`
}
type Result struct {
Name string `json:"name"`
Rating float64 `json:"rating"`
Icon string `json:"icon"`
Address string `json:"vicinity"`
}
type Data struct {
Name string
Rating string
Icon string
Address string
}
type HP struct {
Title string
}
func Home(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
t, _ := template.ParseFiles("./public/templates/home.html")
p := HP{Title: "Eat The Shoals"}
t.Execute(w, p)
}
func Search(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
var url string
switch ps.ByName("name") {
//cases taken out due to privacy issues
}
resultClient := http.Client{
Timeout: time.Second * 2,
}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("User-Agent", "eattheshoals")
res, getErr := resultClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
byteValue, _ := ioutil.ReadAll(res.Body)
//if readErr != nil {
// log.Fatal(readErr)
//}
var results Results
jsonErr := json.Unmarshal(byteValue, &results)
if jsonErr != nil {
log.Fatal(jsonErr)
}
values := make([]Data, len(results.Results), len(results.Results))
for i := 0; i < len(results.Results); i++ {
values[i].Name = results.Results[i].Name
values[i].Rating = strconv.FormatFloat(results.Results[i].Rating, 'f', 1, 64)
values[i].Icon = results.Results[i].Icon
values[i].Address = results.Results[i].Address
//fmt.Println("Name: " + results.Results[i].Name)
//fmt.Println("Rating: " + strconv.FormatFloat(results.Results[i].Rating, 'f', 1, 64))
//fmt.Println("Icon: " + results.Results[i].Icon)
//fmt.Println("Address : " + results.Results[i].Address)
}
t, _ := template.ParseFiles("./public/templates/search.html")
t.Execute(w, values)
}
var homeTemplate *template.Template
func determineListenAddress() (string, error) {
port := os.Getenv("PORT")
if port == "" {
return "", fmt.Errorf("$PORT not set")
}
return ":" + port, nil
}
func main() {
//addr, err := determineListenAddress()
//if err != nil {
// log.Fatal(err)
//}
router := httprouter.New()
router.GET("/search/:name", Search)
router.GET("/", Home)
port := os.Getenv("PORT")
//log.Fatal(http.ListenAndServe(":8080", router))
//log.Printf("Listening on %s...\n", addr)
if err := http.ListenAndServe(":"+port, nil); err != nil {
panic(err)
}
}
档案
web: app
解决方案
我不确定您遇到了什么,但这个问题可能会导致您的本地运行时和 Heroku 运行时之间存在差异。
https://help.heroku.com/PPBPA231/how-do-i-use-the-port-environment-variable-in-container-based-apps
推荐阅读
- objective-c - 如何检查 OC 或 Swift 中未使用的变量
- xampp - #2002 - 连接被拒绝 - 服务器没有响应(或本地服务器的套接字配置不正确)
- angular - 如何在谷歌云功能中设置自定义声明?
- java - 如何开发一个可以使用 java 回答问题的消息机器人
- angular - 更改角度组件中的通知图标
- php - oci8、php7 和 Oracle 10.1 兼容性
- exception - 检查失败:带有音乐播放器插件的 CheckException(env)
- c++ - Crypto++ AES 代码和链接上的未定义符号
- python - 为什么我在 Django 中有这个错误?
- react-admin - React Admin:如何使用引导程序而不是 Material Design?