Created
October 19, 2020 12:01
-
-
Save antonydevanchi/771765e1012f42134f6b98fa4bcdf4d1 to your computer and use it in GitHub Desktop.
sls foo for trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "encoding/json" | |
| "fmt" | |
| ) | |
| // Входной JSON-документ будет автоматически преобразован в объект данного типа | |
| type Request struct { | |
| Message string `json:"message"` | |
| Number int `json:"number"` | |
| } | |
| type ResponseBody struct { | |
| Context context.Context `json:"context"` | |
| Request interface{} `json:"request"` | |
| } | |
| func Handler(ctx context.Context, request *Request) ([]byte, error) { | |
| // В логах функции будут напечатаны значения контекста вызова и тела запроса | |
| fmt.Println("context", ctx) | |
| fmt.Println("request", request) | |
| // Объект, содержащий тело ответа, преобразуется в массив байтов | |
| body, err := json.Marshal(&ResponseBody{ | |
| Context: ctx, | |
| Request: request, | |
| }) | |
| if err != nil { | |
| return nil, err | |
| } | |
| // Тело ответа необходимо вернуть в виде массива байтов | |
| return body, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment