Skip to content

Commit

Permalink
all: support /r
Browse files Browse the repository at this point in the history
For #4
  • Loading branch information
changkun committed Dec 16, 2020
1 parent 6433d09 commit 4af12fc
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 80 deletions.
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ if exist. The website itself will redirect the request to [pkg.go.dev](https://p
There is a reserved ping router for debugging purpose `/x/.ping` which will
give you a pong.

### 2. Redirect `golang.design/s/alias`
### 2. Redirect `golang.design/s/alias` and `golang.design/r/randstr`

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

```
$ redir
usage: redir [-s] [-f <file>] [-op <operator> -a <alias> -l <link>]
options:
-a string
Expand All @@ -42,8 +43,9 @@ options:
-s run redir service
example:
redir -s run the redir service
redir -f ./import.yml import aliases from a file
redir -f ./import.yml import aliases from a file
redir -a alias -l link allocate new short link if possible
redir -l link allocate a random alias for the given link if possible
redir -op fetch -a alias fetch alias information
```

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

```
redir -a changkun -l https://changkun.de
$ redir -a changkun -l https://changkun.de
https://golang.design/s/changkun
```

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

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:

```
$ redir -l https://changkun.de
https://golang.design/r/qFlKSP
```

creates a new alias under [golang.design/r/qFlKSP](https://golang.design/r/qFlKSP).

Import from a YAML file is also possible, for instance:

```
redir -f import.yml
$ redir -f import.yml
```

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

Moreover, it is possible to visit [`/s`](https://golang.design/s) directly listing all exist aliases under [golang.design](https://golang.design/).
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/).

## Build

`Makefile` defines different ways to build the service:

```bash
make # build native binary
make run # assume your local redis is running
make build # build docker images
make up # run via docker-compose
make down # remove compose stuff
make clean # cleanup
$ make # build native binary
$ make run # assume your local redis is running
$ make build # build docker images
$ make up # run via docker-compose
$ make down # remove compose stuff
$ make clean # cleanup
```

## Troubleshooting
Expand Down
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type config struct {
S struct {
Prefix string `yaml:"prefix"`
} `yaml:"s"`
R struct {
Length int `yaml:"length"`
Prefix string `yaml:"prefix"`
} `yaml:"r"`
X struct {
Prefix string `yaml:"prefix"`
VCS string `yaml:"vcs"`
Expand Down
3 changes: 3 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ backup_dir: ./data/backup
log: "golang.design/redir: "
s:
prefix: /s/
r:
length: 6
prefix: /r/
x:
prefix: /x/
vcs: git
Expand Down
15 changes: 5 additions & 10 deletions data/container.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# by a MIT license that can be found in the LICENSE file.

---
title: The golang.design Initiative
title: "The golang.design Initiative"
host: https://golang.design
addr: :8080
store: redis://redis:6379/9
Expand All @@ -12,18 +12,13 @@ backup_dir: ./data/backup
log: "golang.design/redir: "
s:
prefix: /s/
r:
length: 6
prefix: /r/
x:
prefix: /x/
vcs: git
import_path: golang.design/x/*
repo_path: https://github.com/golang-design/*
godoc_host: https://pkg.go.dev/
google_analytics: |
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-80889616-4"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-80889616-4');
</script>
google_analytics: UA-80889616-4
12 changes: 10 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ var (
errExistedAlias = errors.New("alias is existed")
)

type aliasKind int

const (
kindShort aliasKind = iota
kindRandom
)

// arecord indicates an alias record that stores an short alias
// in data store with statistics regarding its UVs and PVs.
type arecord struct {
Alias string `json:"alias"`
Kind aliasKind `json:"kind"`
URL string `json:"url"`
UV uint64 `json:"uv"`
PV uint64 `json:"pv"`
Expand Down Expand Up @@ -69,9 +77,9 @@ func (s *store) Close() (err error) {
}

// StoreAlias stores a given short alias with the given link if not exists
func (s *store) StoreAlias(ctx context.Context, a, l string) (err error) {
func (s *store) StoreAlias(ctx context.Context, a, l string, kind aliasKind) (err error) {
b, err := json.Marshal(&arecord{
URL: l, Alias: a, PV: 0, UV: 0,
URL: l, Kind: kind, Alias: a, PV: 0, UV: 0,
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
})
Expand Down
2 changes: 1 addition & 1 deletion db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func prepare(ctx context.Context, t *testing.T) *store {
t.Fatalf("cannot connect to data store")
}

err = s.StoreAlias(ctx, kalias, "link")
err = s.StoreAlias(ctx, kalias, "link", kindShort)
if err != nil {
t.Fatalf("cannot store alias to data store, err: %v\n", err)
}
Expand Down
3 changes: 2 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func (s *server) registerHandler() {
})

// short redirector
http.HandleFunc(conf.S.Prefix, s.sHandler)
http.HandleFunc(conf.S.Prefix, s.shortHandler(kindShort))
http.HandleFunc(conf.R.Prefix, s.shortHandler(kindRandom))
// repo redirector
http.Handle(conf.X.Prefix, s.xHandler(conf.X.VCS, conf.X.ImportPath, conf.X.RepoPath))
}
Expand Down
5 changes: 4 additions & 1 deletion import.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
---
changkun: https://changkun.de/
short:
changkun: https://changkun.de
random:
- https://to.what.ever
11 changes: 9 additions & 2 deletions public/stats.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
{{ .GoogleAnalytics }}
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ .GoogleAnalytics }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ .GoogleAnalytics }}');
</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}}</title>
Expand Down Expand Up @@ -67,7 +74,7 @@ <h5><a href="https://github.com/golang-design/redir">An alias request redirector
{{range .Records}}
<tr>
<td>{{ .PV }}/{{ .UV }}</td>
<td><a href="{{ $.Host }}/s/{{ .Alias }}">{{ $.Host }}/s/{{ .Alias }}</a></td>
<td><a href="{{ $.Host }}{{ $.Prefix }}{{ .Alias }}">{{ $.Host }}{{ $.Prefix }}{{ .Alias }}</a></td>
</tr>
{{end}}
</table>
Expand Down
2 changes: 1 addition & 1 deletion public/x.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</head><body>
<h1>{{.ImportRoot}}{{.Suffix}}</h1>
<ul>
<li>Source: <a href="https://pkg.go.dev/{{.ImportRoot}}{{.Suffix}}">pkg.go.dev/{{.ImportRoot}}{{.Suffix}}</a></li>
<li>Source: <a href="{{.VCSRoot}}">{{.VCSRoot}}</a></li>
<li>Doc: <a href="https://pkg.go.dev/{{.ImportRoot}}{{.Suffix}}">pkg.go.dev/{{.ImportRoot}}{{.Suffix}}</a></li>
</ul>
</body></html>
7 changes: 4 additions & 3 deletions redir.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ examples:
redir -s run the redir service
redir -f ./import.yml import aliases from a file
redir -a alias -l link allocate new short link if possible
redir -l link allocate a random alias for the given link if possible
redir -op fetch -a alias fetch alias information
`)
os.Exit(2)
Expand Down Expand Up @@ -77,12 +78,12 @@ func runCmd() {
}

switch o := op(*operate); o {
case opCreate, opUpdate:
if *alias == "" || *link == "" {
case opCreate:
if *link == "" {
flag.Usage()
return
}
case opDelete, opFetch:
case opUpdate, opDelete, opFetch:
if *alias == "" {
flag.Usage()
return
Expand Down
Loading

0 comments on commit 4af12fc

Please sign in to comment.