{"id":556,"date":"2019-07-05T19:05:00","date_gmt":"2019-07-05T10:05:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2019\/07\/05\/api-gateway-aws-sam-golang-websocket"},"modified":"2019-07-05T19:05:00","modified_gmt":"2019-07-05T10:05:00","slug":"api-gateway-aws-sam-golang-websocket","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2019\/07\/05\/api-gateway-aws-sam-golang-websocket\/","title":{"rendered":"API Gateway+AWS SAM+Golang\u3067WebSocket\u901a\u4fe1\u3092\u884c\u3046"},"content":{"rendered":"
AWS\u8a8d\u5b9a SAP\u306e\u52c9\u5f37\u306b\u65e5\u3005\u7cbe\u9032\u3057\u3066\u304a\u308a\u307e\u3059\u3002\u3084\u3063\u3055\u3093\u3067\u3054\u3056\u3044\u307e\u3059\u3002
\n\u4eca\u56de\u306f\u3001AWS SAM\u3068Go\u8a00\u8a9e\u3092\u7528\u3044\u3066\u3001
\nWebSocket\u901a\u4fe1\u306b\u3088\u308b\u30c1\u30e3\u30c3\u30c8\u6a5f\u80fd\u3092\u5b9f\u8df5\u3057\u3066\u307f\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n
Amazon API Gateway\u3067WebSocket\u304c\u5229\u7528\u53ef\u80fd\u306b\u306a\u3063\u305f\u306e\u306f\u3001 \u3053\u308c\u307e\u3067\u306f\u53cc\u65b9\u5411\u306e\u632f\u308b\u821e\u3044\u3092\u884c\u3046\u305f\u3081\u306e\u6c38\u7d9a\u7684\u306a\u7ba1\u7406\u3092\u62c5\u5f53\u3059\u308b \u4ee5\u4e0b\u306e\u6982\u8981\u56f3\u304b\u3089\u7c21\u5358\u306b\u51e6\u7406\u5185\u5bb9\u3092\u8aac\u660e\u3044\u305f\u3057\u307e\u3059\u3002 \u8a73\u7d30\u306a\u30b3\u30fc\u30c9\u5185\u5bb9\u306fGitHub\u3082\u3054\u53c2\u7167\u4e0b\u3055\u3044\u3002 \u305d\u308c\u3067\u306f\u3001\u5b9f\u8df5\u3057\u3066\u3044\u304d\u307e\u3057\u3087\u3046\uff01<\/p>\n onConnect\u306eLambda\u30d5\u30a1\u30f3\u30af\u30b7\u30e7\u30f3\u306f\u3001 DynamoDB\u3078\u306eCRUD\u3092\u3088\u308a\u4fbf\u5229\u306b\u3059\u308b\u30e9\u30a4\u30d6\u30e9\u30eaguregu\/dynamo<\/a>\u3092\u5229\u7528\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n sendMessage\u306f\u3001\u5168\u3066\u306e\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092\u53d6\u5f97\u3057\u3001 onDisconnect\u306f\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304c\u5207\u65ad\u3057\u305f\u5834\u5408\u306b
\n\u53bb\u5e74\u306e2018\u5e7412\u6708\u306b\u306a\u308a\u307e\u3059\u306e\u3067\u3001\u307e\u3060\u307e\u3060\u8a18\u61b6\u306b\u65b0\u3057\u3044\u51fa\u6765\u4e8b\u3067\u3059\u3002
\n[\u767a\u8868]Amazon API Gateway\u3067Websocket\u304c\u5229\u7528\u53ef\u80fd<\/a><\/p>\n
\n\u30db\u30b9\u30c8\u30b5\u30fc\u30d0\u30fc\u304c\u5fc5\u8981\u3067\u3057\u305f\u304c\u3001\u30b5\u30fc\u30d0\u30fc\u30ec\u30b9\u3067\u5b9f\u73fe\u3059\u308b\u3053\u3068\u3067\u4e0d\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\nAPI Gateway\u306b\u3088\u308bWebSocket\u901a\u4fe1\u306e\u6982\u8981\u56f3<\/h3>\n
\n<\/p>\n\n
onConnect<\/code> \u306eLabmda\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u6642\u3001\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092DynamoDB\u306b\u6c38\u7d9a\u5316\u3057\u307e\u3059\u3002<\/li>\n
sendMessage<\/code> \u306eLambda\u306f\u5168\u3066\u306e\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092\u5b9b\u5148\u3068\u3057\u3066\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u9001\u308a\u307e\u3059\u3002<\/li>\n
onDisconect<\/code> \u306eLambda\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u6642\u3001\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092DynamoDB\u304b\u3089\u524a\u9664\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n
\nhttps:\/\/github.com\/x-blood\/xblood-go-sam-websocket<\/a><\/p>\nonConnect\u306e\u51e6\u7406\u5185\u5bb9<\/h3>\n
\n\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092DynamoDB\u306b\u6c38\u7d9a\u5316\u3057\u307e\u3059\u3002<\/p>\nfunc HandleRequest(request events.APIGatewayWebsocketProxyRequest) (events.APIGatewayProxyResponse, error) {\n fmt.Println("start on_connect")\n connectionID := request.RequestContext.ConnectionID\n fmt.Printf("connectionId : %s \u00a5n", connectionID)\n\n \/\/ \u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092Put\u3059\u308b\n err := dynamodb.Put(connectionID)\n if err != nil {\n fmt.Println(err)\n return response.Create500response()\n }\n\n fmt.Println("end on_connect")\n return response.Create200response()\n}<\/code><\/pre>\n
sendMessage\u306e\u51e6\u7406\u5185\u5bb9<\/h3>\n
\n\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u9001\u4fe1\u3055\u308c\u305f\u30e1\u30c3\u30bb\u30fc\u30b8<\/code> \u3092
\n\u63a5\u7d9a\u4e2d\u306e\u5168\u3066\u306e\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306b\u9001\u4fe1\u3057\u307e\u3059\u3002<\/p>\nfunc HandleRequest(request events.APIGatewayWebsocketProxyRequest) (events.APIGatewayProxyResponse, error) {\n fmt.Println("start send_message")\n\n \/\/ \u5168\u3066\u306e\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092\u53d6\u5f97\n connections, err := dynamodb.GetAll()\n if err != nil {\n response.Create500response()\n }\n\n var config *aws.Config\n newSession, err := session.NewSession(config)\n if err != nil {\n response.Create500response()\n }\n\n \/\/ \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u9001\u4fe1\u3055\u308c\u305f\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u53d6\u5f97\u3059\u308b\n var postData postData\n err = json.Unmarshal([]byte(request.Body), &postData)\n if err != nil {\n response.Create500response()\n }\n\n svc := apigatewaymanagementapi.New(newSession)\n svc.Endpoint = fmt.Sprintf("https:\/\/%s\/%s", request.RequestContext.DomainName, request.RequestContext.Stage)\n\n for _, connection := range connections {\n connectionID := connection.ConnectionID\n\n \/\/ \u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092\u5b9b\u5148\u3068\u3057\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u9001\u4fe1\u3059\u308b\n svc.PostToConnection(&apigatewaymanagementapi.PostToConnectionInput{\n ConnectionId: &connectionID,\n Data: []byte(postData.Data),\n })\n }\n\n fmt.Println("end send_message")\n return response.Create200response()\n}<\/code><\/pre>\n
onDisconnect\u306e\u51e6\u7406\u5185\u5bb9<\/h3>\n
\n\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092DynamoDB\u304b\u3089\u524a\u9664\u3057\u307e\u3059\u3002<\/p>\nfunc HandleRequest(request events.APIGatewayWebsocketProxyRequest) (events.APIGatewayProxyResponse, error) {\n fmt.Println("start on_disconnect")\n connectionID := request.RequestContext.ConnectionID\n fmt.Printf("connectionId : %s \u00a5n", connectionID)\n\n \/\/ \u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092Delete\u3059\u308b\n err := dynamodb.Delete(connectionID)\n if err != nil {\n fmt.Println(err)\n return response.Create500response()\n }\n fmt.Println("end on_disconnect")\n return response.Create200response()\n}<\/code><\/pre>\n
AWS SAM template.yml\u306e\u5185\u5bb9<\/h3>\n