rimgo/main.go

112 lines
3.1 KiB
Go
Raw Normal View History

2022-01-17 20:23:04 +00:00
package main
import (
"flag"
2022-05-31 01:33:44 +00:00
"fmt"
2022-01-17 20:23:04 +00:00
"net/http"
"time"
2022-01-17 20:23:04 +00:00
2022-01-17 22:34:05 +00:00
"codeberg.org/video-prize-ranch/rimgo/pages"
"codeberg.org/video-prize-ranch/rimgo/static"
2022-04-22 15:55:53 +00:00
"codeberg.org/video-prize-ranch/rimgo/utils"
2022-01-17 22:34:05 +00:00
"codeberg.org/video-prize-ranch/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"
2022-01-17 20:23:04 +00:00
"github.com/gofiber/template/handlebars"
"github.com/joho/godotenv"
2022-01-17 20:23:04 +00:00
)
func main() {
envPath := flag.String("c", ".env", "Path to env file")
err := godotenv.Load(*envPath)
if err != nil {
fmt.Println(err)
}
2022-04-22 15:55:53 +00:00
utils.LoadConfig()
2022-01-17 22:34:05 +00:00
2022-01-17 20:23:04 +00:00
engine := handlebars.NewFileSystem(http.FS(views.GetFiles()), ".hbs")
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,
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)
},
}))
2022-01-17 20:23:04 +00:00
app.Use("/static", filesystem.New(filesystem.Config{
2022-06-18 16:21:22 +00:00
Next: func(c *fiber.Ctx) bool {
c.Response().Header.Add("Cache-Control", "public,max-age=2592000")
return false
},
2022-01-17 20:23:04 +00:00
Root: http.FS(static.GetFiles()),
}))
2022-10-15 00:27:37 +00:00
app.Use(cache.New(cache.Config{
Expiration: 30 * time.Minute,
MaxBytes: 25000000,
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
})
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
app.Get("/", pages.HandleFrontpage)
app.Get("/privacy", pages.HandlePrivacy)
app.Get("/:postID.gifv", pages.HandleGifv)
2022-01-17 22:11:33 +00:00
app.Get("/:baseName.:extension", pages.HandleMedia)
2022-09-30 20:17:24 +00:00
app.Get("/stack/:baseName.:extension", pages.HandleMedia)
app.Get("/:postID", pages.HandlePost)
app.Get("/:postID/embed", pages.HandleEmbed)
2022-05-23 15:10:50 +00:00
app.Get("/a/:postID", pages.HandlePost)
app.Get("/a/:postID/embed", pages.HandleEmbed)
2022-02-18 21:56:56 +00:00
app.Get("/t/:tag", pages.HandleTag)
2022-01-28 01:41:10 +00:00
app.Get("/user/:userID", pages.HandleUser)
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)
app.Get("/gallery/:postID/embed", pages.HandleEmbed)
2022-01-17 20:23:04 +00:00
app.Listen(utils.Config.Addr + ":" + utils.Config.Port)
2022-01-17 22:34:05 +00:00
}