-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
125 lines (109 loc) · 3.02 KB
/
api.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
package main
import (
"encoding/json"
"errors"
"github.com/gin-gonic/gin"
KGS "github.com/jscode017/go_key_generator_service"
"log"
"net/http"
//"strconv"
"time"
)
func DealWithAddUrlRequest(c *gin.Context) { //please use an url that starts with http:// or https://
log.Println("receive add url request")
var requestBody map[string]interface{}
err := json.NewDecoder(c.Request.Body).Decode(&requestBody)
if err != nil {
c.Error(err)
c.JSON(http.StatusInternalServerError, gin.H{"result": false, "error": err.Error()})
return
}
var urlData UrlData
if url, ok := requestBody["url"]; ok {
urlData.OriginalUrl = url.(string)
urlData.ExpireTime = "5000" //TODO: might change it later
} else {
err = errors.New("No original url input")
c.JSON(http.StatusInternalServerError, gin.H{"result": false, "error": err.Error()})
return
}
shortenUrl, err := AddUrl(urlData)
if err != nil {
log.Print(err)
c.Error(err)
c.JSON(http.StatusInternalServerError, gin.H{"result": false, "error": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{"result": true, "shortenurl": shortenUrl})
return
}
func DealWithDeleteUrlRequest(c *gin.Context) {
log.Println("receive delete url request")
var requestBody map[string]interface{}
err := json.NewDecoder(c.Request.Body).Decode(&requestBody)
if err != nil {
c.Error(err)
c.JSON(http.StatusInternalServerError, gin.H{"result": false, "error": err.Error()})
return
}
var shortenUrl string
if url, ok := requestBody["url"]; ok {
shortenUrl = url.(string)
} else {
err = errors.New("No shorten url input")
c.JSON(http.StatusInternalServerError, gin.H{"result": false, "error": err.Error()})
return
}
err = RemoveUrl(shortenUrl)
if err != nil {
c.Error(err)
c.JSON(http.StatusInternalServerError, gin.H{"result": false, "error": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{"result": true, "shortenurl": shortenUrl + " deleted"})
return
}
func AddUrl(urlData UrlData) (string, error) {
//shortenUrl := GenerateKey(urlData.OriginalUrl)
conn, err := NewRedisConn()
if err != nil {
return "", err
}
/*exist, err := CheckKeyExist(conn, shortenUrl)
if err != nil {
return "", err
}
for exist == 1 {
randomNum := RandonIntFromTime()
newShortenUrl := GenerateKey(urlData.OriginalUrl + strconv.Itoa(randomNum)) //keep trying to get an unique shortenUrl
exist, err = CheckKeyExist(conn, shortenUrl)
if err != nil {
return "", err
}
if exist != 1 {
shortenUrl = newShortenUrl
}
}*/
shortenUrl, err := KGS.GetKeyFromRedis(conn) //since redis do not support concurrent operation, so no concurrency problem to worry, no lock needed
if err != nil {
return "", err
}
urlData.ShortenUrl = shortenUrl
urlData.CreationTime = ConvertTimeToStr(time.Now())
err = AddUrlToDb(conn, urlData)
if err != nil {
return "", err
}
return shortenUrl, nil
}
func RemoveUrl(shortenUrl string) error {
conn, err := NewRedisConn()
if err != nil {
return err
}
err = DeleteUrlData(conn, shortenUrl)
if err != nil {
return err
}
return nil
}