short

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2022 License: BSD-3-Clause Imports: 17 Imported by: 0

README

Go Report Card

go-short-url

A URL shortener library implemented in Go.

Installation

go get github.com/TomerHeber/go-short-url

Documentation

Documentation is available at: https://pkg.go.dev/github.com/TomerHeber/go-short-url

Example

Below is a basic example (Note: errors are not checked):

// Create a new shortner.
s, _ := short.NewShortener()

// Create a shortened url for "https://www.google.com?a=b&c=d". 
surl, _ := s.CreateShortenedUrl(context.TODO(), "https://www.google.com?a=b&c=d")

// Return the original url from the shortened url.
ourl, _ := s.GetUrlFromShortenedUrl(context.TODO(), surl.GetUrl())

Running the webserver example

A more complete example is available under the example directory. It's a tiny webserver that generates short urls.

docker-compose up -d
go run ./example/main.go

MongoDB

To store the mappings between short urls and their original urls, A MongoDB database is required.

Development

Install golangci-lint:
Check https://golangci-lint.run/usage/install/#local-installation for installation instructions.

Install pre-commit:
Check https://pre-commit.com/ for installation instructions.

Enable the git pre commit hooks:
pre-commit install

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config interface {

	// WithHost sets the hosts of the shortened url.
	// E.g. if host is `my.url“ the shortened url could be `https://my.url/eRt35df`.
	WithHost(host string) Config

	// WithMongo sets the URI for connecting to Mongo.
	// https://www.mongodb.com/docs/manual/reference/connection-string/
	// Example: `mongodb://root:password123@198.174.21.23:27017/databasename`
	WithMongoUri(mongoUri string) Config
	// contains filtered or unexported methods
}

Config is used to customize the shortener. To create a config instance use `DefaultConfig()`.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a configuration with default values. default host: `localhost:8080`. default mongo URI: `mongodb://localhost:27017`.

type ConflictError

type ConflictError struct{}

func (*ConflictError) Error

func (e *ConflictError) Error() string

type IdNotFoundError

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

func (*IdNotFoundError) Error

func (e *IdNotFoundError) Error() string

type ShortenedURL

type ShortenedURL interface {
	GetUrl() string
}

type Shortener

type Shortener interface {
	// CreateShortenedUrl creates a shortened url for `url`.
	CreateShortenedUrl(ctx context.Context, url string, config ...UrlConfig) (ShortenedURL, error)
	// GetUrlFromShortenedUrl receives a shortened url `surl` and returns the original url.
	// E.g.: https://short.com/abCD123
	GetUrlFromShortenedUrl(ctx context.Context, surl string) (string, error)
	// GetUrlFromShortenedUrl receives a shortened url `id` and returns the original url.
	// E.g.: abCD123
	GetUrlFromShortenedUrlId(ctx context.Context, id string) (string, error)
}

func NewShortener

func NewShortener(config ...Config) (Shortener, error)

NewShortener creates a new shortener. If no configuration is passed uses the default configuration (see: `DefaultConfig()`)

type Store

type Store interface {
	// Insert adds a record to the storage.
	// url, id and override are passed via an insertConfig struct.
	Insert(ctx context.Context, ic *insertConfig) error
	// GetUrl returns the url given an id.
	GetUrl(ctx context.Context, id string) (string, error)
}

type UrlConfig

type UrlConfig interface {

	// WithAlias sets a short url alias instead of generating a random one.
	// E.g.: if the alias is `tastypizzas` the shortened url could be https://link.com/tastypizzas
	WithAlias(alias string) UrlConfig

	// WithOverrideAlias set the override configuration.
	// When override is `true` it will insert a new or override an existing shortened url.
	// This field is ignored when there is no alias.
	WithOverrideAlias(override bool) UrlConfig

	// WithExpirationDate sets an expiration date for the shortened url.
	// Once the expiration date has expired the url becomes invalid or allocated for other urls.
	WithExpirationDate(expriationDate time.Time) UrlConfig
	// contains filtered or unexported methods
}

UrlConfig may be used to customize the way a url is shortened. A UrlConfig instance can be created by calling `DefaultUrlConfig()`.

func DefaultUrlConfig

func DefaultUrlConfig() UrlConfig

DefaultConfig returns a configuration with default values. default alias: "" (empty string). default overrideAlias: false. default expirationDate: no expiration.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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