-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhandlers.go
122 lines (103 loc) · 2.73 KB
/
handlers.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
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
var service Service
// NewRouter instantiates a new gin Router
func NewRouter(_service Service) *gin.Engine {
service = _service
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/", landing)
router.POST("/new", newEntry)
router.GET("/r/:shortId", defaultRedirect)
router.GET("/c/:shortId", clientSideRedirect)
router.GET("/s/:shortId", serverSideRedirect)
router.GET("/stats/:shortId", showStats)
router.StaticFile("/img/paytm.png", "./static/paytm.png")
router.NoRoute(noRouteHandler)
return router
}
func landing(c *gin.Context) {
if len(config.AuthToken) != 0 {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
"message": "Authentication required to access dashboard",
})
return
}
c.HTML(http.StatusOK, "landing.tmpl", gin.H{
"newform": true,
})
}
func showStats(c *gin.Context) {
if len(config.AuthToken) != 0 {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
"message": "Authentication required to access dashboard",
})
return
}
link, err := service.getLinkForShortID(c.Param("shortId"))
c.HTML(http.StatusOK, "landing.tmpl", gin.H{
"newform": false,
"link": link,
"err": err,
})
}
func newEntry(c *gin.Context) {
if len(config.AuthToken) != 0 && config.AuthToken != c.PostForm("auth_token") {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
"message": "Unauthorized",
})
return
}
link := service.newLink(c.PostForm("url"))
if len(config.AuthToken) == 0 {
c.Redirect(http.StatusMovedPermanently, "/stats/"+link.ShortID)
return
}
c.JSON(http.StatusOK, gin.H{
"link": link,
"success": true,
})
}
func defaultRedirect(c *gin.Context) {
switch config.RedirectMethod {
case "server-side":
serverSideRedirect(c)
case "client-side":
fallthrough
default:
clientSideRedirect(c)
}
}
func clientSideRedirect(c *gin.Context) {
// redirectURL := "https://google.com/search?q=" + c.Param("shortId")
link, err := service.getLinkForShortID(c.Param("shortId"))
// if err == mgo.ErrNotFound {
if err != nil {
noRouteHandler(c)
return
}
service.incrementLinkCounter(link)
c.HTML(http.StatusOK, "client-side-redirect.tmpl", gin.H{
"REDIRECT_URL": link.URL,
})
}
func serverSideRedirect(c *gin.Context) {
// redirectURL := "https://google.com/search?q=" + c.Param("shortId")
link, err := service.getLinkForShortID(c.Param("shortId"))
// if err == mgo.ErrNotFound {
if err != nil {
noRouteHandler(c)
return
}
service.incrementLinkCounter(link)
c.Redirect(http.StatusMovedPermanently, link.URL)
}
func noRouteHandler(c *gin.Context) {
c.HTML(http.StatusNotFound, "404.tmpl", nil)
}
func gatewayErrHandler(c *gin.Context) {
c.HTML(http.StatusBadGateway, "500.tmpl", nil)
}