LogoPly
Dependency injection

Context

The request context used in Ply handlers.

Configure Context

Ply passes a Context object to every request handler. It wraps the low-level fasthttp context and provides helpers for request parsing, responses, handler chaining and access to the built-in engines.

Core capabilities

  • Access to the request and response through fasthttp
  • Handler chaining with Next() and Abort()
  • Session-like key-value storage
  • Route and query parsing helpers
  • Access to the pub/sub and queue engines
  • Per-request caching through c.Cache.Memory
  • Async and parallel execution

Basic usage

func HelloHandler(c *core.Context) {
    name := c.Query("name")
    c.Text(200, "Hello, " + name)
}

Built-in helpers

MethodDescription
c.JSON()Respond with JSON
c.Text()Respond with plain text
c.Query()Get a query parameter from the URL
c.Param()Get a dynamic route parameter
c.BindJSON()Parse a JSON body into a struct
c.Header()Get a request header
c.SetSession()Store a key in the session map
c.GetSession()Retrieve a value from the session

Handler control

func ExtractUserID(c *core.Context) {
    userID := c.Param("id")
    fmt.Println("Processing user ID:", userID)
    c.Next()
}
  • c.Next() continues to the next handler.
  • c.Abort() stops chain execution.
  • c.Aborted() reports whether the chain was aborted.

Queue and pub/sub access

c.MustQueue().EnqueueAll("user:welcome-email", data)
c.MustPubsub().PublishAll("user.created", data)
  • MustQueue and MustPubsub panic when the engine is not set.
  • Use SetQueue and SetPubsub to inject the engines.

Caching

key := "my-key"
val, found := ctx.Cache.Memory.Get(key)
if found {
    fmt.Println("Cache hit:", key)
    ctx.Text(200, "From cache: "+string(val))
    return
}

fmt.Println("Cache miss:", key)
result := "Hello, Ply!"
ctx.Cache.Memory.Set(key, []byte(result), 10*time.Second)
ctx.Text(200, "Generated: "+result)
  • Use ctx.Cache.Memory.Set and ctx.Cache.Memory.Get to store and read values.
  • Enable the cache with .WithCache() when creating the app.

Concurrency utilities

c.Async(func() {
    log.Println("non-blocking job")
})

c.Parallel(
    func() { log.Println("task 1") },
    func() { log.Println("task 2") },
)
  • Async() runs the function in a non-blocking goroutine.
  • Parallel() runs multiple functions and waits for them.

Session storage

c.SetSession("userID", "123")
id := c.GetSession("userID")

The session is a key-value map scoped to the request. It is intended for small values such as a user ID or token.

Use cases

  • HTTP request handling
  • Middleware logic
  • Queue and pub/sub integration
  • Controlled asynchronous work