-
Notifications
You must be signed in to change notification settings - Fork 6
/
redir.go
115 lines (101 loc) · 2.29 KB
/
redir.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
// Copyright 2021 The golang.design Initiative Authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Originally written by Changkun Ou <changkun.de> at
// changkun.de/s/redir, adopted by Mai Yang <maiyang.me>.
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
)
var (
daemon = flag.Bool("s", false, "run redir service")
fromfile = flag.String("f", "", "import aliases from a YAML file")
operate = flag.String("op", "create", "operators, create/update/delete/fetch")
alias = flag.String("a", "", "alias for a new link")
link = flag.String("l", "", "actual link for the alias, optional for delete/fetch")
)
func usage() {
fmt.Fprintf(os.Stderr,
`usage: redir [-s] [-f <file>] [-op <operator> -a <alias> -l <link>]
options:
`)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
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 -op fetch -a alias fetch alias information
`)
os.Exit(2)
}
func main() {
log.SetPrefix("redir: ")
log.SetFlags(log.Lmsgprefix | log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile)
flag.Usage = usage
flag.Parse()
if len(os.Args) < 2 {
flag.Usage()
return
}
if *daemon {
runServer()
return
}
runCmd()
}
func runServer() {
s := newServer(context.Background())
s.registerHandler()
log.Printf("serving at %s\n", conf.Addr)
if err := http.ListenAndServe(conf.Addr, nil); err != nil {
log.Printf("ListenAndServe %s: %v\n", conf.Addr, err)
}
s.close()
}
func runCmd() {
if *fromfile != "" {
importFile(*fromfile)
return
}
if !op(*operate).valid() {
flag.Usage()
return
}
switch o := op(*operate); o {
case opCreate:
if *alias == "" || *link == "" {
flag.Usage()
return
}
case opUpdate, opDelete, opFetch:
if *alias == "" {
flag.Usage()
return
}
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
done := make(chan bool, 1)
go func() {
err := shortCmd(ctx, op(*operate), *alias, *link)
if err != nil {
log.Println(err)
}
done <- true
}()
select {
case <-ctx.Done():
log.Fatalf("command timeout!")
return
case <-done:
return
}
}