dongre8505 2015-06-27 11:13
浏览 67
已采纳

在Heroku上部署Go应用后面临的问题

I have deployed the Go App on Heroku but on hitting the url.. its not giving proper response. The Heroku logs looks something like this :

2015-06-27T10:43:06.839094+00:00 heroku[web.1]: Starting process with command `FlickrImage`
2015-06-27T10:43:08.998400+00:00 heroku[web.1]: State changed from starting to crashed
2015-06-27T10:43:08.998400+00:00 heroku[web.1]: State changed from crashed to starting
2015-06-27T10:43:08.985737+00:00 heroku[web.1]: Process exited with status 0
2015-06-27T10:43:10.795684+00:00 heroku[web.1]: Starting process with command `FlickrImage`
2015-06-27T10:43:13.837301+00:00 heroku[web.1]: Process exited with status 0
2015-06-27T10:43:13.850141+00:00 heroku[web.1]: State changed from starting to crashed
2015-06-27T10:44:41.914412+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/Index" host=morning-ridge-1365.herokuapp.com request_id=89d2e794-a725-4ddf-b437-dbcbd988428c fwd="202.12.83.44" dyno= connect= service= status=503 bytes=

FlickrImage is my go file and I have created a Procfile which contains "web: FlickrImage"

EDIT : FlickrImageServer Code :

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "reflect"
    "strconv"
    "strings"

    "github.com/gorilla/mux"
    "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

type imageLinks struct {
    Link     string `bson:"link"`
    Upvote   int    `bson:"upvote"`
    Downvote int    `bson:"downvote"`
}

type Result struct {
    Photos struct {
        Page    int    `json: "page"`
        Pages   int    `json: "pages"`
        PerPage int    `json: "perpage"`
        Total   string `json: "total"`
        Photo   []struct {
            Id       string `json: "id"`
            Owner    string `json: "owner"`
            Secret   string `json: "secret"`
            Server   string `json: "server"`
            Farm     int    `json: "farm"`
            Title    string `json: "title"`
            IsPublic int    `json: "ispublic"`
            IsFriend int    `json: "isfriend"`
            IsFamily int    `json: "isfamily`
        } `json: "photo"`
    } `json: "photos"`
    Stat string `json: "stat"`
}

func main() {

    router := mux.NewRouter().StrictSlash(true)
    router.HandleFunc("/Index", Index)
    router.HandleFunc("/UpVote", UpVoteRoute)
    router.HandleFunc("/DownVote", DownVoteRoute)
    log.Fatal(http.ListenAndServe(":8080", router))
}

func UpVoteRoute(w http.ResponseWriter, r *http.Request) {

    link := r.URL.Query().Get("imagelink")
    w.Header().Set("Content-Type", "application/json")
    session, err := mgo.Dial("mongodb://<user>:<password>@ds061631.mongolab.com:61631/flickrimagedb")
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    }
    defer session.Close()

    session.SetMode(mgo.Monotonic, true)
    c := session.DB("flickrimagedb").C("image_links_votes")

    err = c.Update(bson.M{"link": link}, bson.M{"$inc": bson.M{"upvote": 1}})
    if err != nil {
        fmt.Printf("Can't update document %v
", err)
        os.Exit(1)
    }
}

func DownVoteRoute(w http.ResponseWriter, r *http.Request) {

    link := r.URL.Query().Get("imagelink")
    w.Header().Set("Content-Type", "application/json")
    session, err := mgo.Dial("mongodb://*********:*******@ds061631.mongolab.com:61631/flickrimagedb")
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    }
    defer session.Close()

    session.SetMode(mgo.Monotonic, true)
    c := session.DB("flickrimagedb").C("image_links_votes")

    err = c.Update(bson.M{"link": link}, bson.M{"$inc": bson.M{"downvote": 1}})
    if err != nil {
        fmt.Printf("Can't update document %v
", err)
        os.Exit(1)
    }
}

func Index(w http.ResponseWriter, r *http.Request) {

    w.Header().Set("Content-Type", "application/json")
    session, err := mgo.Dial("mongodb://*********:*********@ds061631.mongolab.com:61631/flickrimagedb")
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    }

    defer session.Close()

    session.SetMode(mgo.Monotonic, true)
    c := session.DB("flickrimagedb").C("image_links_votes")

    checkResult := &imageLinks{}
    // Create a slice to begin with
    myType := reflect.TypeOf(checkResult)
    slice := reflect.MakeSlice(reflect.SliceOf(myType), 10, 10)
    // Create a pointer to a slice value and set it to the slice
    x := reflect.New(slice.Type())
    x.Elem().Set(slice)
    err = c.Find(bson.M{}).All(x.Interface())
    if err != nil {
        response, err := json.Marshal(x.Interface())
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(2)
        }
        fmt.Fprintf(w, string(response))
    } else {
        url := "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=*****************************&text=cute+puppies&per_page=12&format=json&nojsoncallback=1"
        res, err := http.Get(url)
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(3)
        }
        body, err := ioutil.ReadAll(res.Body)
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(4)
        }

        jsonData := &Result{}
        err = json.Unmarshal(body, jsonData)

        for value := range jsonData.Photos.Photo {
            s1 := []string{"https://farm", ".staticflickr.com/"}
            s2 := []string{strings.Join(s1, strconv.Itoa(jsonData.Photos.Photo[value].Farm)), "/"}
            s3 := []string{strings.Join(s2, jsonData.Photos.Photo[value].Server), "_"}
            s4 := []string{strings.Join(s3, jsonData.Photos.Photo[value].Id), ".jpg"}
            s := strings.Join(s4, jsonData.Photos.Photo[value].Secret)
            singleReuslt := imageLinks{}
            err = c.Find(bson.M{"link": s}).One(&singleReuslt)
            if err != nil {
                err = c.Insert(&imageLinks{Link: s, Upvote: 0, Downvote: 0})
                if err != nil {
                    fmt.Printf("%s", err)
                    os.Exit(5)
                }
            }
        }

        allResult := &imageLinks{}
        // Create a slice to begin with
        myType := reflect.TypeOf(allResult)
        slice := reflect.MakeSlice(reflect.SliceOf(myType), 10, 10)
        // Create a pointer to a slice value and set it to the slice
        x := reflect.New(slice.Type())
        x.Elem().Set(slice)
        err = c.Find(bson.M{}).All(x.Interface())
        response, err := json.Marshal(x.Interface())
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(6)
        }
        fmt.Fprintf(w, string(response))
    }
}

Any help would be appreciated

  • 写回答

1条回答 默认 最新

  • douyi7283 2015-06-27 23:34
    关注

    Heroku dynos dynamically assign a port. It's likely your application is crashing because it can't bind to port 8080.

    Change this line:

    log.Fatal(http.ListenAndServe(":8080", router))
    

    ... to this:

        port := ":" + os.Getenv("PORT")
        log.Fatal(http.ListenAndServe(port, router))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?