-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (56 loc) · 1.54 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
package main
import (
"fmt"
"net/http"
"strings"
"sync"
)
// URLStore keeps track of short to long URL mappings.
var (
URLStore = make(map[string]string)
mu sync.Mutex
)
// RedirectHandler is called when someone visits a short URL.
func RedirectHandler(w http.ResponseWriter, r *http.Request) {
shortURL := r.URL.Path[1:] // Get the part after the "/"
mu.Lock()
longURL, ok := URLStore[shortURL]
mu.Unlock()
if ok {
http.Redirect(w, r, longURL, http.StatusFound)
} else {
http.NotFound(w, r) // Oops! Short URL not found.
}
}
func main() {
// Start the HTTP server in the background.
go func() {
http.HandleFunc("/", RedirectHandler)
if err := http.ListenAndServe(":8080", nil); err != nil {
fmt.Printf("Oops! Server didn't start: %v\n", err)
}
}()
fmt.Println("Server is up and running at http://localhost:8080")
for {
var longURL, shortURL string
// Ask for the long URL.
fmt.Print("Enter long URL: ")
fmt.Scanln(&longURL)
// Add "https://" if the URL doesn’t start with "http://" or "https://"
if !strings.HasPrefix(longURL, "http://") && !strings.HasPrefix(longURL, "https://") {
longURL = "https://" + longURL
}
// Ask for a custom short URL.
fmt.Print("Enter custom short URL (optional): ")
fmt.Scanln(&shortURL)
// If no custom URL is given, generate one.
if shortURL == "" {
shortURL = fmt.Sprintf("%d", len(URLStore)+1)
}
// Store the URL mapping.
mu.Lock()
URLStore[shortURL] = longURL
mu.Unlock()
fmt.Printf("Your short URL is: http://localhost:8080/%s\n\n", shortURL)
}
}