From cd318815f06372befbbabb2e4ee264228289adb1 Mon Sep 17 00:00:00 2001 From: iacore Date: Tue, 27 Aug 2024 15:44:39 +0000 Subject: [PATCH] clean up --- main.go | 39 +++++++++++++++++++++++-------------- routes/rankingCalendar.go | 6 ++++-- routes/tag.go | 2 +- routes/types.go | 7 +++++++ {routes => utils}/compat.go | 7 ++++++- 5 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 routes/types.go rename {routes => utils}/compat.go (93%) diff --git a/main.go b/main.go index e4627f3..499343c 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "codeberg.org/vnpower/pixivfe/v2/core" "codeberg.org/vnpower/pixivfe/v2/routes" "codeberg.org/vnpower/pixivfe/v2/session" + "codeberg.org/vnpower/pixivfe/v2/utils" ) func CanRequestSkipLimiter(r *http.Request) bool { @@ -70,16 +71,24 @@ func main() { setGlobalHeaders(w, r) - // redirect any request with ?r=url - redirect_to := r.URL.Query().Get("r") - if redirect_to != "" { - // could this be unsafe since this redirects to any website? - http.Redirect(w, r, redirect_to, http.StatusTemporaryRedirect) + if r.URL.Path != "/" && strings.HasSuffix(r.URL.Path, "/") { + url := r.URL + url.Path, _ = strings.CutSuffix(url.Path, "/") + http.Redirect(w, r, url.String(), http.StatusFound) + } else { + // redirect any request with ?r=url + redirect_to := r.URL.Query().Get("r") + if redirect_to != "" { + // could this be unsafe since this redirects to any website? + http.Redirect(w, r, redirect_to, http.StatusTemporaryRedirect) + } + + // all the routes are listed here + router.ServeHTTP(w, r) } - router.ServeHTTP(w, r) - CatchError(func(w http.ResponseWriter, r routes.CompatRequest) error { + CatchError(func(w http.ResponseWriter, r utils.CompatRequest) error { err := GetUserContext(r.Request).err if err != nil { // error handler log.Println(err) @@ -88,7 +97,7 @@ func main() { // Send custom error page err = routes.Render(w, r, routes.Data_error{Title: "Error", Error: err}) if err != nil { - err = routes.SendString(w, (fmt.Sprintf("Internal Server Error: %s", err))) + err = utils.SendString(w, (fmt.Sprintf("Internal Server Error: %s", err))) if err != nil { return err } @@ -195,9 +204,9 @@ func defineRoutes() *mux.Router { router.HandleFunc("/users/{id}/{category}.atom.xml", CatchError(routes.UserAtomFeed)).Methods("GET") router.HandleFunc("/users/{id}", CatchError(routes.UserPage)).Methods("GET") router.HandleFunc("/users/{id}/{category}", CatchError(routes.UserPage)).Methods("GET") - router.HandleFunc("/artworks/{id}/", CatchError(routes.ArtworkPage)).Methods("GET") - router.HandleFunc("/artworks-multi/{ids}/", CatchError(routes.ArtworkMultiPage)).Methods("GET") - router.HandleFunc("/novel/{id}/", CatchError(routes.NovelPage)).Methods("GET") + router.HandleFunc("/artworks/{id}", CatchError(routes.ArtworkPage)).Methods("GET") + router.HandleFunc("/artworks-multi/{ids}", CatchError(routes.ArtworkMultiPage)).Methods("GET") + router.HandleFunc("/novel/{id}", CatchError(routes.NovelPage)).Methods("GET") router.HandleFunc("/pixivision", CatchError(routes.PixivisionHomePage)).Methods("GET") router.HandleFunc("/pixivision/a/{id}", CatchError(routes.PixivisionArticlePage)).Methods("GET") @@ -224,7 +233,7 @@ func defineRoutes() *mux.Router { // Legacy illust URL router.HandleFunc("/member_illust.php", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, "/artworks/"+routes.CompatRequest{Request: r}.Query("illust_id"), http.StatusFound) + http.Redirect(w, r, "/artworks/"+utils.CompatRequest{Request: r}.Query("illust_id"), http.StatusFound) }).Methods("GET") // Proxy routes @@ -232,16 +241,16 @@ func defineRoutes() *mux.Router { handlePrefix(router, "/proxy/s.pximg.net/", CatchError(routes.SPximgProxy)).Methods("GET") handlePrefix(router, "/proxy/ugoira.com/", CatchError(routes.UgoiraProxy)).Methods("GET") - router.NewRoute().HandlerFunc(CatchError(func(w http.ResponseWriter, r routes.CompatRequest) error { + router.NewRoute().HandlerFunc(CatchError(func(w http.ResponseWriter, r utils.CompatRequest) error { return errors.New("Route not found") })) return router } -func CatchError(handler func(w http.ResponseWriter, r routes.CompatRequest) error) http.HandlerFunc { +func CatchError(handler func(w http.ResponseWriter, r utils.CompatRequest) error) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - GetUserContext(r).err = handler(w, routes.CompatRequest{Request: r}) + GetUserContext(r).err = handler(w, utils.CompatRequest{Request: r}) } } diff --git a/routes/rankingCalendar.go b/routes/rankingCalendar.go index 31aa8af..3e63c97 100644 --- a/routes/rankingCalendar.go +++ b/routes/rankingCalendar.go @@ -5,8 +5,10 @@ import ( "strconv" "time" - "codeberg.org/vnpower/pixivfe/v2/core" "net/http" + + "codeberg.org/vnpower/pixivfe/v2/core" + "codeberg.org/vnpower/pixivfe/v2/utils" ) type DateWrap struct { @@ -40,7 +42,7 @@ func RankingCalendarPicker(w http.ResponseWriter, r CompatRequest) error { } date := r.FormValue("date") - return RedirectToRoute(w, r,"/rankingCalendar", map[string]string{ + return utils.RedirectToRoute(w, r,"/rankingCalendar", map[string]string{ "mode": mode, "date": date, diff --git a/routes/tag.go b/routes/tag.go index a7570c2..3e83431 100644 --- a/routes/tag.go +++ b/routes/tag.go @@ -55,7 +55,7 @@ func TagPage(w http.ResponseWriter, r CompatRequest) error { } func AdvancedTagPost(w http.ResponseWriter, r CompatRequest) error { - return RedirectToRoute(w, r,"/tags", map[string]string{ + return utils.RedirectToRoute(w, r,"/tags", map[string]string{ "name": r.Query("name", r.FormValue("name")), "category": r.Query("category", "artworks"), "order": r.Query("order", "date_d"), diff --git a/routes/types.go b/routes/types.go new file mode 100644 index 0000000..5423bc4 --- /dev/null +++ b/routes/types.go @@ -0,0 +1,7 @@ +package routes + +import "codeberg.org/vnpower/pixivfe/v2/utils" + +type CompatRequest = utils.CompatRequest + +var SendString = utils.SendString \ No newline at end of file diff --git a/routes/compat.go b/utils/compat.go similarity index 93% rename from routes/compat.go rename to utils/compat.go index 8d7b81e..35e176a 100644 --- a/routes/compat.go +++ b/utils/compat.go @@ -1,6 +1,7 @@ -package routes +package utils import ( + "encoding/json" "net/http" "net/url" @@ -73,3 +74,7 @@ func RedirectToRoute(w http.ResponseWriter, r CompatRequest, path string, query_ http.Redirect(w, r.Request, path+query.Encode(), code) return nil } + +func WriteJson(w http.ResponseWriter, data any) { + json.NewEncoder(w).Encode(data) +} \ No newline at end of file