diff --git a/backend/handlers/projects.go b/backend/handlers/projects.go index 2c67c70..94f0eed 100644 --- a/backend/handlers/projects.go +++ b/backend/handlers/projects.go @@ -23,29 +23,27 @@ func projectsRouter(router fiber.Router) error { return nil } -var projectsLastEdit time.Time; +var projectsLastEdit time.Time = time.Now().Truncate(time.Second); func GetAllProjects(c *fiber.Ctx) error { - isCached, err := utils.Cache(c, projectsLastEdit); + isCached, err := utils.Cache(c, &projectsLastEdit); if err == nil && isCached { return nil; } projects, err := db.GetAllProjects() - currentTime := time.Now(); if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": "Cannot retrieve projects", "trace": fmt.Sprint(err), }) } - + err = c.Status(fiber.StatusOK).JSON(projects) if err != nil { return err; } - projectsLastEdit = currentTime; return nil; } @@ -83,6 +81,8 @@ func CreateProject(c *fiber.Ctx) error { }) } + projectsLastEdit = time.Now().Truncate(time.Second); + return c.Status(fiber.StatusCreated).JSON(fiber.Map{ "id": id, }) @@ -111,6 +111,8 @@ func UpdateProject(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } + projectsLastEdit = time.Now().Truncate(time.Second); + return c.SendStatus(fiber.StatusNoContent) } @@ -132,6 +134,8 @@ func DeleteProject(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusNotFound) } + projectsLastEdit = time.Now().Truncate(time.Second); + return c.SendStatus(fiber.StatusNoContent) } diff --git a/backend/main.go b/backend/main.go index a8a9727..b0223a3 100644 --- a/backend/main.go +++ b/backend/main.go @@ -33,7 +33,7 @@ func main() { app.Use(cors.New(cors.Config{ AllowOrigins: origins, AllowMethods: "GET,POST,PUT,DELETE", - AllowHeaders: "Origin, Content-Type, Accept", + AllowHeaders: "Origin, Content-Type, Accept, Cache-Control, Pragma,Expires, If-Modified-Since", })) // app.Use(cache.New()) diff --git a/backend/utils/cache.go b/backend/utils/cache.go index 280cb64..f083470 100644 --- a/backend/utils/cache.go +++ b/backend/utils/cache.go @@ -6,16 +6,22 @@ import ( "github.com/gofiber/fiber/v2" ) -func Cache(c *fiber.Ctx, lastEdit time.Time) (bool, error) { - clientLast, err := time.Parse(time.RFC1123, c.Get("If-Modified-Since")) +func Cache(c *fiber.Ctx, lastEdit *time.Time) (bool, error) { + ifModifiedSince := c.Get("If-Modified-Since") + if ifModifiedSince == "" { + return false, nil + } + + clientLast, err := time.Parse(time.RFC1123, ifModifiedSince) if err != nil { return false, err } - - if clientLast.Before(lastEdit) { + + if !lastEdit.After(clientLast) { c.SendStatus(fiber.StatusNotModified) return true, nil } + c.Set("Last-Modified", lastEdit.Format(time.RFC1123)) return false, nil } \ No newline at end of file