shortener

package
v0.0.0-...-3dea803 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package shortener implements saving/extracting URL to/from database and encoding/decoding URL database id to BASE62 formatted string.

Index

Constants

View Source
const EncShift = 1024 * 1024

EncShift is added to URL's id before encoding it to BASE62.

Variables

View Source
var (
	DecodeErr   = errors.New("code is incorrect")
	EncShiftErr = errors.New("code is less than encoding shift")
)

Functions

func Decode

func Decode(code string) (int64, error)

func Encode

func Encode(id int64) string

Types

type Engine

type Engine struct {
	DB *sqlx.DB
}

Engine contains the database for storing URLs.

func New

func New(db *sqlx.DB) Engine

New constructs a new Engine.

func (Engine) Expand

func (e Engine) Expand(ctx context.Context, code string) (string, error)

Expand takes the BASE62 code, decodes it and finds a corresponding URL in the database.

func (Engine) Shorten

func (e Engine) Shorten(ctx context.Context, url string) (string, error)

Shorten saves a URL to the database and returns its id encoded to BASE62 string.

Jump to

Keyboard shortcuts

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