所以我目前正在使用 gin-gonic 包在 go 中構建一個 restful api。我希望將代碼部署到谷歌云平臺計算引擎 VM。當我在我的本地機器上運行代碼時,它使用本地主機工作,但是當在指定外部 IP 的實際 VM 實例上運行它時,我收到 TCP 連接的綁定錯誤。任何幫助表示贊賞。服務器.gopackage mainimport ( "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-gonic 無法分配請求的地址
慕田峪7331174
2023-05-04 17:00:15