rimgo/main.go

136 lines
3.6 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"
2023-06-10 16:04:29 +00:00
"os"
"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"
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()
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")
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)
},
}))
2023-06-10 16:04:29 +00:00
if os.Getenv("ENV") == "dev" {
app.Use("/static", filesystem.New(filesystem.Config{
MaxAge: 2592000,
Root: http.Dir("./static"),
}))
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{
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)
2023-06-10 16:04:29 +00:00
app.Get("/about", pages.HandleAbout)
app.Get("/privacy", pages.HandlePrivacy)
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)
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)
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)
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
app.Listen(utils.Config.Addr + ":" + utils.Config.Port)
2022-01-17 22:34:05 +00:00
}