|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "encoding/json" |
5 |
| - "fmt" |
6 | 4 | "net/http"
|
7 | 5 |
|
8 |
| - "github.com/gorilla/mux" |
9 |
| -) |
10 |
| - |
11 |
| -var ( |
12 |
| - shortRoutes = make(map[string]func(r *http.Request, vars map[string]string) string) |
| 6 | + "github.com/go-chi/chi/v5" |
| 7 | + "github.com/go-chi/chi/v5/middleware" |
13 | 8 | )
|
14 | 9 |
|
15 | 10 | const (
|
16 | 11 | mainDomain = "https://infinitybots.gg"
|
17 | 12 | )
|
18 | 13 |
|
19 |
| -func wrapRoute(router *mux.Router, f func(r *http.Request, vars map[string]string) string) http.HandlerFunc { |
20 |
| - return func(w http.ResponseWriter, r *http.Request) { |
21 |
| - if r.URL.Query().Get("debug") == "true" { |
22 |
| - w.WriteHeader(200) |
23 |
| - resp := f(r, mux.Vars(r)) |
24 |
| - go fmt.Println(r.URL, "=>", resp) |
25 |
| - w.Write([]byte("Going to redirect to " + resp)) |
26 |
| - return |
27 |
| - } |
28 |
| - |
29 |
| - resp := f(r, mux.Vars(r)) |
30 |
| - go fmt.Println(r.URL, "=>", resp) |
31 |
| - http.Redirect(w, r, resp, http.StatusFound) |
32 |
| - } |
33 |
| -} |
| 14 | +type Handler = func(r *http.Request) string |
34 | 15 |
|
35 |
| -func shortRoute(path string, r *mux.Router, f func(r *http.Request, vars map[string]string) string) { |
36 |
| - shortRoutes[path] = f |
| 16 | +func wrap(fn Handler) http.HandlerFunc { |
| 17 | + return func(w http.ResponseWriter, r *http.Request) { |
| 18 | + var redirPath = fn(r) |
37 | 19 |
|
38 |
| - r.HandleFunc(path, wrapRoute(r, f)) |
| 20 | + http.Redirect(w, r, redirPath, http.StatusTemporaryRedirect) |
| 21 | + } |
39 | 22 | }
|
40 | 23 |
|
41 | 24 | func main() {
|
42 |
| - r := mux.NewRouter() |
| 25 | + r := chi.NewRouter() |
43 | 26 |
|
44 |
| - r.NotFoundHandler = http.HandlerFunc(wrapRoute(r, func(r *http.Request, vars map[string]string) string { |
| 27 | + r.Use(middleware.CleanPath) |
| 28 | + |
| 29 | + r.NotFound(wrap(func(r *http.Request) string { |
45 | 30 | return mainDomain + r.URL.EscapedPath()
|
46 | 31 | }))
|
47 | 32 |
|
48 | 33 | // handle / route
|
49 |
| - shortRoute("/", r, func(r *http.Request, vars map[string]string) string { |
| 34 | + r.Handle("/", wrap(func(r *http.Request) string { |
50 | 35 | return mainDomain
|
51 |
| - }) |
| 36 | + })) |
52 | 37 |
|
53 | 38 | // We redirect bots onto itself to allow for better redirects
|
54 |
| - shortRoute("/bot/{id}", r, func(r *http.Request, vars map[string]string) string { |
55 |
| - return "https://" + r.Host + "/" + vars["id"] |
56 |
| - }) |
57 |
| - |
58 |
| - shortRoute("/bot/{id}/{path}", r, func(r *http.Request, vars map[string]string) string { |
59 |
| - return "https://" + r.Host + "/" + vars["id"] + "/" + vars["path"] |
60 |
| - }) |
61 |
| - |
62 |
| - shortRoute("/bots/{id}", r, func(r *http.Request, vars map[string]string) string { |
63 |
| - return "https://" + r.Host + "/" + vars["id"] |
64 |
| - }) |
| 39 | + r.Handle("/bot/{id}", wrap(func(r *http.Request) string { |
| 40 | + return "https://" + r.Host + "/" + chi.URLParam(r, "id") |
| 41 | + })) |
65 | 42 |
|
66 |
| - shortRoute("/bots/{id}/{path}", r, func(r *http.Request, vars map[string]string) string { |
67 |
| - return "https://" + r.Host + "/" + vars["id"] + "/" + vars["path"] |
68 |
| - }) |
| 43 | + r.Handle("/bot/{id}/{path}", wrap(func(r *http.Request) string { |
| 44 | + return "https://" + r.Host + "/" + chi.URLParam(r, "id") + "/" + chi.URLParam(r, "path") |
| 45 | + })) |
69 | 46 |
|
70 | 47 | // Bot redirects
|
71 |
| - shortRoute("/{id}", r, func(r *http.Request, vars map[string]string) string { |
72 |
| - return mainDomain + "/bots/" + vars["id"] |
73 |
| - }) |
74 |
| - |
75 |
| - // Invite |
76 |
| - shortRoute("/{id}/i", r, func(r *http.Request, vars map[string]string) string { |
77 |
| - return mainDomain + "/bots/" + vars["id"] + "/invite" |
78 |
| - }) |
79 |
| - |
80 |
| - shortRoute("/{id}/inv", r, func(r *http.Request, vars map[string]string) string { |
81 |
| - return mainDomain + "/bots/" + vars["id"] + "/invite" |
82 |
| - }) |
83 |
| - |
84 |
| - shortRoute("/{id}/invite", r, func(r *http.Request, vars map[string]string) string { |
85 |
| - return mainDomain + "/bots/" + vars["id"] + "/invite" |
86 |
| - }) |
| 48 | + r.Handle("/{id}", wrap(func(r *http.Request) string { |
| 49 | + return mainDomain + "/bots/" + chi.URLParam(r, "id") |
| 50 | + })) |
87 | 51 |
|
88 |
| - // Vote |
89 |
| - shortRoute("/{id}/v", r, func(r *http.Request, vars map[string]string) string { |
90 |
| - return mainDomain + "/bots/" + vars["id"] + "/vote" |
91 |
| - }) |
| 52 | + r.Handle("/{id}/{path}", wrap(func(r *http.Request) string { |
| 53 | + path := chi.URLParam(r, "path") |
| 54 | + |
| 55 | + var redirPath string |
| 56 | + |
| 57 | + switch path { |
| 58 | + case "i": |
| 59 | + redirPath = "invite" |
| 60 | + case "inv": |
| 61 | + redirPath = "invite" |
| 62 | + case "invite": |
| 63 | + redirPath = "invite" |
| 64 | + case "v": |
| 65 | + redirPath = "vote" |
| 66 | + case "vote": |
| 67 | + redirPath = "vote" |
| 68 | + default: |
| 69 | + redirPath = path |
| 70 | + } |
92 | 71 |
|
93 |
| - shortRoute("/{id}/vote", r, func(r *http.Request, vars map[string]string) string { |
94 |
| - return mainDomain + "/bots/" + vars["id"] + "/vote" |
95 |
| - }) |
| 72 | + return mainDomain + "/bots/" + chi.URLParam(r, "id") + "/" + redirPath |
| 73 | + })) |
96 | 74 |
|
97 | 75 | // Packs
|
98 |
| - shortRoute("/p/{id}", r, func(r *http.Request, vars map[string]string) string { |
99 |
| - return mainDomain + "/packs/" + vars["id"] |
100 |
| - }) |
101 |
| - |
102 |
| - shortRoute("/{id}/p", r, func(r *http.Request, vars map[string]string) string { |
103 |
| - return mainDomain + "/packs/" + vars["id"] |
104 |
| - }) |
105 |
| - |
106 |
| - shortRoute("/{id}/packs", r, func(r *http.Request, vars map[string]string) string { |
107 |
| - return mainDomain + "/packs/" + vars["id"] |
108 |
| - }) |
| 76 | + for _, p := range []string{ |
| 77 | + "/pack/{id}", |
| 78 | + "/packs/{id}", |
| 79 | + "/p/{id}", |
| 80 | + "/pack/{id}/", |
| 81 | + "/packs/{id}/", |
| 82 | + "/p/{id}/", |
| 83 | + } { |
| 84 | + r.Handle(p, wrap(func(r *http.Request) string { |
| 85 | + return mainDomain + "/packs/" + chi.URLParam(r, "id") |
| 86 | + })) |
| 87 | + } |
109 | 88 |
|
110 | 89 | // User routes
|
111 |
| - shortRoute("/u/{id}", r, func(r *http.Request, vars map[string]string) string { |
112 |
| - return mainDomain + "/users/" + vars["id"] |
113 |
| - }) |
114 |
| - |
115 |
| - shortRoute("/user/{id}", r, func(r *http.Request, vars map[string]string) string { |
116 |
| - return mainDomain + "/users/" + vars["id"] |
117 |
| - }) |
118 |
| - |
119 |
| - shortRoute("/users/{id}", r, func(r *http.Request, vars map[string]string) string { |
120 |
| - return mainDomain + "/users/" + vars["id"] |
121 |
| - }) |
122 |
| - |
123 |
| - shortRoute("/profile/{id}", r, func(r *http.Request, vars map[string]string) string { |
124 |
| - return mainDomain + "/users/" + vars["id"] |
125 |
| - }) |
126 |
| - |
127 |
| - shortRoute("/profiles/{id}", r, func(r *http.Request, vars map[string]string) string { |
128 |
| - return mainDomain + "/users/" + vars["id"] |
129 |
| - }) |
130 |
| - |
131 |
| - // Short API |
132 |
| - |
133 |
| - r.HandleFunc("/api/redirects.json", func(w http.ResponseWriter, r *http.Request) { |
134 |
| - var apiResp = make(map[string]string) |
135 |
| - |
136 |
| - for k, v := range shortRoutes { |
137 |
| - apiResp[k] = v(r, map[string]string{ |
138 |
| - "id": "%ID%", |
139 |
| - "path": "%PATH%", |
140 |
| - }) |
141 |
| - } |
142 |
| - |
143 |
| - bytes, err := json.Marshal(apiResp) |
144 |
| - |
145 |
| - if err != nil { |
146 |
| - w.WriteHeader(http.StatusInternalServerError) |
147 |
| - w.Write([]byte("Error marshalling JSON")) |
148 |
| - return |
149 |
| - } |
150 |
| - |
151 |
| - w.Header().Set("Content-Type", "application/json") |
152 |
| - w.Write(bytes) |
| 90 | + for _, p := range []string{ |
| 91 | + "/u/{id}", |
| 92 | + "/user/{id}", |
| 93 | + "/users/{id}", |
| 94 | + "/profile/{id}", |
| 95 | + "/profiles/{id}", |
| 96 | + "/u/{id}/", |
| 97 | + "/user/{id}/", |
| 98 | + "/users/{id}/", |
| 99 | + "/profile/{id}/", |
| 100 | + "/profiles/{id}/", |
| 101 | + } { |
| 102 | + r.Handle(p, wrap(func(r *http.Request) string { |
| 103 | + return mainDomain + "/users/" + chi.URLParam(r, "id") |
| 104 | + })) |
| 105 | + } |
153 | 106 |
|
154 |
| - go fmt.Println(r.URL, "=>", http.StatusOK) |
155 |
| - }) |
| 107 | + err := http.ListenAndServe(":1010", r) |
156 | 108 |
|
157 |
| - http.ListenAndServe(":1010", r) |
| 109 | + if err != nil { |
| 110 | + panic(err) |
| 111 | + } |
158 | 112 | }
|
0 commit comments