2022-01-17 20:23:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-04 23:41:55 +00:00
|
|
|
"flag"
|
2022-05-31 01:33:44 +00:00
|
|
|
"fmt"
|
2022-01-17 20:23:04 +00:00
|
|
|
"net/http"
|
2023-06-10 16:04:29 +00:00
|
|
|
"os"
|
2022-09-30 20:57:18 +00:00
|
|
|
"time"
|
2022-01-17 20:23:04 +00:00
|
|
|
|
2023-06-10 16:27:20 +00:00
|
|
|
"codeberg.org/rimgo/rimgo/pages"
|
|
|
|
"codeberg.org/rimgo/rimgo/static"
|
|
|
|
"codeberg.org/rimgo/rimgo/utils"
|
|
|
|
"codeberg.org/rimgo/rimgo/views"
|
2022-10-13 21:29:05 +00:00
|
|
|
"github.com/aymerick/raymond"
|
2022-01-17 20:23:04 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2022-10-15 00:27:37 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/cache"
|
2022-01-17 20:23:04 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/filesystem"
|
2022-04-22 15:38:09 +00:00
|
|
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
2023-08-10 15:02:51 +00:00
|
|
|
"github.com/gofiber/template/handlebars/v2"
|
2022-08-04 23:41:55 +00:00
|
|
|
"github.com/joho/godotenv"
|
2022-01-17 20:23:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-08-04 23:41:55 +00:00
|
|
|
envPath := flag.String("c", ".env", "Path to env file")
|
2023-07-19 23:54:12 +00:00
|
|
|
godotenv.Load(*envPath)
|
2022-04-22 15:55:53 +00:00
|
|
|
utils.LoadConfig()
|
2023-08-14 22:47:59 +00:00
|
|
|
|
2023-01-01 20:12:03 +00:00
|
|
|
pages.InitializeApiClient()
|
2022-01-17 22:34:05 +00:00
|
|
|
|
2023-06-10 16:04:29 +00:00
|
|
|
views := http.FS(views.GetFiles())
|
|
|
|
if os.Getenv("ENV") == "dev" {
|
|
|
|
views = http.Dir("./views")
|
|
|
|
}
|
|
|
|
engine := handlebars.NewFileSystem(views, ".hbs")
|
2023-08-14 22:47:59 +00:00
|
|
|
|
2022-10-13 21:29:05 +00:00
|
|
|
engine.AddFunc("noteq", func(a interface{}, b interface{}, options *raymond.Options) interface{} {
|
|
|
|
if raymond.Str(a) != raymond.Str(b) {
|
|
|
|
return options.Fn()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
})
|
|
|
|
|
2022-01-17 20:23:04 +00:00
|
|
|
app := fiber.New(fiber.Config{
|
2022-01-17 22:34:05 +00:00
|
|
|
Views: engine,
|
2022-09-30 20:57:18 +00:00
|
|
|
Prefork: utils.Config.FiberPrefork,
|
2022-01-17 22:34:05 +00:00
|
|
|
UnescapePath: true,
|
2022-01-17 20:23:04 +00:00
|
|
|
StreamRequestBody: true,
|
2022-09-13 20:31:55 +00:00
|
|
|
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
|
|
|
|
code := fiber.StatusInternalServerError
|
|
|
|
|
|
|
|
if e, ok := err.(*fiber.Error); ok {
|
|
|
|
code = e.Code
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ctx.Status(code).Render("errors/error", fiber.Map{
|
|
|
|
"err": err,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2022-01-17 20:23:04 +00:00
|
|
|
})
|
|
|
|
|
2022-05-31 01:33:44 +00:00
|
|
|
app.Use(recover.New(recover.Config{
|
|
|
|
EnableStackTrace: true,
|
2022-10-15 00:27:37 +00:00
|
|
|
StackTraceHandler: func(c *fiber.Ctx, e interface{}) {
|
2022-05-31 01:33:44 +00:00
|
|
|
fmt.Println(e)
|
|
|
|
},
|
|
|
|
}))
|
2023-08-14 22:47:59 +00:00
|
|
|
|
2023-06-10 16:04:29 +00:00
|
|
|
if os.Getenv("ENV") == "dev" {
|
|
|
|
app.Use("/static", filesystem.New(filesystem.Config{
|
|
|
|
MaxAge: 2592000,
|
2023-08-14 22:47:59 +00:00
|
|
|
Root: http.Dir("./static"),
|
2023-06-10 16:04:29 +00:00
|
|
|
}))
|
|
|
|
app.Get("/errors/429", func(c *fiber.Ctx) error {
|
|
|
|
return c.Render("errors/429", nil)
|
|
|
|
})
|
|
|
|
app.Get("/errors/404", func(c *fiber.Ctx) error {
|
|
|
|
return c.Render("errors/404", nil)
|
|
|
|
})
|
|
|
|
app.Get("/errors/error", func(c *fiber.Ctx) error {
|
|
|
|
return c.Render("errors/error", fiber.Map{
|
|
|
|
"err": "Test error",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
app.Use("/static", filesystem.New(filesystem.Config{
|
|
|
|
Root: http.FS(static.GetFiles()),
|
|
|
|
}))
|
|
|
|
app.Use(cache.New(cache.Config{
|
2023-08-14 22:47:59 +00:00
|
|
|
Expiration: 30 * time.Minute,
|
|
|
|
MaxBytes: 25000000,
|
2023-08-10 15:02:51 +00:00
|
|
|
KeyGenerator: func(c *fiber.Ctx) string {
|
2023-08-14 22:47:59 +00:00
|
|
|
return c.OriginalURL()
|
|
|
|
},
|
2023-06-10 16:04:29 +00:00
|
|
|
CacheControl: true,
|
|
|
|
StoreResponseHeaders: true,
|
|
|
|
}))
|
|
|
|
}
|
2022-01-17 20:23:04 +00:00
|
|
|
|
2022-01-21 23:35:12 +00:00
|
|
|
app.Get("/robots.txt", func(c *fiber.Ctx) error {
|
|
|
|
file, _ := static.GetFiles().ReadFile("robots.txt")
|
|
|
|
_, err := c.Write(file)
|
|
|
|
return err
|
|
|
|
})
|
2022-04-10 15:24:54 +00:00
|
|
|
app.Get("/favicon.ico", func(c *fiber.Ctx) error {
|
|
|
|
file, _ := static.GetFiles().ReadFile("favicon/favicon.ico")
|
|
|
|
_, err := c.Write(file)
|
|
|
|
return err
|
|
|
|
})
|
2022-01-21 23:35:12 +00:00
|
|
|
|
2022-02-23 00:06:39 +00:00
|
|
|
app.Get("/", pages.HandleFrontpage)
|
2023-06-10 16:04:29 +00:00
|
|
|
app.Get("/about", pages.HandleAbout)
|
2022-08-04 23:41:55 +00:00
|
|
|
app.Get("/privacy", pages.HandlePrivacy)
|
2023-08-10 15:02:51 +00:00
|
|
|
app.Get("/search", pages.HandleSearch)
|
2023-08-14 22:47:59 +00:00
|
|
|
app.Get("/trending", pages.HandleTrending)
|
2022-05-23 15:10:50 +00:00
|
|
|
app.Get("/a/:postID", pages.HandlePost)
|
2022-07-16 19:59:06 +00:00
|
|
|
app.Get("/a/:postID/embed", pages.HandleEmbed)
|
2022-02-18 21:56:56 +00:00
|
|
|
app.Get("/t/:tag", pages.HandleTag)
|
2023-06-29 22:23:14 +00:00
|
|
|
app.Get("/t/:tag/:postID", pages.HandlePost)
|
2022-01-28 01:41:10 +00:00
|
|
|
app.Get("/user/:userID", pages.HandleUser)
|
2022-02-17 21:07:15 +00:00
|
|
|
app.Get("/r/:sub/:postID", pages.HandlePost)
|
2022-01-28 01:41:10 +00:00
|
|
|
app.Get("/user/:userID/cover", pages.HandleUserCover)
|
|
|
|
app.Get("/user/:userID/avatar", pages.HandleUserAvatar)
|
2022-05-23 15:10:50 +00:00
|
|
|
app.Get("/gallery/:postID", pages.HandlePost)
|
2022-07-16 19:59:06 +00:00
|
|
|
app.Get("/gallery/:postID/embed", pages.HandleEmbed)
|
2023-03-11 18:26:30 +00:00
|
|
|
app.Get("/:postID.gifv", pages.HandleGifv)
|
|
|
|
app.Get("/:baseName.:extension", pages.HandleMedia)
|
|
|
|
app.Get("/stack/:baseName.:extension", pages.HandleMedia)
|
|
|
|
app.Get("/:postID", pages.HandlePost)
|
|
|
|
app.Get("/:postID/embed", pages.HandleEmbed)
|
2022-01-17 20:23:04 +00:00
|
|
|
|
2022-09-30 20:57:18 +00:00
|
|
|
app.Listen(utils.Config.Addr + ":" + utils.Config.Port)
|
2022-01-17 22:34:05 +00:00
|
|
|
}
|