// Command rest-api is an API Gateway v1 example that returns "ok" for GET
// /health and a JSON echo for POST /echo. The point of the example is to
// show the shape of the lambdalog middleware stack, not to be a production
// router.
//
// See mercemay.top/src/lambdalog/examples/rest-api/.
package main
import (
"context"
"encoding/json"
awslambda "github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
v1 "mercemay.top/src/lambdalog/adapters/apigateway/v1"
)
type logger struct{}
func (logger) Info(string, ...v1.Field) {}
func (logger) Error(string, ...v1.Field) {}
func (logger) With(...v1.Field) v1.Logger { return logger{} }
func route(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
switch {
case req.HTTPMethod == "GET" && req.Path == "/health":
return events.APIGatewayProxyResponse{StatusCode: 200, Body: "ok"}, nil
case req.HTTPMethod == "POST" && req.Path == "/echo":
return echo(req)
default:
return v1.ErrorResponse(404, "not found"), nil
}
}
func echo(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var body map[string]any
if req.Body != "" {
if err := json.Unmarshal([]byte(req.Body), &body); err != nil {
return v1.ErrorResponse(400, "invalid json"), nil
}
}
out, err := json.Marshal(body)
if err != nil {
return v1.ErrorResponse(500, "encode failed"), nil
}
return events.APIGatewayProxyResponse{
StatusCode: 200,
Headers: map[string]string{"Content-Type": "application/json"},
Body: string(out),
}, nil
}
func main() {
wrapped := v1.Wrap(logger{}, route)
awslambda.Start(wrapped)
}