router.go 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package handlers
  2. import (
  3. "errors"
  4. "net/http"
  5. "strings"
  6. "codeberg.org/vnpower/pixivfe/v2/routes"
  7. "github.com/gorilla/mux"
  8. )
  9. func handlePrefix(router *mux.Router, pathPrefix string, handler http.Handler) *mux.Route {
  10. return router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix, handler))
  11. }
  12. func serveFile(filename string) func(w http.ResponseWriter, r *http.Request) {
  13. return func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, filename) }
  14. }
  15. func DefineRoutes() *mux.Router {
  16. router := mux.NewRouter()
  17. // redirect handler: strip trailing / to make router behave
  18. router.MatcherFunc(func(r *http.Request, rm *mux.RouteMatch) bool {
  19. return r.URL.Path != "/" && strings.HasSuffix(r.URL.Path, "/")
  20. }).HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  21. url := r.URL
  22. url.Path = url.Path[0 : len(url.Path)-1]
  23. http.Redirect(w, r, url.String(), http.StatusPermanentRedirect)
  24. })
  25. //router.Use(MiddlewareChain)
  26. router.HandleFunc("/favicon.ico", serveFile("./assets/img/favicon.ico"))
  27. router.HandleFunc("/robots.txt", serveFile("./assets/robots.txt"))
  28. handlePrefix(router, "/img/", http.FileServer(http.Dir("./assets/img")))
  29. handlePrefix(router, "/css/", http.FileServer(http.Dir("./assets/css")))
  30. handlePrefix(router, "/js/", http.FileServer(http.Dir("./assets/js")))
  31. // Proxy routes. cache headers set by upstream servers.
  32. handlePrefix(router, "/proxy/i.pximg.net/", CatchError(routes.IPximgProxy)).Methods("GET")
  33. handlePrefix(router, "/proxy/s.pximg.net/", CatchError(routes.SPximgProxy)).Methods("GET")
  34. handlePrefix(router, "/proxy/ugoira.com/", CatchError(routes.UgoiraProxy)).Methods("GET")
  35. router.HandleFunc("/", CatchError(routes.IndexPage)).Methods("GET")
  36. router.HandleFunc("/about", CatchError(routes.AboutPage)).Methods("GET")
  37. router.HandleFunc("/newest", CatchError(routes.NewestPage)).Methods("GET")
  38. router.HandleFunc("/discovery", CatchError(routes.DiscoveryPage)).Methods("GET")
  39. router.HandleFunc("/discovery/novel", CatchError(routes.NovelDiscoveryPage)).Methods("GET")
  40. router.HandleFunc("/ranking", CatchError(routes.RankingPage)).Methods("GET")
  41. router.HandleFunc("/rankingCalendar", CatchError(routes.RankingCalendarPage)).Methods("GET")
  42. router.HandleFunc("/rankingCalendar", CatchError(routes.RankingCalendarPicker)).Methods("POST")
  43. router.HandleFunc("/users/{id}.atom.xml", CatchError(routes.UserAtomFeed)).Methods("GET")
  44. router.HandleFunc("/users/{id}/{category}.atom.xml", CatchError(routes.UserAtomFeed)).Methods("GET")
  45. router.HandleFunc("/users/{id}", CatchError(routes.UserPage)).Methods("GET")
  46. router.HandleFunc("/users/{id}/{category}", CatchError(routes.UserPage)).Methods("GET")
  47. router.HandleFunc("/artworks/{id}", CatchError(routes.ArtworkPage)).Methods("GET")
  48. router.HandleFunc("/artworks-multi/{ids}", CatchError(routes.ArtworkMultiPage)).Methods("GET")
  49. router.HandleFunc("/novel/{id}", CatchError(routes.NovelPage)).Methods("GET")
  50. router.HandleFunc("/pixivision", CatchError(routes.PixivisionHomePage)).Methods("GET")
  51. router.HandleFunc("/pixivision/a/{id}", CatchError(routes.PixivisionArticlePage)).Methods("GET")
  52. router.HandleFunc("/settings", CatchError(routes.SettingsPage)).Methods("GET")
  53. router.HandleFunc("/settings/{type}", CatchError(routes.SettingsPost)).Methods("POST")
  54. router.HandleFunc("/self", CatchError(routes.LoginUserPage)).Methods("GET")
  55. router.HandleFunc("/self/followingWorks", CatchError(routes.FollowingWorksPage)).Methods("GET")
  56. router.HandleFunc("/self/bookmarks", CatchError(routes.LoginBookmarkPage)).Methods("GET")
  57. router.HandleFunc("/self/addBookmark/{id}", CatchError(routes.AddBookmarkRoute)).Methods("GET")
  58. router.HandleFunc("/self/deleteBookmark/{id}", CatchError(routes.DeleteBookmarkRoute)).Methods("GET")
  59. router.HandleFunc("/self/like/{id}", CatchError(routes.LikeRoute)).Methods("GET")
  60. router.HandleFunc("/oembed", CatchError(routes.Oembed)).Methods("GET")
  61. router.HandleFunc("/tags/{name}", CatchError(routes.TagPage)).Methods("GET")
  62. router.HandleFunc("/tags/{name}", CatchError(routes.TagPage)).Methods("POST")
  63. router.HandleFunc("/tags", CatchError(routes.TagPage)).Methods("GET")
  64. router.HandleFunc("/tags", CatchError(routes.AdvancedTagPost)).Methods("POST")
  65. // Legacy illust URL
  66. router.HandleFunc("/member_illust.php", func(w http.ResponseWriter, r *http.Request) {
  67. http.Redirect(w, r, "/artworks/"+routes.GetQueryParam(r, "illust_id"), http.StatusPermanentRedirect)
  68. }).Methods("GET")
  69. router.NewRoute().HandlerFunc(CatchError(func(w http.ResponseWriter, r *http.Request) error {
  70. return errors.New("Route not found")
  71. }))
  72. return router
  73. }