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 pathcomment.go.bak
206 lines (168 loc) · 4.15 KB
/
comment.go.bak
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Unused code that might be useless for later
/*
r.GET("/test", func(c *gin.Context) {
c.Request.URL.Path = "/test2"
r.HandleContext(c)
})
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
r.GET(currID, func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, inputURL)
})
data := urldatabase{
UrlID: currID,
LongURL: inputURL,
}
fmt.Println(data)
file, _ := json.MarshalIndent(data, "", " ")
fmt.Print(string(file))
ioutil.WriteFile("urlmap.json", file, 0644)
below is unchanged prev code:
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"github.com/gin-gonic/gin"
gonanoid "github.com/matoous/go-nanoid/v2"
)
// Look this is a branch for a PR
func genID(length int) string {
id, err := gonanoid.New(length)
if err != nil {
panic(err)
}
fmt.Printf("Generated id: %s\n", id)
return id
}
func isUrl(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}
type urldatabase struct {
UrlID string `json:"urlID"`
LongURL string `json:"longURL"`
}
func routerSetup(data []urldatabase) *gin.Engine {
router := gin.Default()
for i := range data {
router.GET(data[i].UrlID, func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, data[i].LongURL)
})
}
router.GET("/home", func(c *gin.Context) {
c.JSON(200, gin.H{"hello": "world"})
})
return router
}
func main() {
sc := bufio.NewScanner(os.Stdin)
fmt.Println("Type 'r' to run server or 'a' to add urls to the database: ")
sc.Scan()
in := sc.Text()
if in == "r" || in == "R" {
var dataArray []urldatabase
jsonFile, err := os.Open("urlmap.json")
if err != nil {
fmt.Println(err)
}
byteValue, _ := ioutil.ReadAll(jsonFile)
if err := json.Unmarshal(byteValue, &dataArray); err != nil {
log.Println(err)
}
jsonFile.Close()
router := routerSetup(dataArray)
for _, v := range dataArray {
fmt.Println("localhost:8080/" + v.UrlID + " " + v.LongURL)
}
router.Run(":8080")
} else {
var dataArray []urldatabase
jsonFile, err := os.Open("urlmap.json")
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened url.json")
byteValue, _ := ioutil.ReadAll(jsonFile)
if err := json.Unmarshal(byteValue, &dataArray); err != nil {
log.Println(err)
}
M := make(map[string]string)
idLength := 3
for {
fmt.Println("")
fmt.Println("Type (c/C) to check the stored data for this run")
fmt.Println("Type (s/S) to shorten/store a url")
fmt.Println("Type (l/L) to look up a url using a key/id")
fmt.Print("Type anything else to quit: ")
sc.Scan()
input := sc.Text()
fmt.Println("")
if input == "c" || input == "C" {
for id, url := range M {
fmt.Println("ID:", id, "=> URL:", url)
}
} else if input == "s" || input == "S" {
fmt.Println("Please paste your url: ")
sc.Scan()
inputURL := sc.Text()
if isUrl(inputURL) {
for {
currID := genID(idLength)
if _, ok := M[currID]; !ok {
M[currID] = inputURL
dataArray = append(dataArray, urldatabase{UrlID: currID, LongURL: inputURL})
for _, v := range dataArray {
fmt.Println(v)
}
break
} else {
idLength += 1
}
}
} else {
fmt.Println("invalid url")
}
} else if input == "l" || input == "L" {
fmt.Println("Please paste an ID: ")
sc.Scan()
inputID := sc.Text()
if val, ok := M[inputID]; ok {
fmt.Println("Your Url is: " + val)
} else {
fmt.Println("The ID you provided is invalid")
}
} else {
break
}
}
result, err := json.Marshal(dataArray)
if err != nil {
log.Println(err)
}
ioutil.WriteFile("urlmap.json", result, 0644)
jsonFile.Close()
}
}
// func postReq(c *gin.Context) {
// longURL := c.PostForm("longURL")
// if longURL == "" {
// c.String(http.StatusBadRequest, "enter a url to shorten")
// return
// } else {
// result, err := dataProccess(longURL)
// if err != nil {
// c.String(http.StatusBadRequest, "invalid URL")
// } else {
// c.String(http.StatusOK, fmt.Sprintf(
// "Your new link is: %s/%s", SERVER_URL, result))
// }
// }
// }
*/