Deprecated in favor of firebase/firebase-admin-go.
A Firebase client written in Go
go get -u gopkg.in/zabawaba99/firego.v1
Import firego
import "gopkg.in/zabawaba99/firego.v1"
Create a new firego reference
f := firego.New("https://my-firebase-app.firebaseIO.com", nil)
with existing http client
f := firego.New("https://my-firebase-app.firebaseIO.com", client)
By default, the Firebase
reference will timeout after 30 seconds of trying
to reach a Firebase server. You can configure this value by setting the global
timeout duration
firego.TimeoutDuration = time.Minute
You can authenticate with your service_account.json
file by using the
golang.org/x/oauth2
package (thanks @m00sey for the snippet)
d, err := ioutil.ReadFile("our_service_account.json")
if err != nil {
return nil, err
}
conf, err := google.JWTConfigFromJSON(d, "https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/firebase.database")
if err != nil {
return nil, err
}
fb := firego.New("https://you.firebaseio.com", conf.Client(oauth2.NoContext))
// use the authenticated fb instance
f.Auth("some-token-that-was-created-for-me")
f.Unauth()
Visit Fireauth if you'd like to generate your own auth tokens
var v map[string]interface{}
if err := f.Value(&v); err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", v)
Take a look at Firebase's query parameters for more information on what each function does.
var v map[string]interface{}
if err := f.StartAt("a").EndAt("c").LimitToFirst(8).OrderBy("field").Value(&v); err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", v)
v := map[string]string{"foo":"bar"}
if err := f.Set(v); err != nil {
log.Fatal(err)
}
v := "bar"
pushedFirego, err := f.Push(v)
if err != nil {
log.Fatal(err)
}
var bar string
if err := pushedFirego.Value(&bar); err != nil {
log.Fatal(err)
}
// prints "https://my-firebase-app.firebaseIO.com/-JgvLHXszP4xS0AUN-nI: bar"
fmt.Printf("%s: %s\n", pushedFirego, bar)
v := map[string]string{"foo":"bar"}
if err := f.Update(v); err != nil {
log.Fatal(err)
}
if err := f.Remove(); err != nil {
log.Fatal(err)
}
notifications := make(chan firego.Event)
if err := f.Watch(notifications); err != nil {
log.Fatal(err)
}
defer f.StopWatching()
for event := range notifications {
fmt.Printf("Event %#v\n", event)
}
fmt.Printf("Notifications have stopped")
You can use a reference to save or read data from a specified reference
userID := "bar"
usersRef,err := f.Ref("users/"+userID)
if err != nil {
log.Fatal(err)
}
v := map[string]string{"id":userID}
if err := usersRef.Set(v); err != nil {
log.Fatal(err)
}
Check the GoDocs or Firebase Documentation for more details
In order to run the tests you need to go get -t ./...
first to go-get the test dependencies.
Feel free to open an issue if you come across any bugs or if you'd like to request a new feature.
- Fork it
- Create your feature branch (
git checkout -b new-feature
) - Commit your changes (
git commit -am 'Some cool reflection'
) - Push to the branch (
git push origin new-feature
) - Create new Pull Request