Switch to Fiber cache (closes #63)

This commit is contained in:
video-prize-ranch
2022-10-14 20:27:37 -04:00
parent 1f1d930072
commit 9d65fa95b4
3 changed files with 9 additions and 38 deletions

24
main.go
View File

@@ -3,10 +3,7 @@ package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time"
"codeberg.org/video-prize-ranch/rimgo/pages"
@@ -15,6 +12,7 @@ import (
"codeberg.org/video-prize-ranch/rimgo/views"
"github.com/aymerick/raymond"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cache"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/template/handlebars"
@@ -29,18 +27,6 @@ func main() {
}
utils.LoadConfig()
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)
}
}
}()
}
engine := handlebars.NewFileSystem(http.FS(views.GetFiles()), ".hbs")
engine.AddFunc("noteq", func(a interface{}, b interface{}, options *raymond.Options) interface{} {
@@ -75,7 +61,7 @@ func main() {
app.Use(recover.New(recover.Config{
EnableStackTrace: true,
StackTraceHandler: func (c *fiber.Ctx, e interface{}) {
StackTraceHandler: func(c *fiber.Ctx, e interface{}) {
fmt.Println(e)
},
}))
@@ -86,6 +72,12 @@ func main() {
},
Root: http.FS(static.GetFiles()),
}))
app.Use(cache.New(cache.Config{
Expiration: 30 * time.Minute,
MaxBytes: 25000000,
CacheControl: true,
StoreResponseHeaders: true,
}))
app.Get("/robots.txt", func(c *fiber.Ctx) error {
file, _ := static.GetFiles().ReadFile("robots.txt")