-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (53 loc) · 1.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"context"
"net/http"
"cloud.google.com/go/firestore"
"github.com/gin-gonic/gin"
"google.golang.org/api/option"
)
const firestoreAccountFile = "firebase.json"
const firestoreProjectID = "davisdavid-golang-playground"
type formData struct {
Name string `json:"name" binding:"required"`
Email string `json:"email" binding:"required"`
}
func main() {
// Gin init
r := gin.Default()
// Serve from static build directory
r.StaticFS("/", http.Dir("./build"))
// routes
r.POST("/api/user", userHandler)
// run application on port 8080
r.Run(":8080")
}
func writeLogIfError(c *gin.Context, err error) {
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}
func getNewFirestoreClient(ctx context.Context) (*firestore.Client, error) {
return firestore.NewClient(ctx, firestoreProjectID, option.WithServiceAccountFile(firestoreAccountFile))
}
// User enters the form fields to create a new user, which is then populated in our Firestore db.
func userHandler(c *gin.Context) {
ctx := context.Background()
client, err := getNewFirestoreClient(ctx)
writeLogIfError(c, err)
defer client.Close()
// Get form data
var form formData
c.BindJSON(&form)
// [START add user to firestore]
_, _, err = client.Collection("users").Add(ctx, map[string]interface{}{
"name": form.Name,
"email": form.Email,
})
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// [END add user to firestore]
c.JSON(http.StatusOK, gin.H{"status": "user added to db"})
}