-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.go
More file actions
100 lines (85 loc) · 2.75 KB
/
api.go
File metadata and controls
100 lines (85 loc) · 2.75 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
package shorts
import (
"encoding/json"
"net/http"
"net/url"
)
type UpdateRequest struct {
Type string
Slug string
URL string
Overwrite bool
}
// EditConfigHandler handles the /api/edit endpoint which allows updating redirect configurations
// It supports modifying both temporary and permanent redirects and can conditionally overwrite existing entries
func EditConfigHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
if r.Method != http.MethodPost {
http.Error(w, "Error: Method Not Allowed", http.StatusMethodNotAllowed)
return
}
var updateReq UpdateRequest
err := json.NewDecoder(r.Body).Decode(&updateReq)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
switch updateReq.Type {
case "temporary":
_, keyExists := Redirects.Temporary[updateReq.Slug]
if updateReq.Overwrite || !keyExists {
Redirects.Temporary[updateReq.Slug] = updateReq.URL
}
case "permanent":
_, keyExists := Redirects.Permanent[updateReq.Slug]
if updateReq.Overwrite || !keyExists {
Redirects.Permanent[updateReq.Slug] = updateReq.URL
}
default:
http.Error(w, "Invalid section", http.StatusBadRequest)
return
}
err = WriteRedirects()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// TryRedirectHandler handles the /api/try endpoint which attempts to redirect to a slug
// and falls back to a fallback path if the slug is not found
func TryRedirectHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
slug := r.URL.Query().Get("slug")
if slug == "" {
http.Error(w, "Missing required parameter: slug", http.StatusBadRequest)
return
}
fallback := r.URL.Query().Get("fallback")
if fallback == "" {
http.Error(w, "Missing required parameter: fallback", http.StatusBadRequest)
return
}
// Validate URL is in correct format
fallbackURL, err := url.Parse(fallback)
if err != nil || fallbackURL.Scheme == "" || fallbackURL.Host == "" {
http.Error(w, "Invalid fallback URL format", http.StatusBadRequest)
return
}
// Check if slug exists in redirects map
if url, ok := Redirects.Permanent[slug]; ok {
http.Redirect(w, r, url, http.StatusMovedPermanently)
UpdateStat(slug)
return
}
if url, ok := Redirects.Temporary[slug]; ok {
http.Redirect(w, r, url, http.StatusFound)
UpdateStat(slug)
return
}
// Slug not found, redirect to the provided fallback URL
http.Redirect(w, r, fallback, http.StatusFound)
}