-
Notifications
You must be signed in to change notification settings - Fork 2
/
url.go
157 lines (143 loc) · 3.13 KB
/
url.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package ifth
import (
"time"
"github.com/globalsign/mgo/bson"
)
const (
RedirectProxy = "proxy"
Redirect301 = "301"
Redirect302 = "302"
Redirect303 = "303"
Redirect307 = "307"
)
type Url struct {
Slot string `json:"slot"`
Origin string `json:"origin"`
RedirectType string `json:"redirect_type" bson:"redirect_type"`
CreatedTime time.Time `json:"created_time" bson:"created_time"`
ExpiresIn int `json:"expires_in" bson:"expires_in"`
Count int64 `json:"count"`
Url string `json:"url" bson:"-"`
}
func NewUrl(origin string, unique bool) *Url {
//check origin exist
if unique && OriginExist(origin) {
url, err := FindUrlByOrigin(origin)
if err != nil {
return nil
}
return url
}
url := &Url{
Slot: SlotGenerator.Get(),
Origin: origin,
RedirectType: Redirect301,
CreatedTime: time.Now(),
ExpiresIn: 0,
Count: 0,
}
for {
// check slot exist
if !SlotExist(url.Slot) {
break
}
url.Slot = SlotGenerator.Get()
}
url.Save()
return url
}
func NewCustomUrl(slot, origin string) *Url {
if SlotExist(slot) {
return nil
}
url := &Url{
Slot: slot,
Origin: origin,
RedirectType: Redirect301,
CreatedTime: time.Now(),
ExpiresIn: 0,
Count: 0,
}
url.Save()
return url
}
func FindUrlByOrigin(origin string) (*Url, error) {
session := GetMgo()
defer session.Close()
var url Url
err := session.DB("ifth").C("url").Find(bson.M{"origin": origin}).One(&url)
if err != nil {
return nil, err
}
return &url, nil
}
func FindUrlBySlot(slot string) (*Url, error) {
session := GetMgo()
defer session.Close()
var url Url
err := session.DB("ifth").C("url").Find(bson.M{"slot": slot}).One(&url)
if err != nil {
return nil, err
}
url.Count++
url.Save()
return &url, nil
}
func FindHottestUrls(limit int) ([]Url, error) {
session := GetMgo()
defer session.Close()
var urls []Url
err := session.DB("ifth").C("url").Find(bson.M{}).Sort("-count").Limit(limit).All(&urls)
if err != nil {
return nil, err
}
return urls, nil
}
func FindNewestUrls(limit int) ([]Url, error) {
session := GetMgo()
defer session.Close()
var urls []Url
err := session.DB("ifth").C("url").Find(bson.M{}).Sort("-_id").Limit(limit).All(&urls)
if err != nil {
return nil, err
}
return urls, nil
}
func OriginExist(origin string) bool {
session := GetMgo()
defer session.Close()
ct, err := session.DB("ifth").C("url").Find(bson.M{"origin": origin}).Count()
if err != nil {
return false //may cause problem?
}
if ct > 0 {
return true
}
return false
}
func SlotExist(slot string) bool {
session := GetMgo()
defer session.Close()
ct, err := session.DB("ifth").C("url").Find(bson.M{"slot": slot}).Count()
if err != nil {
return false //may cause problem?
}
if ct > 0 {
return true
}
return false
}
func (u *Url) Expired() bool {
if u.ExpiresIn > 0 {
if int(time.Now().Sub(u.CreatedTime).Seconds()) > u.ExpiresIn {
return true
}
}
return false
}
func (u *Url) Save() error {
session := GetMgo()
defer session.Close()
_, err := session.DB("ifth").C("url").Upsert(bson.M{"slot": u.Slot}, u)
return err
}