pebble

package
v0.0.0-...-5d712d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package pebble contains gotd storage implementations using CockroachDB pebble.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Credentials

type Credentials struct {
	kv.Credentials
}

Credentials stores user credentials to Pebble.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	pebbledb "github.com/cockroachdb/pebble"
	"github.com/go-faster/errors"

	tgauth "github.com/gotd/td/telegram/auth"

	"github.com/gotd/td/telegram"

	"github.com/johnnyipcom/gotd-contrib/auth"
	"github.com/johnnyipcom/gotd-contrib/auth/terminal"
	"github.com/johnnyipcom/gotd-contrib/pebble"
)

func pebbleAuth(ctx context.Context) error {
	db, err := pebbledb.Open("pebble.db", &pebbledb.Options{})
	if err != nil {
		return errors.Errorf("create pebble storage: %w", err)
	}
	cred := pebble.NewCredentials(db).
		WithPhoneKey("phone").
		WithPasswordKey("password")

	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return errors.Errorf("create client: %w", err)
	}

	return client.Run(ctx, func(ctx context.Context) error {
		return client.Auth().IfNecessary(
			ctx,
			tgauth.NewFlow(auth.Build(cred, terminal.OS()), tgauth.SendCodeOptions{}),
		)
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := pebbleAuth(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	}
}
Output:

func NewCredentials

func NewCredentials(db *pebble.DB) Credentials

NewCredentials creates new Credentials.

type PeerStorage

type PeerStorage struct {
	// contains filtered or unexported fields
}

PeerStorage is a peer storage based on pebble.

func NewPeerStorage

func NewPeerStorage(db *pebble.DB) *PeerStorage

NewPeerStorage creates new peer storage using pebble.

func (PeerStorage) Add

func (s PeerStorage) Add(ctx context.Context, value storage.Peer) (rerr error)

Add adds given peer to the storage.

func (PeerStorage) Assign

func (s PeerStorage) Assign(ctx context.Context, key string, value storage.Peer) (rerr error)

Assign adds given peer to the storage and associate it to the given key.

func (PeerStorage) Find

func (s PeerStorage) Find(ctx context.Context, key storage.PeerKey) (_ storage.Peer, rerr error)

Find finds peer using given key.

func (PeerStorage) Iterate

Iterate creates and returns new PeerIterator.

func (PeerStorage) Resolve

func (s PeerStorage) Resolve(ctx context.Context, key string) (_ storage.Peer, rerr error)

Resolve finds peer using associated key.

func (*PeerStorage) WithWriteOptions

func (s *PeerStorage) WithWriteOptions(writeOpts *pebble.WriteOptions) *PeerStorage

WithWriteOptions sets pebble's write options for write operations.

type SessionStorage

type SessionStorage struct {
	kv.Session
}

SessionStorage is a MTProto session Pebble storage.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	pebbledb "github.com/cockroachdb/pebble"
	"github.com/go-faster/errors"

	"github.com/gotd/td/telegram"

	"github.com/johnnyipcom/gotd-contrib/pebble"
)

func pebbleStorage(ctx context.Context) error {
	db, err := pebbledb.Open("pebble.db", &pebbledb.Options{})
	if err != nil {
		return errors.Errorf("create pebble storage: %w", err)
	}
	storage := pebble.NewSessionStorage(db, "session")

	client, err := telegram.ClientFromEnvironment(telegram.Options{
		SessionStorage: storage,
	})
	if err != nil {
		return errors.Errorf("create client: %w", err)
	}

	return client.Run(ctx, func(ctx context.Context) error {
		_, err := client.Auth().Bot(ctx, os.Getenv("BOT_TOKEN"))
		return err
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := pebbleStorage(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	}
}
Output:

func NewSessionStorage

func NewSessionStorage(db *pebble.DB, key string) SessionStorage

NewSessionStorage creates new SessionStorage.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL