db

package
v0.0.0-...-29e63d5 Latest Latest
Warning

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

Go to latest
Published: May 15, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound represents the case when a long URL is no found
	ErrNotFound = errors.New("No items found by the db layer")
)

Functions

This section is empty.

Types

type PostgresDB

type PostgresDB struct {
	SchemaName string
	TableName  string
	// contains filtered or unexported fields
}

PostgresDB represents a connection to a Postgres database.

func (*PostgresDB) DeleteURL

func (pgDB *PostgresDB) DeleteURL(slug string) error

DeleteURL removes a URL from the database.

func (*PostgresDB) GetList

func (pgDB *PostgresDB) GetList() ([]ShortenObject, error)

GetList lists all shortened URLs.

func (*PostgresDB) GetLongURL

func (pgDB *PostgresDB) GetLongURL(slug string) (string, error)

GetLongURL searches for the short URL reference in order to return the long url.

func (*PostgresDB) ShortenURL

func (pgDB *PostgresDB) ShortenURL(slug, longURL, owner string, expires time.Time) error

ShortenURL creates a new record for a shortened URL.

type Redis

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

Redis is a wrapper over a Redis pool connection.

func (Redis) DeleteURL

func (r Redis) DeleteURL(slug string) error

DeleteURL removes all reference to a URL.

func (Redis) GetList

func (r Redis) GetList() ([]ShortenObject, error)

GetList lists all shortened URLs.

func (Redis) GetLongURL

func (r Redis) GetLongURL(slug string) (string, error)

GetLongURL searches for the short URL reference in order to return the long url.

func (Redis) ShortenURL

func (r Redis) ShortenURL(slug, longURL, owner string, expires time.Time) error

ShortenURL adds a URL object to the db.

type ShortenBackend

type ShortenBackend interface {
	DeleteURL(slug string) error
	ShortenURL(slug, longURL, owner string, expires time.Time) error
	GetLongURL(slug string) (string, error)
	GetList() ([]ShortenObject, error)
}

ShortenBackend represents the necessary interface for storing and updating URLs.

func NewPostgresDB

func NewPostgresDB() ShortenBackend

NewPostgresDB configures and connects to a postgres database.

func NewRedisDB

func NewRedisDB() ShortenBackend

NewRedisDB connects to Redis and pools connections.

type ShortenObject

type ShortenObject struct {
	Slug     string    `json:"slug"`
	Owner    string    `json:"owner"`
	LongURL  string    `json:"long_url"`
	Modified time.Time `json:"modified_date,omitempty"`
	Expires  time.Time `json:"expire_date,omitempty"`
}

ShortenObject holds the metadata and the mapping for a shortened URL I like the NullTime concept from the pq library, so even for other backends let's use it instead of checking whether the date is 0001-01-01

Jump to

Keyboard shortcuts

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