feat: initialize cache and use it in context
This commit is contained in:
23
api/cache.go
23
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, "")
|
||||
}
|
||||
}
|
||||
|
1
go.mod
1
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
|
||||
)
|
||||
|
2
go.sum
2
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=
|
||||
|
@@ -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"))
|
||||
}
|
||||
|
Reference in New Issue
Block a user