Heroku Go Webapp崩溃

时间:2018-11-27 18:41:30

标签: 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.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)
    }
}

Procfile

web: app

2 个答案:

答案 0 :(得分:0)

我不确定您遇到了什么,但是这个问题可能会导致您的本地运行时和Heroku运行时有所不同。

https://help.heroku.com/PPBPA231/how-do-i-use-the-port-environment-variable-in-container-based-apps

答案 1 :(得分:0)

通过一次重新构建项目,我能够使它正常工作。因此,这可能是一个问题。

谢谢大家的帮助!

https://serene-lake-65180.herokuapp.com/