shortener

package module
v0.0.0-...-b17f1b0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2019 License: MIT Imports: 1 Imported by: 0

README

go-url-shortener

Build Status Go Report Card

Golang url shortener using clean code.

Features:

  • Multiple storage engine: Redis and MongoDB.
  • Multiple message format: JSON and Msgpack.

Run Tests

  1. Set envvars
export MONGODB_URL=mongodb://localhost/shortener
export MONGODB_TIMEOUT=30
export MONGODB_DATABASE=shortener
export REDIS_URL=redis://localhost:6379
  1. Run tests
make test

Run the server

  1. Set envvars

For redis storage:

export PORT=3000
export STORAGE_ENGINE=redis
export REDIS_URL=redis://localhost:6379

For mongo storage:

export PORT=3000
export STORAGE_ENGINE=mongo
export MONGODB_URL=mongodb://localhost/shortener
export MONGODB_TIMEOUT=30
export MONGODB_DATABASE=shortener
  1. Run the server

With binary:

make build
./shortener

Without binary:

make run

How to use

Create new short url:

curl -X POST http://localhost:3000 \
  -H 'Content-Type: application/json' \
  -d '{
	"url": "https://github.com/allisson"
}'

The response:

{
    "code": "YzaFuvFWg",
    "url": "https://github.com/allisson",
    "created_at": 1566827456
}

Get redirected:

curl -X GET http://localhost:3000/YzaFuvFWg

The response:

<a href="https://github.com/allisson">Moved Permanently</a>.

If you want to use msgpack format, change Content-Type header to application/x-msgpack.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRedirectNotFound error
	ErrRedirectNotFound = errors.New("Redirect Not Found")
	// ErrRedirectInvalid error
	ErrRedirectInvalid = errors.New("Redirect Invalid")
)

Functions

This section is empty.

Types

type Redirect

type Redirect struct {
	Code      string `bson:"code" json:"code" msgpack:"code"`
	URL       string `bson:"url" json:"url" msgpack:"url" validate:"format=url"`
	CreatedAt int64  `bson:"created_at" msgpack:"created_at" json:"created_at"`
}

Redirect info

type RedirectRepository

type RedirectRepository interface {
	Find(code string) (*Redirect, error)
	Store(redirect *Redirect) error
}

RedirectRepository ...

type RedirectSerializer

type RedirectSerializer interface {
	Decode(input []byte) (*Redirect, error)
	Encode(input *Redirect) ([]byte, error)
}

RedirectSerializer represents a contract to convert bytes to pointer of Redirect and vice-versa

type RedirectService

type RedirectService interface {
	Find(code string) (*Redirect, error)
	Store(redirect *Redirect) error
}

RedirectService ...

Directories

Path Synopsis
cmd
repository
serializer

Jump to

Keyboard shortcuts

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