Created
June 9, 2018 08:13
-
-
Save mogeta/6cd955c6a0479a80b1558399dd7d301d to your computer and use it in GitHub Desktop.
lambdaからfirebase
This file contains 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" | |
"firebase.google.com/go" | |
"google.golang.org/api/option" | |
"log" | |
"time" | |
"github.com/aws/aws-lambda-go/lambda" | |
) | |
type iratto struct { | |
Type string | |
Created time.Time | |
} | |
func main() { | |
lambda.Start(hello) | |
} | |
func hello(event IotButtonEvent) { | |
ctx := context.Background() | |
opt := option.WithCredentialsFile("./firebase_secret_key.json") | |
app, err := firebase.NewApp(ctx, nil, opt) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
client, err := app.Firestore(ctx) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
defer client.Close() | |
_, _, err = client.Collection("users").Add(ctx, iratto{ | |
Type: event.DeviceEvent.ButtonClicked.ClickType, | |
Created: time.Now(), | |
}) | |
if err != nil { | |
log.Fatalf("Failed adding alovelace: %v", err) | |
} | |
} |
This file contains 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 "time" | |
type IotButtonEvent struct { | |
DeviceInfo struct { | |
DeviceID string `json:"deviceId"` | |
Type string `json:"type"` | |
RemainingLife float64 `json:"remainingLife"` | |
Attributes struct { | |
ProjectRegion string `json:"projectRegion"` | |
ProjectName string `json:"projectName"` | |
PlacementName string `json:"placementName"` | |
DeviceTemplateName string `json:"deviceTemplateName"` | |
} `json:"attributes"` | |
} `json:"deviceInfo"` | |
DeviceEvent struct { | |
ButtonClicked struct { | |
ClickType string `json:"clickType"` | |
ReportedTime time.Time `json:"reportedTime"` | |
} `json:"buttonClicked"` | |
} `json:"deviceEvent"` | |
PlacementInfo struct { | |
ProjectName string `json:"projectName"` | |
PlacementName string `json:"placementName"` | |
Attributes struct { | |
Key string `json:"key"` | |
} `json:"attributes"` | |
Devices struct { | |
SampleRequest string `json:"Sample-Request"` | |
} `json:"devices"` | |
} `json:"placementInfo"` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment