-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (141 loc) · 3.7 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"context"
"html/template"
"log"
"math/rand"
"net/http"
"sync"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
mu sync.Mutex
shortURLs = make(map[string]string)
client *mongo.Client
collection *mongo.Collection
)
var tpl = template.Must(template.New("").Parse(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>URL Shortener</title>
</head>
<body>
<h1>URL Shortener</h1>
<form method="post" action="/shorten">
<label for="url">URL to Shorten:</label>
<input type="url" name="url" required>
<button type="submit">Shorten</button>
</form>
<br>
<h2>Shortened URLs:</h2>
<ul>
{{range $code, $url := .ShortURLs}}
<li><a href="/{{$code}}" target="_blank">{{$url}}</a></li>
{{end}}
</ul>
</body>
</html>
`))
type PageVariables struct {
ShortURLs map[string]string
}
type URLMapping struct {
Code string `bson:"code"`
URL string `bson:"url"`
}
func main() {
rand.Seed(time.Now().UnixNano())
// Connect to MongoDB
clientOptions := options.Client().ApplyURI("")
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(context.Background())
// Select the database and collection
database := client.Database("urlshortener")
collection = database.Collection("urls")
// Initialize HTTP server
r := http.NewServeMux()
r.HandleFunc("/", homeHandler)
r.HandleFunc("/shorten", shortenHandler)
r.HandleFunc("/{code}", redirectHandler)
log.Fatal(http.ListenAndServe(":4001", r))
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
pageVariables := PageVariables{
ShortURLs: shortURLs,
}
err := tpl.Execute(w, pageVariables)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func shortenHandler(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
url := r.FormValue("url")
if url == "" {
http.Error(w, "URL cannot be empty", http.StatusBadRequest)
return
}
shortCode := generateShortCode()
shortURLs[shortCode] = url
// Save to MongoDB
if err := saveToMongoDB(shortCode, url); err != nil {
log.Printf("Failed to save to database: %v", err)
http.Error(w, "Failed to save to database", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func redirectHandler(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
shortCode := r.URL.Path[1:]
if originalURL, ok := shortURLs[shortCode]; ok {
http.Redirect(w, r, originalURL, http.StatusSeeOther)
return
}
// If not found in the map, try to find in MongoDB
url, err := findInMongoDB(shortCode)
if err == nil && url != "" {
http.Redirect(w, r, url, http.StatusSeeOther)
return
}
http.NotFound(w, r)
}
func generateShortCode() string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
codeLength := 6
b := make([]byte, codeLength)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}
func saveToMongoDB(code, url string) error {
_, err := collection.InsertOne(context.Background(), URLMapping{Code: code, URL: url})
if err != nil {
log.Printf("Error saving to MongoDB: %v", err)
}
return err
}
func findInMongoDB(code string) (string, error) {
var result URLMapping
err := collection.FindOne(context.Background(), bson.M{"code": code}).Decode(&result)
if err != nil {
log.Printf("Error finding URL in MongoDB: %v", err)
return "", err
}
return result.URL, nil
}