-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURLShortnet.go
60 lines (42 loc) · 1.11 KB
/
URLShortnet.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
//create two files a JSON and YAML file with Shortcut and URL Mapping
package main
import (
"fmt"
"net/http"
s "strings"
)
type urlShortCut struct {
shortcut string
urls string
}
func main() {
jsonUrlMap := map[string]string{
"site": "https://www.ui5cn.com",
"blog": "https://www.blog.ui5cn.com",
}
jsonUrlMap["pricing"] = "https://www.ui5cn.com/pages/pricing"
//http.HandleFunc("/", navigate)
getURL(jsonUrlMap)
http.ListenAndServe(":8080", nil)
}
func getURL(mapURL map[string]string) {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
userLink := r.URL.Path
userURL := s.Split(userLink, "/")[1]
if userURL == "" {
fmt.Fprintf(w, "This is Home")
} else if _, ok := mapURL[userURL]; ok {
http.Redirect(w, r, mapURL[userURL], http.StatusFound)
} else {
fmt.Fprintf(w, "URL Not Found")
}
fmt.Println(userURL)
})
}
func redirect(userUrl string){
// func navigate(w http.ResponseWriter, r *http.Request) {
// userLink := r.URL.Path
// fmt.Println(userLink)
// //http.Redirect(w, r, newUrl, http.StatusSeeOther)
// fmt.Fprintf(w, "Test Working")
// }