Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit f326d15

Browse files
author
Your Name
committed
rewrite
1 parent ca80360 commit f326d15

File tree

3 files changed

+79
-125
lines changed

3 files changed

+79
-125
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module shorts
22

33
go 1.18
44

5-
require github.com/gorilla/mux v1.8.0
5+
require github.com/go-chi/chi/v5 v5.0.7

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
2-
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
1+
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
2+
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=

main.go

+76-122
Original file line numberDiff line numberDiff line change
@@ -1,158 +1,112 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"net/http"
75

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"
138
)
149

1510
const (
1611
mainDomain = "https://infinitybots.gg"
1712
)
1813

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
3415

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)
3719

38-
r.HandleFunc(path, wrapRoute(r, f))
20+
http.Redirect(w, r, redirPath, http.StatusTemporaryRedirect)
21+
}
3922
}
4023

4124
func main() {
42-
r := mux.NewRouter()
25+
r := chi.NewRouter()
4326

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 {
4530
return mainDomain + r.URL.EscapedPath()
4631
}))
4732

4833
// handle / route
49-
shortRoute("/", r, func(r *http.Request, vars map[string]string) string {
34+
r.Handle("/", wrap(func(r *http.Request) string {
5035
return mainDomain
51-
})
36+
}))
5237

5338
// 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+
}))
6542

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+
}))
6946

7047
// 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+
}))
8751

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+
}
9271

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+
}))
9674

9775
// 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+
}
10988

11089
// 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+
}
153106

154-
go fmt.Println(r.URL, "=>", http.StatusOK)
155-
})
107+
err := http.ListenAndServe(":1010", r)
156108

157-
http.ListenAndServe(":1010", r)
109+
if err != nil {
110+
panic(err)
111+
}
158112
}

0 commit comments

Comments
 (0)