routes.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package views
  2. import (
  3. "net/http"
  4. "time"
  5. "codeberg.org/vnpower/pixivfe/configs"
  6. "codeberg.org/vnpower/pixivfe/handler"
  7. "github.com/gofiber/fiber/v2"
  8. "github.com/gofiber/fiber/v2/middleware/limiter"
  9. )
  10. var PC *handler.PixivClient
  11. // func not_found_page(c *fiber.Ctx) {
  12. // return c.Render(http.StatusNotFound, "error.html", fiber.Map{
  13. // "Title": "Not found",
  14. // "Error": "Route " + c.Request.URL.Path + " not found.",
  15. // })
  16. // }
  17. func NewPixivClient(timeout int) *handler.PixivClient {
  18. transport := &http.Transport{Proxy: http.ProxyFromEnvironment}
  19. client := &http.Client{
  20. Timeout: time.Duration(timeout) * time.Millisecond,
  21. Transport: transport,
  22. }
  23. pc := &handler.PixivClient{
  24. Client: client,
  25. Header: make(map[string]string),
  26. Cookie: make(map[string]string),
  27. Lang: "en",
  28. }
  29. return pc
  30. }
  31. func SetupRoutes(r *fiber.App) {
  32. PC = NewPixivClient(5000)
  33. PC.SetSessionID(configs.Token)
  34. PC.SetUserAgent(configs.UserAgent)
  35. PC.AddHeader("Accept-Language", configs.AcceptLanguage)
  36. limit := limiter.New(limiter.Config{
  37. Max: 10,
  38. })
  39. r.Get("/", index_page)
  40. r.Get("about", about_page)
  41. r.Get("artworks/:id/", limit, artwork_page).Name("artworks")
  42. r.Get("users/:id/:category?", user_page)
  43. r.Get("newest", newest_artworks_page)
  44. r.Get("ranking", ranking_page)
  45. r.Get("ranking_log", ranking_log_page)
  46. r.Get("tags/:name", search_page)
  47. r.Get("discovery", discovery_page)
  48. self := r.Group("self")
  49. self.Get("/", get_logged_in_user)
  50. self.Get("/following_works", following_works_page)
  51. self.Get("/bookmarks", your_bookmark_page)
  52. r.Get("login", login_page)
  53. r.Post("tags", search)
  54. settings := r.Group("settings")
  55. settings.Get("/", settings_page)
  56. settings.Post("/:type", settings_post)
  57. // Legacy illust URL
  58. r.Get("member_illust.php", func(c *fiber.Ctx) error {
  59. return c.Redirect("artworks/" + c.Query("illust_id"))
  60. })
  61. // 404 page
  62. // r.NoRoute(not_found_page)
  63. }