我有以下控制器,它使用 Go 中内置的包装器进行外部 API 调用。问题是,如果我在没有 docker 的情况下运行我的服务器,端点将返回有效数据。但是,当我从 docker 中运行它时,我得到的错误是unexpected end of JSON input.
回家去
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)
}
这是我的泊坞窗设置:
文件
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 错误(即如何解决这个问题)?
谢谢
炎炎设计
相关分类