main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package main
  2. import (
  3. "net"
  4. "strings"
  5. "time"
  6. "codeberg.org/vnpower/pixivfe/configs"
  7. "codeberg.org/vnpower/pixivfe/handler"
  8. "codeberg.org/vnpower/pixivfe/views"
  9. "github.com/goccy/go-json"
  10. "github.com/gofiber/fiber/v2"
  11. "github.com/gofiber/fiber/v2/middleware/cache"
  12. "github.com/gofiber/fiber/v2/middleware/compress"
  13. "github.com/gofiber/fiber/v2/middleware/logger"
  14. "github.com/gofiber/fiber/v2/middleware/recover"
  15. "github.com/gofiber/fiber/v2/utils"
  16. "github.com/gofiber/template/jet/v2"
  17. )
  18. func setup_router() *fiber.App {
  19. // HTML templates, automatically loaded
  20. engine := jet.New("./template", ".jet.html")
  21. engine.AddFuncMap(handler.GetTemplateFunctions())
  22. server := fiber.New(fiber.Config{
  23. AppName: "PixivFE",
  24. DisableStartupMessage: true,
  25. Views: engine,
  26. Prefork: false,
  27. JSONEncoder: json.Marshal,
  28. JSONDecoder: json.Unmarshal,
  29. ViewsLayout: "layout",
  30. EnableTrustedProxyCheck: true,
  31. TrustedProxies: []string{"0.0.0.0/0"},
  32. ProxyHeader: fiber.HeaderXForwardedFor,
  33. ErrorHandler: func(c *fiber.Ctx, err error) error {
  34. // Status code defaults to 500
  35. code := fiber.StatusInternalServerError
  36. // // Retrieve the custom status code if it's a *fiber.Error
  37. // var e *fiber.Error
  38. // if errors.As(err, &e) {
  39. // code = e.Code
  40. // }
  41. // Send custom error page
  42. err = c.Status(code).Render("pages/error", fiber.Map{"Title": "Error", "Error": err})
  43. if err != nil {
  44. // In case the SendFile fails
  45. return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
  46. }
  47. // Return from handler
  48. return nil
  49. },
  50. })
  51. server.Use(logger.New())
  52. server.Use(cache.New(
  53. cache.Config{
  54. Next: func(c *fiber.Ctx) bool {
  55. resp_code := c.Response().StatusCode()
  56. if resp_code < 200 || resp_code >= 300 {
  57. return true
  58. }
  59. if c.Path() == "/" {
  60. return true
  61. }
  62. // Disable cache for settings page
  63. if strings.Contains(c.Path(), "/settings") {
  64. return true
  65. }
  66. return false
  67. },
  68. Expiration: 5 * time.Minute,
  69. CacheControl: true,
  70. KeyGenerator: func(c *fiber.Ctx) string {
  71. return utils.CopyString(c.OriginalURL())
  72. },
  73. },
  74. ))
  75. server.Use(recover.New())
  76. server.Use(compress.New(compress.Config{
  77. Level: compress.LevelBestSpeed, // 1
  78. }))
  79. // Global headers (from GotHub)
  80. server.Use(func(c *fiber.Ctx) error {
  81. c.Set("X-Frame-Options", "SAMEORIGIN")
  82. c.Set("X-XSS-Protection", "1; mode=block")
  83. c.Set("X-Content-Type-Options", "nosniff")
  84. c.Set("Referrer-Policy", "no-referrer")
  85. c.Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
  86. return c.Next()
  87. })
  88. server.Use(func(c *fiber.Ctx) error {
  89. var baseURL string
  90. if configs.BaseURL != "localhost" {
  91. baseURL = "https://" + configs.BaseURL
  92. }
  93. c.Bind(fiber.Map{"FullURL": baseURL + c.OriginalURL(), "BaseURL": baseURL})
  94. return c.Next()
  95. })
  96. // Static files
  97. server.Static("/favicon.ico", "./template/favicon.ico")
  98. server.Static("css/", "./template/css")
  99. server.Static("assets/", "./template/assets")
  100. server.Static("/robots.txt", "./template/robots.txt")
  101. // Routes/Views
  102. views.SetupRoutes(server)
  103. // Disable trusted proxies since we do not use any for now
  104. // server.SetTrustedProxies(nil)
  105. return server
  106. }
  107. func main() {
  108. err := configs.ParseConfig()
  109. configs.SetupStorage()
  110. if err != nil {
  111. panic(err)
  112. }
  113. r := setup_router()
  114. if strings.Contains(configs.Port, "/") {
  115. ln, err := net.Listen("unix", configs.Port)
  116. if err != nil {
  117. panic("Failed to listen to " + configs.Port)
  118. }
  119. r.Listener(ln)
  120. }
  121. println("PixivFE is up and running on port " + configs.Port + "!")
  122. r.Listen(":" + configs.Port)
  123. }