-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
109 lines (100 loc) · 2.96 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
package main
import (
"crypto/sha1"
"database/sql"
"encoding/base64"
"errors"
"io"
"net/http"
"net/url"
"os"
"regexp"
"github.com/thearavind/go-tinyURL/models"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
var db *sql.DB
var defaultRedirectURL string
var router *gin.Engine
func init() {
defaultRedirectURL = os.Getenv("DEFAULT_URL")
db = models.ConnectToDb()
router = gin.Default()
router.POST("/short", makeTinyURL)
router.GET("/:tinyURL", findTinyURL)
router.Use(static.Serve("/", static.LocalFile("./dist", true)))
}
func main() {
router.Run(":3000")
}
func makeTinyURL(c *gin.Context) {
var tiny models.URL
var urlSearchResult models.URL
c.BindJSON(&tiny)
_, err := url.ParseRequestURI(tiny.Address)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest,
"message": "URL to shorten is not in correct format"})
} else {
h := sha1.New()
io.WriteString(h, tiny.Address)
tiny.Hash = base64.URLEncoding.EncodeToString(h.Sum(nil))[:6]
row, err := db.Query(`SELECT * FROM short_url where hash=$1`, tiny.Hash)
defer row.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError,
"message": err})
} else {
var next = row.Next()
if !next {
inserHashToDB(tiny, c)
} else {
err := row.Scan(&urlSearchResult.ID, &urlSearchResult.Hash, &urlSearchResult.Address, &urlSearchResult.Clicks)
switch err {
case sql.ErrNoRows:
inserHashToDB(tiny, c)
case nil:
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "tiny_url": urlSearchResult})
default:
c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError, "message": err})
}
}
}
}
}
func findTinyURL(c *gin.Context) {
var foundURL models.URL
hash := c.Param("tinyURL")
if regexp.MustCompile(`^[a-zA-Z0-9+]{6,6}$`).MatchString(hash) {
row, err := db.Query(`SELECT * FROM short_url where hash=$1`, hash)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError,
"message": err})
} else {
switch err := row.Scan(&foundURL.ID, &foundURL.Hash, &foundURL.Address, &foundURL.Clicks); err {
case sql.ErrNoRows:
c.Redirect(302, defaultRedirectURL)
case nil:
_, _ = db.Query(`UPDATE short_url SET clicks = $1 WHERE hash = $2;`, foundURL.Clicks+1, foundURL.Hash)
c.Redirect(302, foundURL.Address)
default:
c.Redirect(302, defaultRedirectURL)
}
}
} else {
c.Redirect(302, defaultRedirectURL)
}
}
func redirectHandler(c *gin.Context) {
c.Redirect(302, defaultRedirectURL)
}
func inserHashToDB(tinyObj models.URL, c *gin.Context) {
_, err := db.Query(`INSERT INTO short_url(hash, url, clicks)VALUES($1, $2, 0)`,
tinyObj.Hash, tinyObj.Address)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": http.StatusInternalServerError,
"message": err})
} else {
c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "tiny_url": tinyObj})
}
}