我有以下控制器,该控制器使用Go内置的包装器进行外部API调用。问题是,如果我在没有docker的情况下运行服务器,则端点将返回有效数据。但是,当我从docker中运行它时,我得到的错误是unexpected end of JSON input
。
home.go
package controllers
import (
"fmt"
"encoding/json"
"net/http"
"time"
"strconv"
cmc "github.com/coincircle/go-coinmarketcap"
)
type HomeController struct{}
func NewHomeController() *HomeController {
return &HomeController{}
}
func (hc HomeController) IndexEndpoint(w http.ResponseWriter, r *http.Request) {
threeMonths := int64(60 * 60 * 24 * 90)
now := time.Now()
secs := now.Unix()
start := secs - threeMonths
end := secs
fmt.Println("Time is " + strconv.FormatInt(end, 10))
graph, _ := cmc.TickerGraph(&cmc.TickerGraphOptions{
Start: start,
End: end,
Symbol: "ETH",
})
fmt.Println(graph)
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(graph)
}
这是我的docker设置:
Dockerfile
FROM golang:latest AS builder
COPY . $GOPATH/src/github.com/gohuygo/cryptodemo-api
WORKDIR $GOPATH/src/github.com/gohuygo/cryptodemo-api
RUN go get ./
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix nocgo -o /app .
FROM scratch
COPY --from=builder /app ./
ENTRYPOINT ["./app"]
为什么在涉及docker时为什么抱怨JSON不好(即我该如何解决)?
谢谢
答案 0 :(得分:3)
您的运行应用程序可能尝试建立传出HTTPS连接,但是scratch
容器不包含验证TLS证书所需的CA证书。
在这种情况下,请考虑使用centurylink/ca-certs
而不是scratch
。它包含CA证书,您的程序应自动使用它们。