This repository has been archived by the owner on Jun 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (121 loc) · 2.81 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
142
143
144
145
146
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"github.com/gin-gonic/gin"
gonanoid "github.com/matoous/go-nanoid/v2"
"github.com/sirupsen/logrus"
)
var DATA_ARRAY map[string]string
type Shorten struct {
LongURL string `json:"longUrl"`
ShortURL string `json:"shortUrl"`
}
type Error struct {
ErrCode int `json:"ErrCode"`
ErrMessage string `json:"ErrMessage"`
}
var noURL = Error{
ErrCode: 400,
ErrMessage: "please enter a url to shorten",
}
var invalidURL = Error{
ErrCode: 400,
ErrMessage: "invalid URL",
}
func genID(length int) string {
id, err := gonanoid.New(length)
if err != nil {
logrus.Errorf("error while creating new id: %v", err)
os.Exit(1)
}
return id
}
func isUrl(str string) bool {
u, err := url.Parse(str)
if err != nil {
logrus.Errorf("error parsing url: %v", err)
os.Exit(1)
}
return u.Scheme != "" && u.Host != ""
}
func dataProccess(inputURL string) (string, error) {
idLength := 3
if isUrl(inputURL) {
for {
currID := genID(idLength)
if _, exists := DATA_ARRAY[currID]; !exists {
DATA_ARRAY[currID] = inputURL
result, err := json.Marshal(DATA_ARRAY)
if err != nil {
logrus.Errorf("error while marshalling json: %v", err)
return "", fmt.Errorf("error marshalling json for URL map: %v", err)
}
ioutil.WriteFile("urlmap.json", result, 0644)
return currID, nil
} else {
idLength += 1
}
}
}
return "", fmt.Errorf("input is not valid URL")
}
func getReq(c *gin.Context) {
id := c.Param("id")
fmt.Println(id)
url, exists := DATA_ARRAY[id]
if exists {
c.Redirect(http.StatusMovedPermanently, url)
}
}
func postReq(c *gin.Context) {
var link Shorten
c.BindJSON(&link)
if link.LongURL == "" {
c.JSON(noURL.ErrCode, noURL)
return
}
result, err := dataProccess(link.LongURL)
if err != nil {
c.JSON(invalidURL.ErrCode, invalidURL)
return
}
response := Shorten{
LongURL: link.LongURL,
ShortURL: result,
}
c.JSON(200, response)
}
func main() {
jsonFile, err := os.Open("urlmap.json")
if os.IsNotExist(err) {
jsonFile, err = os.Create("urlmap.json")
if err != nil {
logrus.Errorf("error while creating urlmap.json: %v", err)
os.Exit(1)
}
ioutil.WriteFile("urlmap.json", []byte("{}"), 0644)
} else if err != nil {
logrus.Errorf("error while opening map file: %v", err)
os.Exit(1)
}
defer jsonFile.Close()
fmt.Println("Successfully Opened url.json")
byteValue, _ := ioutil.ReadAll(jsonFile)
if err := json.Unmarshal(byteValue, &DATA_ARRAY); err != nil {
logrus.Errorf("error while unmarshalling json: %v", err)
os.Exit(1)
}
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/:id", getReq)
router.POST("/create", postReq)
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{})
})
router.Run()
}