-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (97 loc) · 2.66 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
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
type URL struct {
ID string `json:"id"`
OriginalURL string `json:"original_url"`
ShortURL string `json:"short_url"`
CreationDate time.Time `json:"creation_date"`
}
/*
d9736711 --> {
ID: "d9736711",
OriginalURL: "https://github.com/Prince-1501/",
ShortURL: "d9736711",
CreationDate: time.Now()
}
*/
var urlDB = make(map[string]URL)
func generateShortURL(OriginalURL string) string {
hasher := md5.New()
hasher.Write([]byte(OriginalURL)) // It converts the originalURL string to a byte slice
fmt.Println("hasher: ", hasher)
data := hasher.Sum(nil)
fmt.Println("hasher data: ", data)
hash := hex.EncodeToString(data)
fmt.Println("EncodeToString: ", hash)
fmt.Println("final string: ", hash[:8])
return hash[:8]
}
func createURL(originalURL string) string {
shortURL := generateShortURL(originalURL)
id := shortURL // Use the short URL as the ID for simplicity
urlDB[id] = URL{
ID: id,
OriginalURL: originalURL,
ShortURL: shortURL,
CreationDate: time.Now(),
}
return shortURL
}
func getURL(id string) (URL, error) {
url, ok := urlDB[id]
if !ok {
return URL{}, errors.New("URL not found")
}
return url, nil
}
func RootPageURL(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, world!")
}
func ShortURLHandler(w http.ResponseWriter, r *http.Request) {
var data struct {
URL string `json:"url"`
}
err := json.NewDecoder(r.Body).Decode(&data)
if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
shortURL_ := createURL(data.URL)
// fmt.Fprintf(w, shortURL)
response := struct {
ShortURL string `json:"short_url"`
}{ShortURL: shortURL_}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func redirectURLHandler(w http.ResponseWriter, r *http.Request) {
id := r.URL.Path[len("/redirect/"):]
url, err := getURL(id)
if err != nil {
http.Error(w, "Invalid request", http.StatusNotFound)
}
http.Redirect(w, r, url.OriginalURL, http.StatusFound)
}
func main() {
// fmt.Println("Starting URL shortener...")
// OriginalURL := "https://github.com/Prince-1501/"
// generateShortURL(OriginalURL)
// Register the handler function to handle all requests to the root URL ("/")
http.HandleFunc("/", RootPageURL)
http.HandleFunc("/shorten", ShortURLHandler)
http.HandleFunc("/redirect/", redirectURLHandler)
// Start the HTTP server on port 8080
fmt.Println("Starting server on port 3000...")
err := http.ListenAndServe(":3000", nil)
if err != nil {
fmt.Println("Error on starting server:", err)
}
}