feat(firebase)!: promote to v1.0.0 — cache auth.Client, adopt xerrors, bump deps to v1
Cache auth.Client in OnInit (eliminates per-probe app.Auth call in HealthCheck). Replace all fmt.Errorf with xerrors structured errors (ErrInvalidInput / ErrInternal). Bump health/launcher/logz to v1.0.1, add xerrors v1.0.1, Go directive to 1.26. API committed as stable.
This commit is contained in:
28
firebase.go
28
firebase.go
@@ -2,7 +2,6 @@ package firebase
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
fb "firebase.google.com/go/v4"
|
||||
"firebase.google.com/go/v4/auth"
|
||||
@@ -10,6 +9,7 @@ import (
|
||||
"code.nochebuena.dev/go/health"
|
||||
"code.nochebuena.dev/go/launcher"
|
||||
"code.nochebuena.dev/go/logz"
|
||||
"code.nochebuena.dev/go/xerrors"
|
||||
)
|
||||
|
||||
// Provider is the minimal interface for consumers that only need the firebase App.
|
||||
@@ -31,9 +31,10 @@ type Config struct {
|
||||
}
|
||||
|
||||
type firebaseComponent struct {
|
||||
cfg Config
|
||||
logger logz.Logger
|
||||
app *fb.App
|
||||
cfg Config
|
||||
logger logz.Logger
|
||||
app *fb.App
|
||||
authClient *auth.Client
|
||||
}
|
||||
|
||||
// New returns a firebase Component. Call lc.Append(fb) to manage its lifecycle.
|
||||
@@ -43,7 +44,7 @@ func New(logger logz.Logger, cfg Config) Component {
|
||||
|
||||
func (f *firebaseComponent) OnInit() error {
|
||||
if f.cfg.ProjectID == "" {
|
||||
return fmt.Errorf("firebase: ProjectID is required")
|
||||
return xerrors.New(xerrors.ErrInvalidInput, "firebase: ProjectID is required")
|
||||
}
|
||||
f.logger.Info("firebase: initializing app", "project_id", f.cfg.ProjectID)
|
||||
app, err := fb.NewApp(context.Background(), &fb.Config{
|
||||
@@ -51,9 +52,14 @@ func (f *firebaseComponent) OnInit() error {
|
||||
})
|
||||
if err != nil {
|
||||
f.logger.Error("firebase: failed to create app", err)
|
||||
return fmt.Errorf("firebase: create app: %w", err)
|
||||
return xerrors.Wrap(xerrors.ErrInternal, "firebase: create app", err)
|
||||
}
|
||||
f.app = app
|
||||
authClient, err := app.Auth(context.Background())
|
||||
if err != nil {
|
||||
return xerrors.Wrap(xerrors.ErrInternal, "firebase: get auth client", err)
|
||||
}
|
||||
f.authClient = authClient
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -70,14 +76,10 @@ func (f *firebaseComponent) OnStop() error {
|
||||
func (f *firebaseComponent) App() *fb.App { return f.app }
|
||||
|
||||
func (f *firebaseComponent) HealthCheck(ctx context.Context) error {
|
||||
if f.app == nil {
|
||||
return fmt.Errorf("firebase: app not initialized")
|
||||
if f.authClient == nil {
|
||||
return xerrors.New(xerrors.ErrInternal, "firebase: app not initialized")
|
||||
}
|
||||
authClient, err := f.app.Auth(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("firebase: get auth client: %w", err)
|
||||
}
|
||||
_, err = authClient.GetUser(ctx, "health-probe-non-existent")
|
||||
_, err := f.authClient.GetUser(ctx, "health-probe-non-existent")
|
||||
if err != nil {
|
||||
if auth.IsUserNotFound(err) {
|
||||
return nil // expected: probe of non-existent UID succeeded
|
||||
|
||||
Reference in New Issue
Block a user