mardi 27 novembre 2018

Heroku Go Webapp Crashing

I have a web app I've written in Go and I can get it to work perfectly on my localhost machine. The issue occurs after I upload it to Heroku. This is also my first Heroku app as well. I have no problem pushing it up to heroku but I get the following errors when I try to run it.

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

Here is my code that I have published to heroku. Thanks for any insights you could give!

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




Aucun commentaire:

Enregistrer un commentaire