我有一个GO程序,当一个URL被击中时,它会以html格式提供波信息.它在带有nginx反向代理的虚拟机上运行.

我正在try 将代码移到AWS Lambda,但我正在努力理解如何触发和返回html.

原始代码完成了它的逻辑,并在下面的片段中以模板HTML的形式呈现了数据.

func indexHandler(w http.ResponseWriter, r *http.Request) {
    GetWeather()
    GetSurf()
    CheckSurf(ForecastGroup)
    p := CodCallPage{Title: "Swell Forecast", RunTime: htmlRunDate, DailyHtmlData: DailyMatchingSwells}
    codcallTemplateInit.Execute(w, p)
}

func main() {
    http.HandleFunc("/", indexHandler)
    http.ListenAndServe(":8000", nil)
}

我相信我不再需要nginx代理,而需要调用lambda函数来运行我的代码.因此,我已将代码更改为以下代码.

func indexHandler(w http.ResponseWriter, r *http.Request) {
    GetWeather()
    GetSurf()
    CheckSurf(ForecastGroup)
    p := CodCallPage{Title: "Swell Forecast", RunTime: htmlRunDate, DailyHtmlData: DailyMatchingSwells}
    codcallTemplateInit.Execute(w, p)
}

func handler(ctx context.Context, request events.APIGatewayProxyRequest) error {
    log.Println("Via Lambda !!")
    http.HandleFunc("/", indexHandler)
}

func main() {
    lambda.Start(handler)
}

当我运行AWS测试时,Lamba函数使用默认的JSON文本.

{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}

它在超时时会给出错误:

{
  "errorMessage": "2023-06-08T08:43:13.714Z d6e2acc0-b1da-4e92-820b-63f8f5050947 Task timed out after 15.01 seconds"
}

我不确定是lambda函数忽略了测试,还是该函数没有以正确的方式返回HTML,或者lambda函数是否期望JSON而不是HTML?请问有什么建议可以帮助我理解,我应该看哪里?

推荐答案

不需要在lambda函数上运行http服务器.此代码应该可以工作

func indexHandler(ctx context.Context, req events.APIGatewayProxyRequest) (string, error)  {
        GetWeather()
        GetSurf()
        CheckSurf(ForecastGroup)
        p := CodCallPage{Title: "Swell Forecast", RunTime: htmlRunDate, DailyHtmlData: DailyMatchingSwells}
        var res bytes.Buffer
        if err := codcallTemplateInit.Execute(&res, p); err != nil {
          return "", err 
        }
        return res.String(), nil
}


func main() {
        lambda.Start(indexHandler)
}

使用AWS API网关代理时,您需要返回events.APIGatewayProxyResponse,因此indexHandler会有所不同.

func indexHandler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error)  {
        GetWeather()
        GetSurf()
        CheckSurf(ForecastGroup)
        p := CodCallPage{Title: "Swell Forecast", RunTime: htmlRunDate, DailyHtmlData: DailyMatchingSwells}
        var res bytes.Buffer
        if err := codcallTemplateInit.Execute(&res, p); err != nil {
          return events.APIGatewayProxyResponse{StatusCode: 400, Body: err.Error()}, err 
        }
        return events.APIGatewayProxyResponse{Body: res.String(), StatusCode: 200}, nil
}

Go相关问答推荐

无法在Macos上使用Azure Speech golang SDK

理解Golang并发:缓冲通道的意外行为

Golang文本/模板序列范围

Go Colly-访问for循环中的URL

如何给杜松子wine 的路由加上一个名字,比如Laravel ?

Golang SDK for DynamoDB:ReturnValuesOnConditionCheckFailure在条件chcek失败发生时不返回条件的详细信息

如何使用 Go 连接到非默认 firestore 数据库?

如何在 zap 记录器库中使用自定义时间函数

如何将 goose 迁移与 pgx 一起使用?

仅使用公共 api 对 alexedwards/scs 进行简单测试

使用Dockertest进行Golang SQL单元测试的基本设置

如何为ANTLR4目标Go调试监听器

使用 httptest 对 http 请求进行单元测试重试

Kubo,来自 IpfsNode.Bootstrap 的无效内存地址或零指针取消引用

io.Reader 无限循环与 fmt.Fscan

如何模仿联合类型

Golang计算 struct struct 中的字段数

如何将多个切片打印为一个切片?

使用go doc命令查看示例函数?

使用 delve 在容器中调试 Golang:container_linux.go:380:启动容器进程导致:exec:/dlv:stat /dlv:没有这样的文件或目录