etcd

package
v0.3.0-pre Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package etcd contains gotd storage implementations using etcd.

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"
	"time"

	clientv3 "go.etcd.io/etcd/client/v3"
	"golang.org/x/xerrors"

	"github.com/gotd/td/telegram"

	"github.com/gotd/contrib/auth"
	"github.com/gotd/contrib/auth/terminal"
	"github.com/gotd/contrib/etcd"
)

func etcdAuth(ctx context.Context) error {
	c, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{"localhost:2379"},
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		return xerrors.Errorf("create etcd storage: %w", err)
	}
	cred := etcd.NewCredentials(c).
		WithPhoneKey("phone").
		WithPasswordKey("password")

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

	return client.Run(ctx, func(ctx context.Context) error {
		return client.AuthIfNecessary(
			ctx,
			telegram.NewAuth(auth.Build(cred, terminal.OS()), telegram.SendCodeOptions{}),
		)
	})
}

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

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

func NewCredentials

func NewCredentials(client *clientv3.Client) Credentials

NewCredentials creates new Credentials.

type PeerStorage

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

PeerStorage is a peer storage based on etcd.

func NewPeerStorage

func NewPeerStorage(etcd *clientv3.Client) *PeerStorage

NewPeerStorage creates new peer storage using etcd.

func (PeerStorage) Add

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

Add adds given peer to the storage.

func (PeerStorage) Assign

func (s PeerStorage) Assign(ctx context.Context, key string, value storage.Peer) 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.Key) (storage.Peer, error)

Find finds peer using given key.

func (PeerStorage) Resolve

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

Resolve finds peer using associated key.

type SessionStorage

type SessionStorage struct {
	kv.Session
}

SessionStorage is a MTProto session Redis storage.

Example
package main

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

	clientv3 "go.etcd.io/etcd/client/v3"
	"golang.org/x/xerrors"

	"github.com/gotd/td/telegram"

	"github.com/gotd/contrib/etcd"
)

func etcdStorage(ctx context.Context) error {
	c, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{"localhost:2379"},
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		return xerrors.Errorf("create etcd storage: %w", err)
	}
	storage := etcd.NewSessionStorage(c, "session")

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

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

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

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

func NewSessionStorage

func NewSessionStorage(client *clientv3.Client, 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