Malcolm
Malcolm

Reputation: 43

gin-gonic cannot assign requested address

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

Upvotes: 1

Views: 4094

Answers (2)

Oktfolio
Oktfolio

Reputation: 176

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.

Upvotes: 5

novalagung
novalagung

Reputation: 11532

You need to use 0.0.0.0 instead of what you are currently using on .Run() statement. By using 0.0.0.0, the server will be accessible from available network interface.

r.Run("0.0.0.0:8080")

So accessing 35.237.89.107:8080 from external IP will points towards your app.

Upvotes: 4

Related Questions