chore: use shortie as a internal package

This commit is contained in:
2022-05-16 11:04:31 +02:00
parent 9841b9d71a
commit 473deec7e9
5 changed files with 7 additions and 4 deletions

15
internal/shortie/go.mod Normal file
View File

@ -0,0 +1,15 @@
module shortie
go 1.18
require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/valyala/fasthttp v1.36.0
)
require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/klauspost/compress v1.15.4 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
)

27
internal/shortie/go.sum Normal file
View File

@ -0,0 +1,27 @@
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.4 h1:1kn4/7MepF/CHmYub99/nNX8az0IJjfSOU/jbnTVfqQ=
github.com/klauspost/compress v1.15.4/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.36.0 h1:NhqfO/cB7Ajn1czkKnWkMHyPYr5nyND14ZGPk23g0/c=
github.com/valyala/fasthttp v1.36.0/go.mod h1:t/G+3rLek+CyY9bnIE+YlMRddxVAAGjhxndDB4i4C0I=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

307
internal/shortie/shortie.go Normal file
View File

@ -0,0 +1,307 @@
package shortie
import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/asaskevich/govalidator"
"github.com/patrickmn/go-cache"
"github.com/valyala/fasthttp"
)
type body struct {
FullHeader bool
IsGhost bool
HasForm bool
IsLink bool
H1 string
H3 string
Line1 string
Line2 string
}
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" // Base strings for randStringBytesMaskImprSrc
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var (
// Src tiny entropy Pool
Src = rand.NewSource(time.Now().UnixNano())
// Pool KV memory DB
Pool *cache.Cache
// URLSize is the default size of the shortened URL
URLSize int = 10
// Port is the listening port address
Port int = 8080
// Exp is the expiration time of the shortened URL
Exp time.Duration = 240 * time.Hour
// Cleanup is the clean up time interval
Cleanup time.Duration = 1 * time.Hour
// Proto is the shortened URL protocol
Proto string = "https"
// Domain is the shortened URL domain
Domain string = "localhost"
// Path is the shortened URL prefix path
Path string = ""
// DumpFile is the file to dump URL data
DumpFile string = "urls.json"
)
// get executes the GET command
func get(key string) (string, bool) {
value, status := Pool.Get(key)
if !status {
return "", status
}
return value.(string), status
}
// set executes the redis SET command
func set(key, suffix string) {
Pool.Set(suffix, key, 0)
}
// randStringBytesMaskImprSrc Generate random string of n size
func randStringBytesMaskImprSrc(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, Src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = Src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
// redirect receives a key searches the kv database for it and if
// found returns the value, or a error if not found
func redirect(k string) (string, error) {
rgx, _ := regexp.Compile("[a-zA-Z0-9]+")
key := rgx.FindString(k)
key, status := get(key)
if !status {
return "", fmt.Errorf("Not Found")
}
u, _ := url.Parse(key)
if u.Scheme == "" {
u.Scheme = "https"
}
return u.String(), nil
}
// shortener receive a url, validates the url, generate a random suffix string
// of urlSize size, checks if the suffix string is ensure on the kv database
// and then writes the kv pair (suffix, url) to the database, returning the suffix
func shortener(u []byte, s int) (string, error) {
var su string
us := string(u)
if !govalidator.IsURL(string(us)) {
return su, fmt.Errorf("Bad Request")
}
pu, _ := url.Parse(us)
for {
su = randStringBytesMaskImprSrc(s)
_, status := get(su)
if !status {
break
}
}
set(pu.String(), su)
return su, nil
}
// dumpDbToFile dumps the kv pairs from the in memory database to file
func dumpDbTOFile() (int, error) {
i := Pool.Items()
dumpObj, _ := json.Marshal(i)
return len(i), ioutil.WriteFile(DumpFile, dumpObj, 0644)
}
// loadFromFile loads kv pairs from the dumpFile json to the in memory database
func loadFromFile() (int, error) {
dumpObj := make(map[string]cache.Item)
jsonFile, err := ioutil.ReadFile(DumpFile)
if err != nil {
return 0, err
}
err = json.Unmarshal([]byte(jsonFile), &dumpObj)
if err != nil {
return 0, err
}
Pool = cache.NewFrom(Exp, Cleanup, dumpObj)
return len(dumpObj), err
}
// itemsFromPost loads kv pairs from a json POST to the in memory database
func loadFromJSON(j []byte) (int, error) {
dumpObj := make(map[string]cache.Item)
err := json.Unmarshal(j, &dumpObj)
if err != nil {
return 0, err
}
Pool = cache.NewFrom(Exp, Cleanup, dumpObj)
return len(dumpObj), nil
}
func internalError(msg string, err error) body {
log.Println(err)
return body{
FullHeader: true,
IsGhost: true,
HasForm: true,
H1: "500",
H3: msg,
Line1: "Boo, the ghost is broken :(",
Line2: "His last words where: " + err.Error(),
}
}
// IndexHandler return a fasthttp.RequestHandler function that genetares the index page
func IndexHandler(t *template.Template) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
t.Execute(ctx, body{
HasForm: true,
Line1: "Welcome to Short, the simple URL shortener,",
Line2: "Type an URL below to shorten it",
})
}
}
// Short return a fasthttp.RequestHandler function that genetares the shortener page
func Short(t *template.Template) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
suf, err := shortener(ctx.FormValue("url"), URLSize)
if err != nil {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
t.Execute(ctx, body{
FullHeader: true,
IsGhost: true,
HasForm: true,
H1: "400",
H3: err.Error(),
Line1: "Boo, looks like this ghost stole this page!",
Line2: "But you can type an URL below to shorten it",
})
return
}
ru, _ := url.Parse(fmt.Sprintf("%s://%s:%v/%s%s", Proto, Domain, Port, Path, suf))
t.Execute(ctx, body{
IsLink: true,
Line1: ru.String(),
})
}
}
// Redir return a fasthttp.RequestHandler function that genetares the shortener redirect page
func Redir(t *template.Template) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
key := ctx.UserValue("key")
if Path != "" {
key = strings.Replace(key.(string), Path, "", 1)
}
u, err := redirect(key.(string))
if err != nil {
ctx.SetStatusCode(fasthttp.StatusNotFound)
t.Execute(ctx, body{
FullHeader: true,
IsGhost: true,
HasForm: true,
H1: "404",
H3: err.Error(),
Line1: "Boo, looks like this ghost stole this page!",
Line2: "But you can type an URL below to shorten it",
})
return
}
ctx.Redirect(u, fasthttp.StatusFound)
}
}
// ToFile return a fasthttp.RequestHandler function that dumps the contents of the
// KV db to the DumpFile file
func ToFile(t *template.Template) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) {
i, err := dumpDbTOFile()
if err != nil {
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
t.Execute(ctx, internalError("Failed to dump kv DB to file", err))
return
}
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
t.Execute(ctx, body{Line1: fmt.Sprintf("Exported %v items to %v", i, DumpFile)})
}
}
// FromFile return a fasthttp.RequestHandler function that loads the content of the DumpFile into the
// KV db
func FromFile(t *template.Template) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) {
i, err := loadFromFile()
if err != nil {
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
t.Execute(ctx, internalError("Error loading DB from file", err))
return
}
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
t.Execute(ctx, body{Line1: fmt.Sprintf("Imported %v items to the DB", i)})
}
}
// Dump return a fasthttp.RequestHandler function that dumps the contents of the
// KV db to the ctx handler
func Dump(t *template.Template) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) {
dumpObj, err := json.Marshal(Pool.Items())
if err != nil {
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
t.Execute(ctx, internalError("Unable to dump key value db: ", err))
return
}
fmt.Fprintf(ctx, "%s", dumpObj)
}
}
// FromPost return a fasthttp.RequestHandler function that loads the content of the JSON POST into the
// KV db
func FromPost(t *template.Template) func(ctx *fasthttp.RequestCtx) {
return func(ctx *fasthttp.RequestCtx) {
i, err := loadFromJSON(ctx.PostBody())
if err != nil {
ctx.SetStatusCode(http.StatusInternalServerError)
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
t.Execute(ctx, internalError("Error loading DB", err))
return
}
ctx.Response.Header.SetCanonical([]byte("Content-Type"), []byte("text/html"))
t.Execute(ctx, body{Line1: fmt.Sprintf("Imported %v items to the DB", i)})
}
}