Skip to content

Commit aaece02

Browse files
committed
all: remove /r handler
Fixes #9
1 parent ba81e6e commit aaece02

File tree

18 files changed

+38
-183
lines changed

18 files changed

+38
-183
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Full-featured, self-hosted URL shortener written in Go.
55
| Features | Description |
66
|:-------:|:------------|
77
|**Custom Domain**| Everything is under control with your own domain |
8-
|**Link Shortener**| Support `/s/semantic-name` for short semantic alias and `/r/random-str` for anonymous shortening |
8+
|**Link Shortener**| Support `/s/semantic-name` for short semantic alias for anonymous shortening |
99
|**Go [Vanity Import](https://golang.org/cmd/go/#hdr-Remote_import_paths)**|Redirect `/x/repo-name` to configured VCS and `pkg.go.dev` for API documentation|
1010
|**Access Control**| 1) Private links won't be listed in public index page; 2) Allow link to be accessible only after a configured time point; 3) Allow warn to visitors about external URL redirects (for liability control)|
1111
|**Public Indexes**| Router `/s` provides a list of avaliable short links |
@@ -26,7 +26,7 @@ There are three major pages available in redir.
2626

2727
| Admin Dashboard | Access Control | Public Indexes |
2828
|:---------------:|:--------------:|:--------------:|
29-
| Router: `/s?mode=admin` or<br/>`/r?mode=admin` for management:<br/>![](./assets/admin.png) | Control a link should only be available after a certain time:<br/>![](./assets/wait.png) | Router `/s` or `/r` provides public accessibility to see all public links:<br/>![](./assets/index.png) |
29+
| Router: `/s?mode=admin` for management:<br/>![](./.github/assets/admin.png) | Control a link should only be available after a certain time:<br/>![](./.github/assets/wait.png) | Router `/s` provides public accessibility to see all public links:<br/>![](./.github/assets/index.png) |
3030

3131
## CLI Usage
3232

data/redirconf.yml

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ store: mongodb://redirdb:27017
1010
cors: false
1111
s:
1212
prefix: /s/
13-
r:
14-
enable: true
15-
prefix: /r/
16-
length: 6
1713
x:
1814
enable: true
1915
prefix: /x/

docs/api.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Redir APIs
22

3-
All possible routers: `/s`, `/r`, and `/x`. The `/s` is the most
3+
All possible routers: `/s`, and `/x`. The `/s` is the most
44
complicated router because we are limited to use these prefixes
55
(for many reasons, e.g. deploy to an existing domain that served a lot
66
different routers. The prefix is configurable).
@@ -9,7 +9,7 @@ Thus, all kinds of data, pages, static files are served under this router.
99

1010
## GET /s
1111

12-
The GET request query parameters of `/s` and `/r` are listed as follows:
12+
The GET request query parameters of `/s` are listed as follows:
1313

1414
- `mode`, possible options: `stats`, `index`, `index-pro`
1515
+ `admin`, access admin dashboard
@@ -27,7 +27,7 @@ The GET request query parameters of `/s` and `/r` are listed as follows:
2727

2828
## POST /s
2929

30-
The POST request body of `/s` and `/r` is in the following format:
30+
The POST request body of `/s` is in the following format:
3131

3232
```json
3333
{

internal/cache/cache_test.go

-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func TestLRU(t *testing.T) {
2626

2727
r := &models.Redir{
2828
Alias: "a",
29-
Kind: models.KindShort,
3029
URL: "1",
3130
Private: false,
3231
ValidFrom: time.Now(),
@@ -42,7 +41,6 @@ func TestLRU(t *testing.T) {
4241

4342
l.Put("b", &models.Redir{
4443
Alias: "b",
45-
Kind: models.KindShort,
4644
URL: "2",
4745
Private: false,
4846
ValidFrom: time.Now(),
@@ -60,7 +58,6 @@ func TestLRU(t *testing.T) {
6058

6159
r = &models.Redir{
6260
Alias: "c",
63-
Kind: models.KindShort,
6461
URL: "3",
6562
Private: false,
6663
ValidFrom: time.Now(),
@@ -89,28 +86,24 @@ func TestLRU(t *testing.T) {
8986
tt := time.Now().UTC()
9087
l.Put("a", &models.Redir{
9188
Alias: "a",
92-
Kind: models.KindShort,
9389
URL: "1",
9490
Private: false,
9591
ValidFrom: tt,
9692
})
9793
l.Put("b", &models.Redir{
9894
Alias: "b",
99-
Kind: models.KindShort,
10095
URL: "2",
10196
Private: false,
10297
ValidFrom: tt,
10398
})
10499
l.Put("c", &models.Redir{
105100
Alias: "c",
106-
Kind: models.KindShort,
107101
URL: "3",
108102
Private: false,
109103
ValidFrom: tt,
110104
})
111105
rr := &models.Redir{
112106
Alias: "a",
113-
Kind: models.KindShort,
114107
URL: "2",
115108
Private: false,
116109
ValidFrom: time.Now().UTC(),
@@ -142,7 +135,6 @@ func BenchmarkLRU(b *testing.B) {
142135

143136
r := &models.Redir{
144137
Alias: "a",
145-
Kind: models.KindShort,
146138
URL: "1",
147139
Private: false,
148140
ValidFrom: time.Now(),
@@ -161,7 +153,6 @@ func BenchmarkLRU(b *testing.B) {
161153
k := rands()
162154
v := &models.Redir{
163155
Alias: k,
164-
Kind: models.KindShort,
165156
URL: rands(),
166157
Private: false,
167158
ValidFrom: time.Now(),
@@ -180,7 +171,6 @@ func BenchmarkLRU(b *testing.B) {
180171
k := rands()
181172
v := &models.Redir{
182173
Alias: k,
183-
Kind: models.KindShort,
184174
URL: rands(),
185175
Private: false,
186176
ValidFrom: time.Now(),

internal/config/config.go

-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ type config struct {
3535
S struct {
3636
Prefix string `yaml:"prefix"`
3737
} `yaml:"s"`
38-
R struct {
39-
Enable bool `yaml:"enable"`
40-
Length int `yaml:"length"`
41-
Prefix string `yaml:"prefix"`
42-
} `yaml:"r"`
4338
X struct {
4439
Enable bool `yaml:"enable"`
4540
Prefix string `yaml:"prefix"`

internal/config/config.yml

-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ store: mongodb://localhost:27018
1010
cors: false
1111
s:
1212
prefix: /s/
13-
r:
14-
enable: true
15-
prefix: /r/
16-
length: 6
1713
x:
1814
enable: true
1915
prefix: /x/

internal/db/alias.go

+3-16
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ func (db *Store) StoreAlias(ctx context.Context, r *models.Redir) (err error) {
2222
col := db.cli.Database(dbname).Collection(collink)
2323

2424
opts := options.Update().SetUpsert(true)
25-
filter := bson.M{"alias": r.Alias, "kind": r.Kind}
25+
filter := bson.M{"alias": r.Alias}
2626

2727
now := time.Now().UTC()
2828
ret, err := col.UpdateOne(ctx, filter, bson.M{"$setOnInsert": bson.M{
2929
// do not use r directly, because it can clear object id.
3030
"alias": r.Alias,
31-
"kind": r.Kind,
3231
"url": r.URL,
3332
"private": r.Private,
3433
"trust": r.Trust,
@@ -108,15 +107,14 @@ func (db *Store) FetchAlias(ctx context.Context, a string) (*models.Redir, error
108107
func (db *Store) FetchAliasAll(
109108
ctx context.Context,
110109
public bool,
111-
kind models.AliasKind,
112110
pageSize, pageNum int64,
113111
) ([]models.RedirIndex, int64, error) {
114112
col := db.cli.Database(dbname).Collection(collink)
115113

116114
// public UI does not offer any statistic informations:
117115
// no PV/UV, no actual URLs.
118116
if public {
119-
filter := bson.M{"kind": kind, "private": false}
117+
filter := bson.M{"private": false}
120118
cur, err := col.Find(ctx, filter, []*options.FindOptions{
121119
options.Find().SetLimit(pageSize),
122120
options.Find().SetSkip((pageNum - 1) * pageSize),
@@ -139,22 +137,19 @@ func (db *Store) FetchAliasAll(
139137

140138
// Non-public mode queries PV/UV as additional information,
141139
// and paginates on this. Let's first find the aliases.
142-
filter := bson.M{"kind": kind}
143-
n, err := col.CountDocuments(ctx, filter)
140+
n, err := col.CountDocuments(ctx, bson.M{})
144141
if err != nil {
145142
return nil, 0, err
146143
}
147144

148145
// db.links.aggregate([
149-
// {$match: {kind: 0}},
150146
// {$skip: 20},
151147
// {$limit: 10},
152148
// {'$lookup': {from: 'visit', localField: 'alias', foreignField: 'alias', as: 'visit'}},
153149
// {'$unwind': {path: '$visit', preserveNullAndEmptyArrays: true}},
154150
// {
155151
// $group: {
156152
// _id: {alias: '$alias', ip: '$visit.ip'},
157-
// kind: {$first: '$kind'},
158153
// url: {$first: '$url'},
159154
// private: {$first: '$private'},
160155
// trust: {$first: '$trust'},
@@ -168,7 +163,6 @@ func (db *Store) FetchAliasAll(
168163
// {$group: {
169164
// _id: '$_id.alias',
170165
// alias: {$first: '$_id.alias'},
171-
// kind: {$first: '$kind'},
172166
// url: {$first: '$url'},
173167
// private: {$first: '$private'},
174168
// trust: {$first: '$trust'},
@@ -184,11 +178,6 @@ func (db *Store) FetchAliasAll(
184178
// {$sort : {uv: -1}},
185179
// ])
186180
cur, err := col.Aggregate(ctx, mongo.Pipeline{
187-
bson.D{
188-
primitive.E{Key: "$match", Value: bson.M{
189-
"kind": kind,
190-
}},
191-
},
192181
bson.D{
193182
primitive.E{Key: "$skip", Value: (pageNum - 1) * pageSize},
194183
},
@@ -212,7 +201,6 @@ func (db *Store) FetchAliasAll(
212201
bson.D{
213202
primitive.E{Key: "$group", Value: bson.M{
214203
"_id": bson.M{"alias": "$alias", "ip": "$visit.ip"},
215-
"kind": bson.M{"$first": "$kind"},
216204
"url": bson.M{"$first": "$url"},
217205
"private": bson.M{"$first": "$private"},
218206
"trust": bson.M{"$first": "$trust"},
@@ -227,7 +215,6 @@ func (db *Store) FetchAliasAll(
227215
primitive.E{Key: "$group", Value: bson.M{
228216
"_id": "$_id.alias",
229217
"alias": bson.M{"$first": "$_id.alias"},
230-
"kind": bson.M{"$first": "$kind"},
231218
"url": bson.M{"$first": "$url"},
232219
"private": bson.M{"$first": "$private"},
233220
"trust": bson.M{"$first": "$trust"},

internal/db/alias_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ func prepare(ctx context.Context, t *testing.T) *db.Store {
2323

2424
err = s.StoreAlias(ctx, &models.Redir{
2525
Alias: kalias,
26-
Kind: models.KindShort,
2726
URL: "link",
2827
Private: false,
2928
Trust: false,
@@ -82,7 +81,7 @@ func TestFetchAliasAll(t *testing.T) {
8281
if err != nil {
8382
t.Skip("cannot connect to data store")
8483
}
85-
rs, total, err := s.FetchAliasAll(ctx, true, models.KindShort, 20, 1)
84+
rs, total, err := s.FetchAliasAll(ctx, true, 20, 1)
8685
if err != nil || len(rs) == 0 || total == 0 {
8786
t.Fatalf("fetch failed: %v, %v, %v", err, rs, total)
8887
}
@@ -107,7 +106,7 @@ func BenchmarkFetchAliasAll(b *testing.B) {
107106
b.ReportAllocs()
108107
b.ResetTimer()
109108
for i := 0; i < b.N; i++ {
110-
rs, total, err := s.FetchAliasAll(ctx, false, models.KindShort, 100, 1)
109+
rs, total, err := s.FetchAliasAll(ctx, false, 100, 1)
111110
if err != nil || len(rs) == 0 || total == 0 {
112111
b.Fatalf("fetch failed: %v, %v, %v", err, rs, total)
113112
}

internal/db/stats.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@ import (
2020
func (db *Store) StatReferer(
2121
ctx context.Context,
2222
a string,
23-
k models.AliasKind,
2423
start, end time.Time,
2524
) ([]models.RefStat, error) {
2625

2726
col := db.cli.Database(dbname).Collection(collink)
2827
opts := options.Aggregate().SetMaxTime(10 * time.Second)
2928
cur, err := col.Aggregate(ctx, mongo.Pipeline{
3029
bson.D{
31-
primitive.E{Key: "$match", Value: bson.M{
32-
"kind": k, "alias": a,
33-
}},
30+
primitive.E{Key: "$match", Value: bson.M{"alias": a}},
3431
},
3532
bson.D{
3633
primitive.E{Key: "$lookup", Value: bson.M{
@@ -98,17 +95,14 @@ func (db *Store) StatReferer(
9895
func (db *Store) StatUA(
9996
ctx context.Context,
10097
a string,
101-
k models.AliasKind,
10298
start, end time.Time,
10399
) ([]models.UAStat, error) {
104100

105101
col := db.cli.Database(dbname).Collection(collink)
106102
opts := options.Aggregate().SetMaxTime(10 * time.Second)
107103
cur, err := col.Aggregate(ctx, mongo.Pipeline{
108104
bson.D{
109-
primitive.E{Key: "$match", Value: bson.M{
110-
"kind": k, "alias": a,
111-
}},
105+
primitive.E{Key: "$match", Value: bson.M{"alias": a}},
112106
},
113107
bson.D{
114108
primitive.E{Key: "$lookup", Value: bson.M{
@@ -180,13 +174,12 @@ func (db *Store) StatUA(
180174
func (db *Store) StatVisitHist(
181175
ctx context.Context,
182176
a string,
183-
k models.AliasKind,
184177
start, end time.Time,
185178
) ([]models.TimeHist, error) {
186179

187180
// Raw query
188181
// db.links.aggregate([
189-
// {$match: {kind: 0, alias: 'changkun'}},
182+
// {$match: {alias: 'changkun'}},
190183
// {'$lookup': {
191184
// from: 'visit', localField: 'alias',
192185
// foreignField: 'alias', as: 'visit'},
@@ -241,9 +234,7 @@ func (db *Store) StatVisitHist(
241234
opts := options.Aggregate().SetMaxTime(10 * time.Second)
242235
cur, err := col.Aggregate(ctx, mongo.Pipeline{
243236
bson.D{primitive.E{
244-
Key: "$match", Value: bson.M{
245-
"kind": k, "alias": a,
246-
},
237+
Key: "$match", Value: bson.M{"alias": a},
247238
}},
248239
bson.D{primitive.E{
249240
Key: "$lookup", Value: bson.M{

internal/models/models.go

-12
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,11 @@ import (
88
"time"
99
)
1010

11-
// AliasKind represents a kind of alias
12-
type AliasKind int
13-
14-
// All kinds of aliases
15-
const (
16-
KindShort AliasKind = iota
17-
KindRandom
18-
)
19-
2011
// Redir is the core redir model, it records a kind of alias
2112
// and its correlated link.
2213
type Redir struct {
2314
ID string `json:"-" yaml:"-" bson:"_id"`
2415
Alias string `json:"alias" yaml:"alias" bson:"alias"`
25-
Kind AliasKind `json:"kind" yaml:"-" bson:"kind"`
2616
URL string `json:"url" yaml:"url" bson:"url"`
2717
Private bool `json:"private" yaml:"private" bson:"private"`
2818
Trust bool `json:"trust" yaml:"trust" bson:"trust"`
@@ -38,7 +28,6 @@ type Redir struct {
3828
type RedirIndex struct {
3929
ID string `json:"-" yaml:"-" bson:"_id"`
4030
Alias string `json:"alias" yaml:"alias" bson:"alias"`
41-
Kind AliasKind `json:"kind" yaml:"-" bson:"kind"`
4231
URL string `json:"url" yaml:"url" bson:"url"`
4332
Private bool `json:"private" yaml:"private" bson:"private"`
4433
Trust bool `json:"trust" yaml:"trust" bson:"trust"`
@@ -55,7 +44,6 @@ type RedirIndex struct {
5544
type Visit struct {
5645
VisitorID string `json:"visitor_id" bson:"visitor_id"`
5746
Alias string `json:"alias" bson:"alias"`
58-
Kind AliasKind `json:"kind" bson:"kind"`
5947
IP string `json:"ip" bson:"ip"`
6048
UA string `json:"ua" bson:"ua"`
6149
Referer string `json:"referer" bson:"referer"`

internal/short/cmd.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,7 @@ func Edit(ctx context.Context, s *db.Store, operate Op, a string, r *models.Redi
6060
}
6161
log.Printf("alias %v has been created:\n", r.Alias)
6262

63-
var prefix string
64-
switch r.Kind {
65-
case models.KindShort:
66-
prefix = config.Conf.S.Prefix
67-
case models.KindRandom:
68-
prefix = config.Conf.R.Prefix
69-
}
63+
prefix := config.Conf.S.Prefix
7064
log.Printf("%s%s%s\n", config.Conf.Host, prefix, r.Alias)
7165
case OpUpdate:
7266
var rr *models.Redir
@@ -89,9 +83,6 @@ func Edit(ctx context.Context, s *db.Store, operate Op, a string, r *models.Redi
8983
if r.ValidFrom == tt {
9084
r.ValidFrom = rr.ValidFrom
9185
}
92-
if rr.Kind != r.Kind {
93-
r.Kind = rr.Kind
94-
}
9586
r.ID = rr.ID
9687
}
9788

0 commit comments

Comments
 (0)