-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathhandlers.go
More file actions
121 lines (105 loc) · 3.17 KB
/
handlers.go
File metadata and controls
121 lines (105 loc) · 3.17 KB
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
package main
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"boot.dev/linko/internal/store"
"golang.org/x/crypto/bcrypt"
)
const shortURLLen = len("http://localhost:8080/") + 6
var (
redirectsMu sync.Mutex
redirects []string
)
//go:embed index.html
var indexPage string
func (s *server) handlerIndex(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
io.WriteString(w, indexPage)
}
func (s *server) handlerLogin(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func (s *server) handlerShortenLink(w http.ResponseWriter, r *http.Request) {
user, ok := r.Context().Value(UserContextKey).(string)
if !ok || user == "" {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
longURL := r.FormValue("url")
if longURL == "" {
http.Error(w, "missing url parameter", http.StatusBadRequest)
return
}
fmt.Println("Shortening URL:", longURL)
u, err := url.Parse(longURL)
if err != nil || u.Scheme == "" || u.Host == "" {
http.Error(w, "invalid URL: must include scheme (http/https) and host", http.StatusBadRequest)
return
}
fmt.Printf("Parsed URL: scheme=%s, host=%s\n", u.Scheme, u.Host)
if err := checkDestination(longURL); err != nil {
http.Error(w, fmt.Sprintf("invalid target URL: %v", err), http.StatusBadRequest)
return
}
shortCode, err := s.store.Create(r.Context(), longURL)
if err != nil {
http.Error(w, "failed to shorten URL", http.StatusInternalServerError)
return
}
fmt.Printf("Generated short code: %s for URL: %s\n", shortCode, longURL)
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusCreated)
io.WriteString(w, shortCode)
}
func (s *server) handlerRedirect(w http.ResponseWriter, r *http.Request) {
longURL, err := s.store.Lookup(r.Context(), r.PathValue("shortCode"))
if err != nil {
if errors.Is(err, store.ErrNotFound) {
http.Error(w, "not found", http.StatusNotFound)
} else {
fmt.Printf("failed to lookup URL: %v\n", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
}
return
}
_, _ = bcrypt.GenerateFromPassword([]byte(longURL), bcrypt.DefaultCost)
if err := checkDestination(longURL); err != nil {
http.Error(w, "destination unavailable", http.StatusBadGateway)
return
}
redirectsMu.Lock()
redirects = append(redirects, strings.Repeat(longURL, 1024))
redirectsMu.Unlock()
http.Redirect(w, r, longURL, http.StatusFound)
}
func (s *server) handlerListURLs(w http.ResponseWriter, r *http.Request) {
codes, err := s.store.List(r.Context())
if err != nil {
fmt.Printf("failed to list URLs: %v\n", err)
http.Error(w, "failed to list URLs", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(codes)
}
func (s *server) handlerStats(w http.ResponseWriter, _ *http.Request) {
redirectsMu.Lock()
snapshot := redirects
redirectsMu.Unlock()
var bytesSaved int
for _, u := range snapshot {
bytesSaved += len(u) - shortURLLen
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]int{
"redirects": len(snapshot),
"bytes_saved": bytesSaved,
})
}