db

package
v0.0.0-...-153d4bb Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2021 License: BSD-3-Clause Imports: 4 Imported by: 0

Documentation

Overview

Package db contains interface and struct types to be implemented by storage backends.

Index

Constants

View Source
const (
	// ErrAlreadyExists is returned when a URL with the given name already exists.
	ErrAlreadyExists = errors.Sentinel("url with this name already exists")
	// ErrNotFound is returned when a URL with the given name doesn't exist.
	ErrNotFound = errors.Sentinel("url with this name doesn't exist")

	// ErrUserNotFound is returned when the given user is not found.
	ErrUserNotFound = errors.Sentinel("user not found")
)

Variables

This section is empty.

Functions

func NewUserContext

func NewUserContext(ctx context.Context, user *User) context.Context

NewUserContext ...

Types

type DB

type DB interface {
	// URL returns the full URL of the given short link
	URL(short string) (u URL, err error)

	// RandomURL generates a random short URL
	RandomURL(userID uuid.UUID, url string) (u URL, err error)
	// NamedURL creates a short URL with the given name
	NamedURL(userID uuid.UUID, short, url string) (u URL, err error)

	// User returns the user with the given UUID.
	User(id uuid.UUID) (u User, err error)
	// CreateUser creates a user with the given username
	CreateUser(name string) (u User, token string, err error)
	// ValidateToken validates the given token
	ValidateToken(token string) (u *User, valid bool, err error)
	// RegenerateToken generates a new token
	RegenerateToken(id uuid.UUID) (token string, err error)
}

DB is a database to store URLs, their expiration, and users.

type URL

type URL struct {
	ID uuid.UUID `json:"id"`

	// The short URL
	Short string `json:"short"`
	// The full URL to redirect to
	URL string `json:"url"`

	// The user that created this short URL
	User User `json:"user"`

	// The time this URL expires and is removed from the database, nil if it doesn't expire.
	Expires *time.Time `json:"expires,omitempty"`
}

URL is a single short URL.

type User

type User struct {
	ID       uuid.UUID `json:"id"`
	Username string    `json:"username"`
}

User is a user.

func UserFromContext

func UserFromContext(ctx context.Context) (*User, bool)

UserFromContext ...

Jump to

Keyboard shortcuts

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