linkshortener

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2020 License: BSD-3-Clause Imports: 9 Imported by: 2

README

Short but not for long

builds.sr.ht status

This is a small link shortener written in golang. It's not meant for permanently shortening link. So in case you don't want dead links after a reboot, this is not for you.

On top of forgetting everything on reboot, it can't hold many links, the upper limit is math.MaxUint16.

Usage example

func main() {
    shortener := NewShortener(1234)
    fmt.Println(shortener.Shorten("https://google.com"))

    blocker := make(chan struct{})
    go func() {
        log.Fatalln(shortener.Start())
        blocker <- struct{}{}
    }()

    <-blocker
}

Run it, open your browser and visit the link that the main-function spits out.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedirectHandler

type RedirectHandler struct {
	Shortener *Shortener
}

RedirectHandler handles all the redirects for the Server.

func (RedirectHandler) ServeHTTP

func (h RedirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Shortener

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

Shortener offers a function to shorten a URL and redirect to the shortened URL as soon as a request comes in.

func NewShortener

func NewShortener(port int) *Shortener

NewShortener creates a new server that uses the given port.

func (*Shortener) CalculateShortenedLength added in v1.1.1

func (shortener *Shortener) CalculateShortenedLength(url string) (int, int)

CalculateShortenedLength returns the length of the shortened URL without a suffix and the length of the suffix, which is 0 if the suffix isn't available.

func (*Shortener) Close

func (shortener *Shortener) Close()

Close closes the internal http server.

func (*Shortener) Shorten

func (shortener *Shortener) Shorten(url string) (string, string)

Shorten takes a url and returns a shortend version that redirects via the local webserver.

func (*Shortener) Start

func (shortener *Shortener) Start() error

Start servers the internal http server, blocks and returns an error on failure.

Jump to

Keyboard shortcuts

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