mirror of
https://github.com/jimeh/ozu.io.git
synced 2026-02-19 08:06:39 +00:00
Fix epic typo :O
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# ozu.io - A shitty URL shortner
|
||||
# ozu.io - A shitty URL shortener
|
||||
|
||||
What do you do when you wanna mess with a new programming language? You make a
|
||||
URL shortner, duh!
|
||||
URL shortener, duh!
|
||||
|
||||
6
main.go
6
main.go
@@ -4,7 +4,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/jimeh/ozu.io/shortner"
|
||||
"github.com/jimeh/ozu.io/shortener"
|
||||
"github.com/jimeh/ozu.io/storage/goleveldbstore"
|
||||
"github.com/jimeh/ozu.io/web"
|
||||
"github.com/valyala/fasthttp"
|
||||
@@ -17,8 +17,8 @@ func main() {
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
shortner := shortner.New(store)
|
||||
router := web.NewRouter(shortner)
|
||||
s := shortener.New(store)
|
||||
router := web.NewRouter(s)
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package shortner
|
||||
package shortener
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -1,4 +1,4 @@
|
||||
package shortner
|
||||
package shortener
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -1,4 +1,4 @@
|
||||
package shortner
|
||||
package shortener
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
@@ -9,20 +9,20 @@ import (
|
||||
)
|
||||
|
||||
// New returns a new *Shortner that uses the given storage.Store.
|
||||
func New(store storage.Store) *Shortner {
|
||||
return &Shortner{Store: store}
|
||||
func New(store storage.Store) *Shortener {
|
||||
return &Shortener{Store: store}
|
||||
}
|
||||
|
||||
var urlKeyPrefix = []byte("url:")
|
||||
var uidKeyPrefix = []byte("uid:")
|
||||
|
||||
// Shortner interface
|
||||
type Shortner struct {
|
||||
type Shortener struct {
|
||||
Store storage.Store
|
||||
}
|
||||
|
||||
// Shorten a given URL.
|
||||
func (s *Shortner) Shorten(rawURL []byte) (uid []byte, url []byte, err error) {
|
||||
func (s *Shortener) Shorten(rawURL []byte) (uid []byte, url []byte, err error) {
|
||||
url, err = NormalizeURL(rawURL)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
@@ -57,7 +57,7 @@ func (s *Shortner) Shorten(rawURL []byte) (uid []byte, url []byte, err error) {
|
||||
}
|
||||
|
||||
// Lookup the URL of a given UID.
|
||||
func (s *Shortner) Lookup(uid []byte) ([]byte, error) {
|
||||
func (s *Shortener) Lookup(uid []byte) ([]byte, error) {
|
||||
uidKey := s.makeUIDKey(uid)
|
||||
|
||||
url, err := s.Store.Get(uidKey)
|
||||
@@ -68,7 +68,7 @@ func (s *Shortner) Lookup(uid []byte) ([]byte, error) {
|
||||
return url, nil
|
||||
}
|
||||
|
||||
func (s *Shortner) newUID() ([]byte, error) {
|
||||
func (s *Shortener) newUID() ([]byte, error) {
|
||||
index, err := s.Store.NextSequence()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -77,11 +77,11 @@ func (s *Shortner) newUID() ([]byte, error) {
|
||||
return base58.Encode(index), nil
|
||||
}
|
||||
|
||||
func (s *Shortner) makeUIDKey(uid []byte) []byte {
|
||||
func (s *Shortener) makeUIDKey(uid []byte) []byte {
|
||||
return append(uidKeyPrefix, uid...)
|
||||
}
|
||||
|
||||
func (s *Shortner) makeURLKey(rawURL []byte) []byte {
|
||||
func (s *Shortener) makeURLKey(rawURL []byte) []byte {
|
||||
urlSHA := fmt.Sprintf("%x", sha1.Sum(rawURL))
|
||||
return append(urlKeyPrefix, urlSHA...)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package shortner
|
||||
package shortener
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jimeh/ozu.io/shortner/mocks"
|
||||
"github.com/jimeh/ozu.io/shortener/mocks"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
@@ -17,36 +17,36 @@ import (
|
||||
|
||||
// Suite Setup
|
||||
|
||||
type ShortnerSuite struct {
|
||||
type ShortenerSuite struct {
|
||||
suite.Suite
|
||||
store *mocks.Store
|
||||
shortner *Shortner
|
||||
shortener *Shortener
|
||||
errNotFound error
|
||||
}
|
||||
|
||||
func (s *ShortnerSuite) SetupTest() {
|
||||
func (s *ShortenerSuite) SetupTest() {
|
||||
s.store = new(mocks.Store)
|
||||
s.shortner = New(s.store)
|
||||
s.shortener = New(s.store)
|
||||
s.errNotFound = errors.New("not found")
|
||||
}
|
||||
|
||||
// Tests
|
||||
|
||||
func (s *ShortnerSuite) TestShortenExisting() {
|
||||
func (s *ShortenerSuite) TestShortenExisting() {
|
||||
rawURL := []byte("http://google.com/")
|
||||
uid := []byte("ig")
|
||||
urlSHA := fmt.Sprintf("%x", sha1.Sum(rawURL))
|
||||
|
||||
s.store.On("Get", append([]byte("url:"), urlSHA...)).Return(uid, nil)
|
||||
|
||||
resultUID, resultURL, err := s.shortner.Shorten(rawURL)
|
||||
resultUID, resultURL, err := s.shortener.Shorten(rawURL)
|
||||
s.NoError(err)
|
||||
s.Equal(uid, resultUID)
|
||||
s.Equal(rawURL, resultURL)
|
||||
s.store.AssertExpectations(s.T())
|
||||
}
|
||||
|
||||
func (s *ShortnerSuite) TestShortenNew() {
|
||||
func (s *ShortenerSuite) TestShortenNew() {
|
||||
rawURL := []byte("https://google.com")
|
||||
url := []byte("https://google.com/")
|
||||
uid := []byte("ig")
|
||||
@@ -57,7 +57,7 @@ func (s *ShortnerSuite) TestShortenNew() {
|
||||
s.store.On("Set", urlKey, uid).Return(nil)
|
||||
s.store.On("Set", append([]byte("uid:"), uid...), url).Return(nil)
|
||||
|
||||
rUID, rURL, err := s.shortner.Shorten(rawURL)
|
||||
rUID, rURL, err := s.shortener.Shorten(rawURL)
|
||||
|
||||
s.NoError(err)
|
||||
s.Equal(uid, rUID)
|
||||
@@ -65,7 +65,7 @@ func (s *ShortnerSuite) TestShortenNew() {
|
||||
s.store.AssertExpectations(s.T())
|
||||
}
|
||||
|
||||
func (s *ShortnerSuite) TestShortenInvalidURL() {
|
||||
func (s *ShortenerSuite) TestShortenInvalidURL() {
|
||||
examples := []struct {
|
||||
url string
|
||||
error string
|
||||
@@ -93,45 +93,45 @@ func (s *ShortnerSuite) TestShortenInvalidURL() {
|
||||
}
|
||||
|
||||
for _, e := range examples {
|
||||
rUID, rURL, err := s.shortner.Shorten([]byte(e.url))
|
||||
rUID, rURL, err := s.shortener.Shorten([]byte(e.url))
|
||||
s.Nil(rUID)
|
||||
s.Nil(rURL)
|
||||
s.EqualError(err, e.error)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ShortnerSuite) TestShortenStoreError() {
|
||||
func (s *ShortenerSuite) TestShortenStoreError() {
|
||||
url := []byte("https://google.com/")
|
||||
storeErr := errors.New("leveldb: something wrong")
|
||||
urlKey := append([]byte("url:"), fmt.Sprintf("%x", sha1.Sum(url))...)
|
||||
|
||||
s.store.On("Get", urlKey).Return(nil, storeErr)
|
||||
|
||||
rUID, rURL, err := s.shortner.Shorten(url)
|
||||
rUID, rURL, err := s.shortener.Shorten(url)
|
||||
s.Nil(rUID)
|
||||
s.Nil(rURL)
|
||||
s.EqualError(err, storeErr.Error())
|
||||
}
|
||||
|
||||
func (s *ShortnerSuite) TestLookupExisting() {
|
||||
func (s *ShortenerSuite) TestLookupExisting() {
|
||||
url := []byte("https://google.com/")
|
||||
uid := []byte("ig")
|
||||
|
||||
s.store.On("Get", append([]byte("uid:"), uid...)).Return(url, nil)
|
||||
|
||||
rURL, err := s.shortner.Lookup(uid)
|
||||
rURL, err := s.shortener.Lookup(uid)
|
||||
|
||||
s.NoError(err)
|
||||
s.Equal(url, rURL)
|
||||
s.store.AssertExpectations(s.T())
|
||||
}
|
||||
|
||||
func (s *ShortnerSuite) TestLookupNonExistant() {
|
||||
func (s *ShortenerSuite) TestLookupNonExistant() {
|
||||
uid := []byte("ig")
|
||||
|
||||
s.store.On("Get", append([]byte("uid:"), uid...)).Return(nil, s.errNotFound)
|
||||
|
||||
rURL, err := s.shortner.Lookup(uid)
|
||||
rURL, err := s.shortener.Lookup(uid)
|
||||
|
||||
s.EqualError(err, "not found")
|
||||
s.Nil(rURL)
|
||||
@@ -140,6 +140,6 @@ func (s *ShortnerSuite) TestLookupNonExistant() {
|
||||
|
||||
// Run Suite
|
||||
|
||||
func TestShortnerSuite(t *testing.T) {
|
||||
suite.Run(t, new(ShortnerSuite))
|
||||
func TestShortenerSuite(t *testing.T) {
|
||||
suite.Run(t, new(ShortenerSuite))
|
||||
}
|
||||
@@ -5,19 +5,19 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/jimeh/ozu.io/shortner"
|
||||
"github.com/jimeh/ozu.io/shortener"
|
||||
"github.com/qiangxue/fasthttp-routing"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
// Handlers handle HTTP requests.
|
||||
type Handlers struct {
|
||||
s *shortner.Shortner
|
||||
s *shortener.Shortener
|
||||
}
|
||||
|
||||
// Index handles requests for root.
|
||||
func (h *Handlers) Index(c *routing.Context) error {
|
||||
c.WriteString("Welcome to ozu.io, a shitty URL shortner.")
|
||||
c.WriteString("Welcome to ozu.io, a shitty URL shortener.")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"github.com/jimeh/ozu.io/shortner"
|
||||
"github.com/jimeh/ozu.io/shortener"
|
||||
"github.com/qiangxue/fasthttp-routing"
|
||||
)
|
||||
|
||||
// NewRouter creates a new routing.Router with all handlers registered.
|
||||
func NewRouter(shortner *shortner.Shortner) *routing.Router {
|
||||
func NewRouter(shortener *shortener.Shortener) *routing.Router {
|
||||
router := routing.New()
|
||||
handlers := Handlers{shortner}
|
||||
handlers := Handlers{shortener}
|
||||
|
||||
router.Get("/", handlers.Index)
|
||||
router.Get("/api/shorten", handlers.Shorten)
|
||||
|
||||
Reference in New Issue
Block a user