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

在Heroku上部署Go应用程序后面临问题,heroku,web,go,server,web-deployment,Heroku,Web,Go,Server,Web Deployment,我已经在Heroku上部署了Go应用程序,但在点击url时。。它没有给出适当的回应。Heroku日志如下所示: 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:4

我已经在Heroku上部署了Go应用程序,但在点击url时。。它没有给出适当的回应。Heroku日志如下所示:

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是我的go文件,我创建了一个包含“web:FlickrImage”的Procfile

编辑: FlickrImageServer代码:

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\n", 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\n", 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))
    }
}
主程序包
进口(
“编码/json”
“fmt”
“io/ioutil”
“日志”
“net/http”
“操作系统”
“反映”
“strconv”
“字符串”
“github.com/gorilla/mux”
“gopkg.in/mgo.v2”
“gopkg.in/mgo.v2/bson”
)
类型imageLinks结构{
链接字符串`bson:“链接”`
Upvote int`bson:“Upvote”`
向下投票int`bson:“向下投票”`
}
类型结果结构{
照片结构{
Page int`json:“Page”`
Pages int`json:“页面”`
PerPage int`json:“PerPage”`
Total字符串`json:“Total”`
照片[]结构{
Id字符串`json:“Id”`
所有者字符串`json:“所有者”`
Secret字符串`json:“Secret”`
服务器字符串`json:“服务器”`
Farm int`json:“Farm”`
标题字符串`json:“标题”`
IsPublic int`json:“IsPublic”`
IsFriend int`json:“IsFriend”`
IsFamily int`json:“IsFamily`
}`json:“照片”`
}`json:“照片”`
Stat字符串`json:“Stat”`
}
func main(){
路由器:=mux.NewRouter().StrictSlash(真)
router.HandleFunc(“/Index”,Index)
路由器.HandleFunc(“/UpVote”,UpVoteRoute)
路由器.HandleFunc(“/DownVote”,DownVoteRoute)
log.Fatal(http.ListenAndServe(“:8080”,路由器))
}
func UpVoteRoute(w http.ResponseWriter,r*http.Request){
link:=r.URL.Query().Get(“imagelink”)
w、 Header().Set(“内容类型”、“应用程序/json”)
session,err:=mgo.Dial(“mongodb://:@ds061631.mongolab.com:61631/flickrimagedb”)
如果错误!=零{
fmt.Printf(“%s”,错误)
操作系统退出(1)
}
延迟会话。关闭()
session.SetMode(mgo.Monotonic,true)
c:=session.DB(“flickrimagedb”).c(“图像链接投票”)
err=c.Update(bson.M{“link”:link},bson.M{“$inc”:bson.M{“upvote”:1})
如果错误!=零{
fmt.Printf(“无法更新文档%v\n”,错误)
操作系统退出(1)
}
}
func下行路由(带http.ResponseWriter,r*http.Request){
link:=r.URL.Query().Get(“imagelink”)
w、 Header().Set(“内容类型”、“应用程序/json”)
session,err:=mgo.Dial(“mongodb://************:***********@ds061631.mongolab.com:61631/flickrimagedb”)
如果错误!=零{
fmt.Printf(“%s”,错误)
操作系统退出(1)
}
延迟会话。关闭()
session.SetMode(mgo.Monotonic,true)
c:=session.DB(“flickrimagedb”).c(“图像链接投票”)
err=c.Update(bson.M{“link”:link},bson.M{“$inc”:bson.M{“downvote”:1})
如果错误!=零{
fmt.Printf(“无法更新文档%v\n”,错误)
操作系统退出(1)
}
}
func索引(w http.ResponseWriter,r*http.Request){
w、 Header().Set(“内容类型”、“应用程序/json”)
session,err:=mgo.Dial(“mongodb://************:**************@ds061631.mongolab.com:61631/flickrimagedb”)
如果错误!=零{
fmt.Printf(“%s”,错误)
操作系统退出(1)
}
延迟会话。关闭()
session.SetMode(mgo.Monotonic,true)
c:=session.DB(“flickrimagedb”).c(“图像链接投票”)
检查结果:=&imageLinks{}
//首先创建一个切片
myType:=reflect.TypeOf(检查结果)
slice:=reflect.MakeSlice(reflect.SliceOf(myType),10,10)
//创建指向切片值的指针并将其设置为切片
x:=reflect.New(slice.Type())
x、 Elem().Set(切片)
err=c.Find(bson.M{}).All(x.Interface())
如果错误!=零{
响应,err:=json.Marshal(x.Interface())
如果错误!=零{
fmt.Printf(“%s”,错误)
操作系统出口(2)
}
fmt.Fprintf(w,字符串(响应))
}否则{
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)
如果错误!=零{
fmt.Printf(“%s”,错误)
操作系统出口(3)
}
body,err:=ioutil.ReadAll(res.body)
如果错误!=零{
fmt.Printf(“%s”,错误)
操作系统出口(4)
}
jsonData:=&结果{}
err=json.Unmarshal(body,jsonData)
对于值:=范围jsonData.Photos.Photo{
s1:=[]字符串{”https://farm“,”.staticflickr.com/“}
s2:=[]字符串{strings.Join(s1,strconv.Itoa(jsonData.Photos.Photo[value].Farm)),“/”}
s3:=[]字符串{strings.Join(s2,jsonData.Photos.Photo[value].Server),“}
s4:=[]字符串{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})
如果错误!=零{
err=c.Insert(&imageLinks{Link:s,向上投票:0,向下投票:0})
如果错误!=零{
fmt.Printf(“%s”,错误)
操作系统出口(5)
}
}
}
allResult:=&imageLinks{}
//首先创建一个切片
myType:=reflect.TypeOf(allResult)
slice:=reflect.MakeSlice(reflect.SliceOf(myType),10,10)
//创建指向切片值的指针并将其设置为切片
x:=reflect.New(slice.Type())
x、 Elem().Set(切片)
err=c.Find(bson.M{}).All(x.Interface())
响应,err:=json.Marshal(x.Interface())
如果错误!=零{
fmt.Printf(“%s”,错误)
操作系统出口(6)
}
fmt.Fprintf(w,字符串(响应))
}
}
如果您有任何帮助,我们将不胜感激。您的应用程序可能会崩溃,因为它无法绑定到端口8080

更改此行:

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

    port := ":" + os.Getenv("PORT")
    log.Fatal(http.ListenAndServe(port, router))
你能发邮件给我吗