diff --git a/api/cache.go b/api/cache.go index d1e427b..1c36c01 100644 --- a/api/cache.go +++ b/api/cache.go @@ -1,11 +1,30 @@ package api import ( + "fmt" "net/http" + "git.dev-null.rocks/alexohneander/distributed-cache/pkg/cache" "github.com/labstack/echo/v4" ) -func GetItem(c echo.Context) error { - return c.String(http.StatusOK, "") +func GetItem(cache *cache.Cache) echo.HandlerFunc { + return func(c echo.Context) error { + cachedObj, found := cache.Get("key") + + resp := "" + + if found { + resp = fmt.Sprintf("%+v", cachedObj) + } + + return c.String(http.StatusOK, resp) + } +} + +func PostItem(cache *cache.Cache) echo.HandlerFunc { + return func(c echo.Context) error { + cache.Set("key", "value") + return c.String(http.StatusCreated, "") + } } diff --git a/go.mod b/go.mod index fc048f1..b20898d 100644 --- a/go.mod +++ b/go.mod @@ -14,4 +14,5 @@ require ( golang.org/x/net v0.40.0 // indirect golang.org/x/sys v0.33.0 // indirect golang.org/x/text v0.25.0 // indirect + golang.org/x/time v0.11.0 // indirect ) diff --git a/go.sum b/go.sum index b92f49c..de5aaba 100644 --- a/go.sum +++ b/go.sum @@ -25,5 +25,7 @@ golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4= golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA= +golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0= +golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/server/server.go b/internal/server/server.go index 5393f5c..2ee7384 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -4,17 +4,26 @@ import ( "net/http" "git.dev-null.rocks/alexohneander/distributed-cache/api" + "git.dev-null.rocks/alexohneander/distributed-cache/pkg/cache" "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" ) func Start() { e := echo.New() + e.Use(middleware.Logger()) + + // Initialize Cache + cache := cache.NewCache() // Routes e.GET("/", func(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") }) - e.GET("/api/v1/cache/:id", api.GetItem) + + // api/v1/cache + e.GET("/api/v1/cache/:id", api.GetItem(cache)) + e.POST("/api/v1/cache", api.PostItem(cache)) e.Logger.Fatal(e.Start(":8080")) }