Web Heroku Go网络应用程序崩溃

Web Heroku Go网络应用程序崩溃,web,go,heroku,Web,Go,Heroku,我有一个用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.c

我有一个用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的代码。感谢您提供的任何见解

App.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)
    }
}

我不确定您遇到了什么,但这个问题可能会导致本地运行时和Heroku运行时之间的差异


通过一次一小步地重建我的项目,我能够实现这一点。所以这可能是一个godeps问题

谢谢大家的帮助


您有文件吗?这是Home func中的Heroku demo Go应用程序,您似乎忽略了ParseFiles调用以及Execute调用返回的错误。尝试记录这些。你将应用程序连接到哪个端口?Heroku给了我一条非常类似但有误导性的错误消息,因为端口权限错误导致我的登台服务器应用程序很快崩溃。运行
sudo setcap CAP\u NET\u BIND\u服务=+eip/path/to/binary
为我修复了它。我不确定这是怎么回事,但我的错误信息是类似的,这是我的解决方案。此外,这可能不是一个好的永久/生产解决方案。@HarrisonBrock我添加了procfile内容
web: app