Fix epic typo :O

This commit is contained in:
2016-07-11 18:16:57 +01:00
parent e3ec2ff16c
commit cbdd5790bd
9 changed files with 42 additions and 42 deletions

View File

@@ -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 What do you do when you wanna mess with a new programming language? You make a
URL shortner, duh! URL shortener, duh!

View File

@@ -4,7 +4,7 @@ import (
"log" "log"
"os" "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/storage/goleveldbstore"
"github.com/jimeh/ozu.io/web" "github.com/jimeh/ozu.io/web"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
@@ -17,8 +17,8 @@ func main() {
} }
defer store.Close() defer store.Close()
shortner := shortner.New(store) s := shortener.New(store)
router := web.NewRouter(shortner) router := web.NewRouter(s)
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port == "" { if port == "" {

View File

@@ -1,4 +1,4 @@
package shortner package shortener
import ( import (
"errors" "errors"

View File

@@ -1,4 +1,4 @@
package shortner package shortener
import ( import (
"strings" "strings"

View File

@@ -1,4 +1,4 @@
package shortner package shortener
import ( import (
"crypto/sha1" "crypto/sha1"
@@ -9,20 +9,20 @@ import (
) )
// New returns a new *Shortner that uses the given storage.Store. // New returns a new *Shortner that uses the given storage.Store.
func New(store storage.Store) *Shortner { func New(store storage.Store) *Shortener {
return &Shortner{Store: store} return &Shortener{Store: store}
} }
var urlKeyPrefix = []byte("url:") var urlKeyPrefix = []byte("url:")
var uidKeyPrefix = []byte("uid:") var uidKeyPrefix = []byte("uid:")
// Shortner interface // Shortner interface
type Shortner struct { type Shortener struct {
Store storage.Store Store storage.Store
} }
// Shorten a given URL. // 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) url, err = NormalizeURL(rawURL)
if err != nil { if err != nil {
return nil, nil, err 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. // 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) uidKey := s.makeUIDKey(uid)
url, err := s.Store.Get(uidKey) url, err := s.Store.Get(uidKey)
@@ -68,7 +68,7 @@ func (s *Shortner) Lookup(uid []byte) ([]byte, error) {
return url, nil return url, nil
} }
func (s *Shortner) newUID() ([]byte, error) { func (s *Shortener) newUID() ([]byte, error) {
index, err := s.Store.NextSequence() index, err := s.Store.NextSequence()
if err != nil { if err != nil {
return nil, err return nil, err
@@ -77,11 +77,11 @@ func (s *Shortner) newUID() ([]byte, error) {
return base58.Encode(index), nil return base58.Encode(index), nil
} }
func (s *Shortner) makeUIDKey(uid []byte) []byte { func (s *Shortener) makeUIDKey(uid []byte) []byte {
return append(uidKeyPrefix, uid...) 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)) urlSHA := fmt.Sprintf("%x", sha1.Sum(rawURL))
return append(urlKeyPrefix, urlSHA...) return append(urlKeyPrefix, urlSHA...)
} }

View File

@@ -1,4 +1,4 @@
package shortner package shortener
import ( import (
"crypto/sha1" "crypto/sha1"
@@ -7,7 +7,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/jimeh/ozu.io/shortner/mocks" "github.com/jimeh/ozu.io/shortener/mocks"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
) )
@@ -17,36 +17,36 @@ import (
// Suite Setup // Suite Setup
type ShortnerSuite struct { type ShortenerSuite struct {
suite.Suite suite.Suite
store *mocks.Store store *mocks.Store
shortner *Shortner shortener *Shortener
errNotFound error errNotFound error
} }
func (s *ShortnerSuite) SetupTest() { func (s *ShortenerSuite) SetupTest() {
s.store = new(mocks.Store) s.store = new(mocks.Store)
s.shortner = New(s.store) s.shortener = New(s.store)
s.errNotFound = errors.New("not found") s.errNotFound = errors.New("not found")
} }
// Tests // Tests
func (s *ShortnerSuite) TestShortenExisting() { func (s *ShortenerSuite) TestShortenExisting() {
rawURL := []byte("http://google.com/") rawURL := []byte("http://google.com/")
uid := []byte("ig") uid := []byte("ig")
urlSHA := fmt.Sprintf("%x", sha1.Sum(rawURL)) urlSHA := fmt.Sprintf("%x", sha1.Sum(rawURL))
s.store.On("Get", append([]byte("url:"), urlSHA...)).Return(uid, nil) 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.NoError(err)
s.Equal(uid, resultUID) s.Equal(uid, resultUID)
s.Equal(rawURL, resultURL) s.Equal(rawURL, resultURL)
s.store.AssertExpectations(s.T()) s.store.AssertExpectations(s.T())
} }
func (s *ShortnerSuite) TestShortenNew() { func (s *ShortenerSuite) TestShortenNew() {
rawURL := []byte("https://google.com") rawURL := []byte("https://google.com")
url := []byte("https://google.com/") url := []byte("https://google.com/")
uid := []byte("ig") uid := []byte("ig")
@@ -57,7 +57,7 @@ func (s *ShortnerSuite) TestShortenNew() {
s.store.On("Set", urlKey, uid).Return(nil) s.store.On("Set", urlKey, uid).Return(nil)
s.store.On("Set", append([]byte("uid:"), uid...), url).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.NoError(err)
s.Equal(uid, rUID) s.Equal(uid, rUID)
@@ -65,7 +65,7 @@ func (s *ShortnerSuite) TestShortenNew() {
s.store.AssertExpectations(s.T()) s.store.AssertExpectations(s.T())
} }
func (s *ShortnerSuite) TestShortenInvalidURL() { func (s *ShortenerSuite) TestShortenInvalidURL() {
examples := []struct { examples := []struct {
url string url string
error string error string
@@ -93,45 +93,45 @@ func (s *ShortnerSuite) TestShortenInvalidURL() {
} }
for _, e := range examples { 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(rUID)
s.Nil(rURL) s.Nil(rURL)
s.EqualError(err, e.error) s.EqualError(err, e.error)
} }
} }
func (s *ShortnerSuite) TestShortenStoreError() { func (s *ShortenerSuite) TestShortenStoreError() {
url := []byte("https://google.com/") url := []byte("https://google.com/")
storeErr := errors.New("leveldb: something wrong") storeErr := errors.New("leveldb: something wrong")
urlKey := append([]byte("url:"), fmt.Sprintf("%x", sha1.Sum(url))...) urlKey := append([]byte("url:"), fmt.Sprintf("%x", sha1.Sum(url))...)
s.store.On("Get", urlKey).Return(nil, storeErr) 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(rUID)
s.Nil(rURL) s.Nil(rURL)
s.EqualError(err, storeErr.Error()) s.EqualError(err, storeErr.Error())
} }
func (s *ShortnerSuite) TestLookupExisting() { func (s *ShortenerSuite) TestLookupExisting() {
url := []byte("https://google.com/") url := []byte("https://google.com/")
uid := []byte("ig") uid := []byte("ig")
s.store.On("Get", append([]byte("uid:"), uid...)).Return(url, nil) 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.NoError(err)
s.Equal(url, rURL) s.Equal(url, rURL)
s.store.AssertExpectations(s.T()) s.store.AssertExpectations(s.T())
} }
func (s *ShortnerSuite) TestLookupNonExistant() { func (s *ShortenerSuite) TestLookupNonExistant() {
uid := []byte("ig") uid := []byte("ig")
s.store.On("Get", append([]byte("uid:"), uid...)).Return(nil, s.errNotFound) 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.EqualError(err, "not found")
s.Nil(rURL) s.Nil(rURL)
@@ -140,6 +140,6 @@ func (s *ShortnerSuite) TestLookupNonExistant() {
// Run Suite // Run Suite
func TestShortnerSuite(t *testing.T) { func TestShortenerSuite(t *testing.T) {
suite.Run(t, new(ShortnerSuite)) suite.Run(t, new(ShortenerSuite))
} }

View File

@@ -5,19 +5,19 @@ import (
"fmt" "fmt"
"net/url" "net/url"
"github.com/jimeh/ozu.io/shortner" "github.com/jimeh/ozu.io/shortener"
"github.com/qiangxue/fasthttp-routing" "github.com/qiangxue/fasthttp-routing"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
) )
// Handlers handle HTTP requests. // Handlers handle HTTP requests.
type Handlers struct { type Handlers struct {
s *shortner.Shortner s *shortener.Shortener
} }
// Index handles requests for root. // Index handles requests for root.
func (h *Handlers) Index(c *routing.Context) error { 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 return nil
} }

View File

@@ -1,14 +1,14 @@
package web package web
import ( import (
"github.com/jimeh/ozu.io/shortner" "github.com/jimeh/ozu.io/shortener"
"github.com/qiangxue/fasthttp-routing" "github.com/qiangxue/fasthttp-routing"
) )
// NewRouter creates a new routing.Router with all handlers registered. // 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() router := routing.New()
handlers := Handlers{shortner} handlers := Handlers{shortener}
router.Get("/", handlers.Index) router.Get("/", handlers.Index)
router.Get("/api/shorten", handlers.Shorten) router.Get("/api/shorten", handlers.Shorten)