-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (109 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
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
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
)
type URL struct {
Id string `json:"id"`
OriginalUrl string `json:"orgurl"`
ShortUrl string `json:"shorturl"`
CreatedAt time.Time `json:"createdat"`
}
// map contains key and value
var urlDb = make(map[string]URL)
func generateShortUrl(originalURL string) string {
// 1. hasing the incomming string
hasher := md5.New()
_, err := hasher.Write([]byte(originalURL))
if err != nil {
panic(err)
}
// fmt.Printf("type:%T\n", hasher)
// fmt.Println("value: ", value)
// fmt.Println("hasher: ", hasher)
// 2. hashing returns the array of distributed bytes
// summation of bytes
data := hasher.Sum(nil)
// fmt.Println("data after sum: ", data)
// fmt.Printf("data:%T\n", data)
// 3. encoding the bytes into an string using hex
encodedData := hex.EncodeToString(data)
// 4. retuning string of 8 charaters
return string(encodedData[:8])
}
func createURL(originalURL string) string {
// 1. calling short url and storing database with same id as of shortURL
shortURL := generateShortUrl(originalURL)
id := shortURL
urlDb[id] = URL{
Id: id,
OriginalUrl: originalURL,
ShortUrl: shortURL,
CreatedAt: time.Now(),
}
return shortURL
}
func getURL(origninalURL string) (URL, error) {
shortURL := createURL(origninalURL)
url, ok := urlDb[shortURL]
if !ok {
return URL{}, errors.New("URL NOT FOUND")
}
return url, nil
}
func main() {
fmt.Println("url shortner")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("GET method", r.Host)
// fmt.Println(r.URL.Path)
str := r.URL.Path[1:]
// fmt.Println("getting path", str)
originalURLFromMap := urlDb[str].OriginalUrl
// fmt.Println("getting original url from map: ",originalURLFromMap);
http.Redirect(w, r, originalURLFromMap, http.StatusSeeOther)
// Fprintf standardly write on that writer which we provides to it
// _, err := fmt.Fprintf(w, "hello sharad")
// if err != nil {
// panic(err)
// }
})
http.HandleFunc("/getShortURL", func(w http.ResponseWriter, r *http.Request) {
type Data struct {
Url string `json:"url"`
}
var data Data;
// json to struct
err := json.NewDecoder(r.Body).Decode(&data);
if err!=nil {
http.Error(w, "Invalid Body", http.StatusBadRequest)
}
originalURL := data.Url
urlObj, err_1 := getURL(originalURL)
// fmt.Println(urlObj)
if err_1!=nil {
panic(err_1)
}
new_shortURL := strings.Join([]string{"http://" + r.Host, urlObj.ShortUrl}, "/")
var sentData = Data{
Url: new_shortURL,
}
w.Header().Set("content-type", "application/json")
// struct to json
json.NewEncoder(w).Encode(sentData)
})
// starting HTTP server at port :8080
fmt.Println("starting server")
err := http.
ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
// handler function manages all itself GET, POST, ..
// it send the request on basic of data we send