-
Notifications
You must be signed in to change notification settings - Fork 0
/
shrt.go
84 lines (70 loc) · 1.82 KB
/
shrt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package goshrt
import (
"fmt"
"net/url"
"regexp"
"strings"
"time"
)
const (
slugAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)
type Shrt struct {
ID int `json:"id"` // ID
Domain string `json:"domain"` // Domain
Slug string `json:"slug"` // Everything avfter domain
Dest string `json:"destination"` // Destination URL
Expiry time.Time `json:"expire"` // Timestamp for expire
}
// Printp pretty prints Shrt struct.
func (s *Shrt) Printp() {
var timestring string
if s.Expiry.IsZero() {
timestring = "not set"
} else {
timestring = s.Expiry.Format("2006.02.01")
}
fmt.Printf("ID\t\t%d\n", s.ID)
fmt.Printf("Domain\t\t%s\n", s.Domain)
fmt.Printf("Slug\t\t%s\n", s.Slug)
fmt.Printf("Destination\t%s\n", s.Dest)
fmt.Printf("Expiry\t\t%s\n", timestring)
}
func (s *Shrt) ValidDest() bool {
_, err := url.ParseRequestURI(s.Dest)
if err != nil {
return false
}
u, err := url.Parse(s.Dest)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
}
return true
}
type ShrtStorer interface {
Migrate() error
Open() error
Close() error
CreateShrt(s *Shrt) error
Shrt(d, s string) (*Shrt, error)
ShrtByID(id int) (*Shrt, error)
DeleteByID(id int) (*Shrt, error)
Shrts() ([]*Shrt, error)
ShrtsByDomain(d string) ([]*Shrt, error)
// TODO: Same as above with pagination
}
// GenerateSlug generates slug with length l.
func GenerateSlug(l uint64) string {
var encodedBuilder strings.Builder
length := len(slugAlphabet)
encodedBuilder.Grow(10)
for ; l > 0; l /= uint64(length) {
encodedBuilder.WriteByte(slugAlphabet[(l % uint64(length))])
}
return encodedBuilder.String()
}
// ValidateSlug validates slug.
func ValidateSlug(s string) bool {
r := regexp.MustCompile(`public(/|$)|api(/|$)|\?|#`)
return !r.MatchString(s)
}