douzhang2092 2018-10-29 22:14
浏览 209
已采纳

gin-gonic无法分配请求的地址

So I am currently working on building a restful api in go using the gin-gonic package. I hope to deploy the code to a google cloud platform compute engine VM. When I run the code on my local machine it works with using local host but when running it on the actual VM instance with the external IP specified I get a bind error with the TCP connection. Any help appreciated.

server.go

package main

import (
    "encoding/json"
    "io/ioutil"
    "net/http"
    "os"

    "github.com/gin-gonic/gin"
)

type headlines struct {
    Author      string
    Title       string
    Description string
    Url         string
    UrlToImage  string
    PublishedAt string
    Content     string
}

type NewsResponse struct {
    Status       string
    TotalResults int
    Articles     []headlines
}

func GetSourceHeadlines(source string) NewsResponse {
    newsAPIKey := os.Getenv("NEWS_API_KEY")
    var newsResponse NewsResponse
    resp, err := http.Get("https://newsapi.org/v2/top-headlines?sources=" + source + "&apiKey=" + newsAPIKey)

    if err != nil {
        panic(err)
    }

    defer resp.Body.Close()

    if resp.StatusCode == http.StatusOK {
        bodyBytes, _ := ioutil.ReadAll(resp.Body)
        err := json.Unmarshal(bodyBytes, &newsResponse)
        if err == nil {
            return newsResponse
        }
    }
    return newsResponse
}

func main() {
    r := gin.Default()
    r.GET("/headlines/ign", func(c *gin.Context) {
        c.JSON(http.StatusOK, GetSourceHeadlines("ign"))
    })

    r.GET("/headlines/polygon", func(c *gin.Context) {
        c.JSON(http.StatusOK, GetSourceHeadlines("polygon"))
    })

    r.GET("/headlines/techcrunch", func(c *gin.Context) {
        c.JSON(http.StatusOK, GetSourceHeadlines("techcrunch"))
    })

    r.GET("/headlines/hacker-news", func(c *gin.Context) {
        c.JSON(http.StatusOK, GetSourceHeadlines("hacker-news"))
    })
    r.Run("35.237.89.107:8080")
}

Console:

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /headlines/ign            --> main.main.func1 (3 handlers)
[GIN-debug] GET    /headlines/polygon        --> main.main.func2 (3 handlers)
[GIN-debug] GET    /headlines/techcrunch     --> main.main.func3 (3 handlers)
[GIN-debug] Listening and serving HTTP on 35.237.89.107:8080
[GIN-debug] [ERROR] listen tcp 35.237.89.107:8080: bind: cannot assign requested address
  • 写回答

2条回答 默认 最新

  • duanping2695 2018-10-30 07:44
    关注

    You can only listen localhost, then access via your host's ip, like 35.237.89.107:8080.

    use

    r.Run(":8080")
    

    0.0.0.0 is not necessary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错