ลองเชี่อมต่อ Firebase Firestore ด้วย Golang


Cloud firestore คืออะไร

คือฐานข้อมูลที่มีรูปแบบ แบบ NoSQL ที่เป็น Document Database แบบเดียวกับ MongoDB

อ่านเพิ่มเติมได้ที่ Cloud Firestore  |  Firebase

ตัวอย่าง โค้ด Golang

  • Package ที่ใช้
import (
	"context"
	"log"
	"net/http"

	"cloud.google.com/go/firestore"
	firebase "firebase.google.com/go"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"github.com/mitchellh/mapstructure"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)
  • ส่วนการ Init Firebase
type App struct {
	client *firestore.Client
	ctx    context.Context
}
func (route *App) Init() {
	route.ctx = context.Background()
	sa := option.WithCredentialsFile("service.json")
	app, err := firebase.NewApp(route.ctx, nil, sa)
	if err != nil {
		log.Fatalln(err)
	}

	route.client, err = app.Firestore(route.ctx)
	if err != nil {
		log.Fatalln(err)
	}

}
// main.go
  • ส่วนการเรียกใช้งาน Firestore
func (route *App) Home(c echo.Context) error {
	BooksData := []models.Income{}

	iter := route.client.Collection("incomes").Documents(route.ctx)
	for {
		BookData := models.Income{}
		doc, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return err
		}

		mapstructure.Decode(doc.Data(), &BookData)
		BooksData = append(BooksData, BookData)
	}
	return c.JSON(http.StatusOK, BooksData)
}
// main.go
  • ส่วน main
func main() {
	route := App{}
	e := echo.New()
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	e.GET("/", func(c echo.Context) error {
		return route.Home(c)
	})
	e.Logger.Fatal(e.Start(":9000"))
}
// main.go
  • ส่วนของ Model
package models

type Income struct {
	Date  string  "json date"
	List  string  "json list"
	Price float32 "json price"
}
// models/models.go
  • ทั้งหมด
package main

import (
	"context"
	"firebase/models"
	"log"
	"net/http"

	"cloud.google.com/go/firestore"
	firebase "firebase.google.com/go"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"github.com/mitchellh/mapstructure"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

type App struct {
	client *firestore.Client
	ctx    context.Context
}

func main() {
	route := App{}
	route.Init()
	e := echo.New()
	e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
		AllowOrigins: []string{"*"},
		AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
	}))

	e.GET("/", func(c echo.Context) error {
		return route.Home(c)
	})
	e.Logger.Fatal(e.Start(":9000"))
}

func (route *App) Init() {
	route.ctx = context.Background()
	sa := option.WithCredentialsFile("income.json")
	app, err := firebase.NewApp(route.ctx, nil, sa)
	if err != nil {
		log.Fatalln(err)
	}

	route.client, err = app.Firestore(route.ctx)
	if err != nil {
		log.Fatalln(err)
	}

}

func (route *App) Home(c echo.Context) error {
	IncomesData := []models.Income{}
	iter := route.client.Collection("incomes").Documents(route.ctx)
	for {
		IncomeData := models.Income{}
		doc, err := iter.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return err
		}

		mapstructure.Decode(doc.Data(), &IncomeData)
		IncomesData = append(IncomesData, IncomeData)
	}
	return c.JSON(http.StatusOK, IncomesData)
}
// main.go
$ go run .


GitHub - jaedsadadotme/go-firebase