-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmd.go
123 lines (110 loc) 路 2.74 KB
/
cmd.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
// Copyright 2021 Changkun Ou. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package short
import (
"context"
"errors"
"fmt"
"log"
"regexp"
"time"
"changkun.de/x/redir/internal/config"
"changkun.de/x/redir/internal/db"
"changkun.de/x/redir/internal/models"
)
var (
Validity = regexp.MustCompile(`^[\w\-][\w\-. \/]+$`)
ErrInvalidAlias = errors.New("invalid alias pattern")
)
// Cmd processes the given alias and link with a specified op.
func Cmd(ctx context.Context, operate Op, r *models.Redir) (err error) {
s, err := db.NewStore(ctx, config.Conf.Store)
if err != nil {
err = fmt.Errorf("cannot create a new alias: %w", err)
return
}
defer s.Close()
defer func() {
if err != nil {
err = fmt.Errorf("cannot %v alias to data store: %w", operate, err)
}
}()
err = Edit(ctx, s, operate, r.Alias, r)
return
}
// Edit edits the datastore for a given alias in a given operation.
// if the operation is create, then the alias is not necessary.
// if the operation is update/fetch/delete, then the alias is used to
// match the existing aliases, meaning that alias can be changed.
func Edit(ctx context.Context, s *db.Store, operate Op, a string, r *models.Redir) (err error) {
switch operate {
case OpCreate:
if !Validity.MatchString(r.Alias) {
err = ErrInvalidAlias
return
}
err = s.StoreAlias(ctx, r)
if err != nil {
return
}
log.Printf("alias %v has been created:\n", r.Alias)
var prefix string
switch r.Kind {
case models.KindShort:
prefix = config.Conf.S.Prefix
case models.KindRandom:
prefix = config.Conf.R.Prefix
}
log.Printf("%s%s%s\n", config.Conf.Host, prefix, r.Alias)
case OpUpdate:
var rr *models.Redir
// fetch the old values if possible, we don't care
// if here returns an error.
//
// Note that this is not atomic, meaning that we might run
// into concurrent inconsistent issue. But for small scale
// use, it is fine for now.
rr, err = s.FetchAlias(ctx, a)
if err != nil {
err = nil
} else {
// use old values if not presents
if r.URL == "" {
r.URL = rr.URL
}
tt := time.Time{}
if r.ValidFrom == tt {
r.ValidFrom = rr.ValidFrom
}
if rr.Kind != r.Kind {
r.Kind = rr.Kind
}
r.ID = rr.ID
}
if r.ID == "" {
err = fmt.Errorf("cannot find alias %s for update", a)
return
}
// do update
err = s.UpdateAlias(ctx, r)
if err != nil {
return
}
log.Printf("alias %v has been updated.\n", a)
case OpDelete:
err = s.DeleteAlias(ctx, a)
if err != nil {
return
}
log.Printf("alias %v has been deleted.\n", a)
case OpFetch:
var r *models.Redir
r, err = s.FetchAlias(ctx, a)
if err != nil {
return
}
log.Println(r.URL)
}
return
}