-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (72 loc) · 2.27 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
package main
import (
"fmt"
"math/rand"
"net/http"
"time"
)
type URLShortener struct {
urls map[string]string
}
func (us *URLShortener) 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()
us.urls[shortKey] = originalURL
// Construct the full shortened URL
shortenedURL := fmt.Sprintf("http://localhost:8080/short/%s", shortKey)
// Render the HTML response with the shortened URL
w.Header().Set("Content-Type", "text/html")
responseHTML := fmt.Sprintf(`
<h2>URL Shortener</h2>
<p>Original URL: %s</p>
<p>Shortened URL: <a href="%s">%s</a></p>
<form method="post" action="/shorten">
<input type="text" name="url" placeholder="Enter a URL">
<input type="submit" value="Shorten">
</form>
`, originalURL, shortenedURL, shortenedURL)
fmt.Fprintf(w, responseHTML)
}
func (us *URLShortener) HandleRedirect(w http.ResponseWriter, r *http.Request) {
shortKey := r.URL.Path[len("/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 := us.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)
}
func main() {
shortener := &URLShortener{
urls: make(map[string]string),
}
http.HandleFunc("/shorten", shortener.HandleShorten)
http.HandleFunc("/short/", shortener.HandleRedirect)
fmt.Println("URL Shortener is running on :8080")
http.ListenAndServe(":8080", nil)
}