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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?