-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
141 lines (119 loc) · 3.02 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v7"
"io/ioutil"
"net/http"
"time"
)
type Response struct {
Code int
Message string
LongUrl string
ShortUrl string
}
type Bitly struct {
CreatedAt string `json:"created_at"`
ID string `json:"id"`
Link string `json:"link"`
CustomBitlinks []interface{} `json:"custom_bitlinks"`
LongURL string `json:"long_url"`
Archived bool `json:"archived"`
Tags []interface{} `json:"tags"`
Deeplinks []interface{} `json:"deeplinks"`
References struct {
Group string `json:"group"`
} `json:"references"`
}
// api文档:https://dev.bitly.com/v4_documentation.html
const endPoint string = "https://api-ssl.bitly.com/v4/shorten"
const defaultToken string = ""
const defaultPort int = 8001
func main() {
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
token := flag.String("token", "", "Bitly api token")
port := flag.Int("port", defaultPort, "服务端口")
cache := flag.Int("cache", 0, "是否使用 redis 缓存")
flag.Parse()
if *token == "" {
panic("Bitly api token is required.")
}
router.POST("/", func(c *gin.Context) {
res := &Response{
Code: 0,
Message: "",
LongUrl: "",
ShortUrl: "",
}
longUrl := c.PostForm("longUrl")
if longUrl == "" {
res.Message = "longUrl为空"
c.JSON(400, *res)
return
}
_longUrl, _ := base64.StdEncoding.DecodeString(longUrl)
longUrl = string(_longUrl)
res.LongUrl = longUrl
// 先查缓存
redisClient := initRedis()
if 1 == *cache {
_shortUrl, err := redisClient.Get(longUrl).Result()
if err == nil && _shortUrl != "" {
res.Code = 1
res.ShortUrl = _shortUrl
c.JSON(200, res)
return
}
}
contentType := "application/json"
postField := fmt.Sprintf("{\"long_url\":\"%s\"}", longUrl)
req, err := http.NewRequest("POST", endPoint, bytes.NewBuffer([]byte(postField)))
if err != nil {
res.Message = err.Error()
c.JSON(500, *res)
return
}
req.Header.Add("Content-Type", contentType)
req.Header.Add("Accept", contentType)
req.Header.Add("Host", "api-ssl.bitly.com")
req.Header.Add("Authorization", "Bearer "+*token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
res.Message = err.Error()
c.JSON(500, *res)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var bitly Bitly
err = json.Unmarshal(body, &bitly)
// 写缓存
res.ShortUrl = bitly.Link
if 1 == *cache {
ttl, err := time.ParseDuration("300s")
if err != nil {
res.Message = err.Error()
c.JSON(500, *res)
return
}
redisClient.Set(res.LongUrl, res.ShortUrl, ttl)
}
res.Code = 1
c.JSON(200, res)
})
router.Run(fmt.Sprintf(":%d", *port))
}
func initRedis() *redis.Client {
return redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
}