gin-gonic 无法分配请求的地址
gin-gonic cannot assign requested address
所以我目前正在使用 gin-gonic 包构建一个 restful api。我希望将代码部署到 google 云平台计算引擎 VM。当我 运行 本地计算机上的代码使用本地主机时,它可以使用本地主机,但是当 运行 在具有指定外部 IP 的实际 VM 实例上使用它时,我收到 TCP 连接的绑定错误。任何帮助表示赞赏。
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")
}
控制台:
[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
您需要使用 0.0.0.0
而不是您当前在 .Run()
语句中使用的内容。通过使用 0.0.0.0
,可以从可用的网络接口访问服务器。
r.Run("0.0.0.0:8080")
因此从外部 IP 访问 35.237.89.107:8080
将指向您的应用。
你只能监听本地主机,然后通过主机的ip访问,如35.237.89.107:8080
。
使用
r.Run(":8080")
0.0.0.0
没有必要。
所以我目前正在使用 gin-gonic 包构建一个 restful api。我希望将代码部署到 google 云平台计算引擎 VM。当我 运行 本地计算机上的代码使用本地主机时,它可以使用本地主机,但是当 运行 在具有指定外部 IP 的实际 VM 实例上使用它时,我收到 TCP 连接的绑定错误。任何帮助表示赞赏。
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")
}
控制台:
[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
您需要使用 0.0.0.0
而不是您当前在 .Run()
语句中使用的内容。通过使用 0.0.0.0
,可以从可用的网络接口访问服务器。
r.Run("0.0.0.0:8080")
因此从外部 IP 访问 35.237.89.107:8080
将指向您的应用。
你只能监听本地主机,然后通过主机的ip访问,如35.237.89.107:8080
。
使用
r.Run(":8080")
0.0.0.0
没有必要。