Skip to content

Commit 4af12fc

Browse files
committed
all: support /r
For #4
1 parent 6433d09 commit 4af12fc

File tree

15 files changed

+218
-80
lines changed

15 files changed

+218
-80
lines changed

README.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ if exist. The website itself will redirect the request to [pkg.go.dev](https://p
2222
There is a reserved ping router for debugging purpose `/x/.ping` which will
2323
give you a pong.
2424

25-
### 2. Redirect `golang.design/s/alias`
25+
### 2. Redirect `golang.design/s/alias` and `golang.design/r/randstr`
2626

2727
The served alias can be allocated by [golang.design](https://golang.design/) members.
2828
The current approach is to use `redir` command on the [golang.design](https://golang.design/)
2929
server. Here is the overview of its usage:
3030

3131
```
32+
$ redir
3233
usage: redir [-s] [-f <file>] [-op <operator> -a <alias> -l <link>]
3334
options:
3435
-a string
@@ -42,8 +43,9 @@ options:
4243
-s run redir service
4344
example:
4445
redir -s run the redir service
45-
redir -f ./import.yml import aliases from a file
46+
redir -f ./import.yml import aliases from a file
4647
redir -a alias -l link allocate new short link if possible
48+
redir -l link allocate a random alias for the given link if possible
4749
redir -op fetch -a alias fetch alias information
4850
```
4951

@@ -52,32 +54,42 @@ The command will talk to the Redis data store and issue a new allocated alias.
5254
For instance, the following command:
5355

5456
```
55-
redir -a changkun -l https://changkun.de
57+
$ redir -a changkun -l https://changkun.de
58+
https://golang.design/s/changkun
5659
```
5760

5861
creates a new alias under [golang.design/s/changkun](https://golang.design/s/changkun).
5962

63+
If the `-a` is not provided, then redir command will generate a random string as an alias, but the link can only be accessed under `/r/alias`. For instance:
64+
65+
```
66+
$ redir -l https://changkun.de
67+
https://golang.design/r/qFlKSP
68+
```
69+
70+
creates a new alias under [golang.design/r/qFlKSP](https://golang.design/r/qFlKSP).
71+
6072
Import from a YAML file is also possible, for instance:
6173

6274
```
63-
redir -f import.yml
75+
$ redir -f import.yml
6476
```
6577

6678
The aliases are either imported as a new alias or updated for an existing alias.
6779

68-
Moreover, it is possible to visit [`/s`](https://golang.design/s) directly listing all exist aliases under [golang.design](https://golang.design/).
80+
Moreover, it is possible to visit [`/s`](https://golang.design/s) or [`/r`](https://golang.design/r) directly listing all exist aliases under [golang.design](https://golang.design/).
6981

7082
## Build
7183

7284
`Makefile` defines different ways to build the service:
7385

7486
```bash
75-
make # build native binary
76-
make run # assume your local redis is running
77-
make build # build docker images
78-
make up # run via docker-compose
79-
make down # remove compose stuff
80-
make clean # cleanup
87+
$ make # build native binary
88+
$ make run # assume your local redis is running
89+
$ make build # build docker images
90+
$ make up # run via docker-compose
91+
$ make down # remove compose stuff
92+
$ make clean # cleanup
8193
```
8294

8395
## Troubleshooting

config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ type config struct {
3131
S struct {
3232
Prefix string `yaml:"prefix"`
3333
} `yaml:"s"`
34+
R struct {
35+
Length int `yaml:"length"`
36+
Prefix string `yaml:"prefix"`
37+
} `yaml:"r"`
3438
X struct {
3539
Prefix string `yaml:"prefix"`
3640
VCS string `yaml:"vcs"`

config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ backup_dir: ./data/backup
1212
log: "golang.design/redir: "
1313
s:
1414
prefix: /s/
15+
r:
16+
length: 6
17+
prefix: /r/
1518
x:
1619
prefix: /x/
1720
vcs: git

data/container.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# by a MIT license that can be found in the LICENSE file.
44

55
---
6-
title: The golang.design Initiative
6+
title: "The golang.design Initiative"
77
host: https://golang.design
88
addr: :8080
99
store: redis://redis:6379/9
@@ -12,18 +12,13 @@ backup_dir: ./data/backup
1212
log: "golang.design/redir: "
1313
s:
1414
prefix: /s/
15+
r:
16+
length: 6
17+
prefix: /r/
1518
x:
1619
prefix: /x/
1720
vcs: git
1821
import_path: golang.design/x/*
1922
repo_path: https://github.com/golang-design/*
2023
godoc_host: https://pkg.go.dev/
21-
google_analytics: |
22-
<!-- Global site tag (gtag.js) - Google Analytics -->
23-
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-80889616-4"></script>
24-
<script>
25-
window.dataLayer = window.dataLayer || [];
26-
function gtag(){dataLayer.push(arguments);}
27-
gtag('js', new Date());
28-
gtag('config', 'UA-80889616-4');
29-
</script>
24+
google_analytics: UA-80889616-4

db.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ var (
2323
errExistedAlias = errors.New("alias is existed")
2424
)
2525

26+
type aliasKind int
27+
28+
const (
29+
kindShort aliasKind = iota
30+
kindRandom
31+
)
32+
2633
// arecord indicates an alias record that stores an short alias
2734
// in data store with statistics regarding its UVs and PVs.
2835
type arecord struct {
2936
Alias string `json:"alias"`
37+
Kind aliasKind `json:"kind"`
3038
URL string `json:"url"`
3139
UV uint64 `json:"uv"`
3240
PV uint64 `json:"pv"`
@@ -69,9 +77,9 @@ func (s *store) Close() (err error) {
6977
}
7078

7179
// StoreAlias stores a given short alias with the given link if not exists
72-
func (s *store) StoreAlias(ctx context.Context, a, l string) (err error) {
80+
func (s *store) StoreAlias(ctx context.Context, a, l string, kind aliasKind) (err error) {
7381
b, err := json.Marshal(&arecord{
74-
URL: l, Alias: a, PV: 0, UV: 0,
82+
URL: l, Kind: kind, Alias: a, PV: 0, UV: 0,
7583
CreatedAt: time.Now().UTC(),
7684
UpdatedAt: time.Now().UTC(),
7785
})

db_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func prepare(ctx context.Context, t *testing.T) *store {
2020
t.Fatalf("cannot connect to data store")
2121
}
2222

23-
err = s.StoreAlias(ctx, kalias, "link")
23+
err = s.StoreAlias(ctx, kalias, "link", kindShort)
2424
if err != nil {
2525
t.Fatalf("cannot store alias to data store, err: %v\n", err)
2626
}

handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ func (s *server) registerHandler() {
7272
})
7373

7474
// short redirector
75-
http.HandleFunc(conf.S.Prefix, s.sHandler)
75+
http.HandleFunc(conf.S.Prefix, s.shortHandler(kindShort))
76+
http.HandleFunc(conf.R.Prefix, s.shortHandler(kindRandom))
7677
// repo redirector
7778
http.Handle(conf.X.Prefix, s.xHandler(conf.X.VCS, conf.X.ImportPath, conf.X.RepoPath))
7879
}

import.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
---
2-
changkun: https://changkun.de/
2+
short:
3+
changkun: https://changkun.de
4+
random:
5+
- https://to.what.ever

public/stats.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
{{ .GoogleAnalytics }}
4+
<!-- Global site tag (gtag.js) - Google Analytics -->
5+
<script async src="https://www.googletagmanager.com/gtag/js?id={{ .GoogleAnalytics }}"></script>
6+
<script>
7+
window.dataLayer = window.dataLayer || [];
8+
function gtag(){dataLayer.push(arguments);}
9+
gtag('js', new Date());
10+
gtag('config', '{{ .GoogleAnalytics }}');
11+
</script>
512
<meta charset="UTF-8">
613
<meta name="viewport" content="width=device-width, initial-scale=1.0">
714
<title>{{.Title}}</title>
@@ -67,7 +74,7 @@ <h5><a href="https://github.com/golang-design/redir">An alias request redirector
6774
{{range .Records}}
6875
<tr>
6976
<td>{{ .PV }}/{{ .UV }}</td>
70-
<td><a href="{{ $.Host }}/s/{{ .Alias }}">{{ $.Host }}/s/{{ .Alias }}</a></td>
77+
<td><a href="{{ $.Host }}{{ $.Prefix }}{{ .Alias }}">{{ $.Host }}{{ $.Prefix }}{{ .Alias }}</a></td>
7178
</tr>
7279
{{end}}
7380
</table>

public/x.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</head><body>
1515
<h1>{{.ImportRoot}}{{.Suffix}}</h1>
1616
<ul>
17-
<li>Source: <a href="https://pkg.go.dev/{{.ImportRoot}}{{.Suffix}}">pkg.go.dev/{{.ImportRoot}}{{.Suffix}}</a></li>
17+
<li>Source: <a href="{{.VCSRoot}}">{{.VCSRoot}}</a></li>
1818
<li>Doc: <a href="https://pkg.go.dev/{{.ImportRoot}}{{.Suffix}}">pkg.go.dev/{{.ImportRoot}}{{.Suffix}}</a></li>
1919
</ul>
2020
</body></html>

0 commit comments

Comments
 (0)