-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
111 lines (95 loc) · 2.56 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 (
"fmt"
"math/rand"
"net/http"
"strings"
"time"
)
var urls = make(map[string]string)
func main() {
http.HandleFunc("/", handleForm)
http.HandleFunc("/shorten", handleShorten)
http.HandleFunc("/short/", handleRedirect)
fmt.Println("URL Shortener is running on :3030")
http.ListenAndServe(":3030", nil)
}
func handleForm(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
http.Redirect(w, r, "/shorten", http.StatusSeeOther)
return
}
// Serve the HTML form
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `
<!DOCTYPE html>
<html>
<head>
<title>URL Shortener</title>
</head>
<body>
<h2>URL Shortener</h2>
<form method="post" action="/shorten">
<input type="url" name="url" placeholder="Enter a URL" required>
<input type="submit" value="Shorten">
</form>
</body>
</html>
`)
}
func handleShorten(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
originalURL := r.FormValue("url")
if originalURL == "" {
http.Error(w, "URL parameter is missing", http.StatusBadRequest)
return
}
// Generate a unique shortened key for the original URL
shortKey := generateShortKey()
urls[shortKey] = originalURL
// Construct the full shortened URL
shortenedURL := fmt.Sprintf("http://localhost:3030/short/%s", shortKey)
// Serve the result page
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, `
<!DOCTYPE html>
<html>
<head>
<title>URL Shortener</title>
</head>
<body>
<h2>URL Shortener</h2>
<p>Original URL: `, originalURL, `</p>
<p>Shortened URL: <a href="`, shortenedURL, `">`, shortenedURL, `</a></p>
</body>
</html>
`)
}
func handleRedirect(w http.ResponseWriter, r *http.Request) {
shortKey := strings.TrimPrefix(r.URL.Path, "/short/")
if shortKey == "" {
http.Error(w, "Shortened key is missing", http.StatusBadRequest)
return
}
// Retrieve the original URL from the `urls` map using the shortened key
originalURL, found := urls[shortKey]
if !found {
http.Error(w, "Shortened key not found", http.StatusNotFound)
return
}
// Redirect the user to the original URL
http.Redirect(w, r, originalURL, http.StatusMovedPermanently)
}
func generateShortKey() string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const keyLength = 6
rand.Seed(time.Now().UnixNano())
shortKey := make([]byte, keyLength)
for i := range shortKey {
shortKey[i] = charset[rand.Intn(len(charset))]
}
return string(shortKey)
}