-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredirect.go
90 lines (73 loc) · 1.86 KB
/
redirect.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
package main
import (
"net/http"
"strings"
"github.com/mtgban/go-mtgban/mtgmatcher"
)
func Redirect(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/go/")
fields := strings.Split(path, "/")
if len(fields) == 2 || len(fields) == 3 {
kind := fields[0]
store := fields[len(fields)-2]
hash := fields[len(fields)-1]
// Default to retail in short mode
if kind == store {
kind = "r"
}
if kind == "r" || kind == "i" {
inv, err := findSellerInventory(store)
if err != nil {
http.NotFound(w, r)
return
}
// Look up the hash: mtgjson, scryfall, and tcgproductid in order
entries, found := inv[hash]
if !found {
entries, found = inv[mtgmatcher.Scryfall2UUID(hash)]
if !found {
entries = inv[mtgmatcher.Tcg2UUID(hash)]
}
}
for _, entry := range entries {
http.Redirect(w, r, entry.URL, http.StatusFound)
return
}
} else if kind == "b" {
bl, err := findVendorBuylist(store)
if err != nil {
http.NotFound(w, r)
return
}
// Look up the hash: mtgjson, scryfall, and tcgproductid in order
entries, found := bl[hash]
if !found {
entries, found = bl[mtgmatcher.Scryfall2UUID(hash)]
if !found {
entries = bl[mtgmatcher.Tcg2UUID(hash)]
}
}
for _, entry := range entries {
http.Redirect(w, r, entry.URL, http.StatusFound)
return
}
}
}
http.NotFound(w, r)
}
func RandomSearch(w http.ResponseWriter, r *http.Request) {
uuid := randomUUID(false)
v := r.URL.Query()
v.Set("q", uuid)
r.URL.RawQuery = v.Encode()
r.URL.Path = "/search"
http.Redirect(w, r, r.URL.String(), http.StatusFound)
}
func RandomSealedSearch(w http.ResponseWriter, r *http.Request) {
uuid := randomUUID(true)
v := r.URL.Query()
v.Set("q", uuid)
r.URL.RawQuery = v.Encode()
r.URL.Path = "/sealed"
http.Redirect(w, r, r.URL.String(), http.StatusFound)
}