rimgo/main.go

111 lines
3.0 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"
"log"
2022-01-17 20:23:04 +00:00
"net/http"
"os"
"path/filepath"
"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-01-17 20:23:04 +00:00
"github.com/gofiber/fiber/v2"
"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
if utils.Config.ImageCache {
go func() {
for range time.Tick(utils.Config.CleanupInterval) {
log.Println("Cache cleaned")
files, _ := filepath.Glob(filepath.Join(utils.Config.CacheDir, "*"))
for _, file := range files {
os.RemoveAll(file)
}
}
}()
}
2022-01-17 20:23:04 +00:00
engine := handlebars.NewFileSystem(http.FS(views.GetFiles()), ".hbs")
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,
StackTraceHandler: func (c *fiber.Ctx, e interface{}) {
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-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
}