go-agent: github.com/sqreen/go-agent/sdk/middleware/sqgin Index | Files

package sqgin

import "github.com/sqreen/go-agent/sdk/middleware/sqgin"

Index

Package Files

gin.go

func Middleware Uses

func Middleware() gingonic.HandlerFunc

Middleware is Sqreen's middleware function for Gin to monitor and protect the requests Gin receives. It creates and stores the HTTP request record both into the Gin and request contexts so that it can be later accessed from handlers using `sdk.FromContext()` to perform SDK calls.

Note that Gin's context implements the `context.Context` interface, so `sdk.FromContext()` can be used both with the Gin and request contexts.

router := gin.Default()
router.Use(sqgin.Middleware())

router.GET("/", func(c *gin.Context) {
	// Accessing the SDK through Gin's context
	sdk.FromContext(c).TrackEvent("my.event.one")
	foo(c.Request)
}

func foo(req *http.Request) {
	// Accessing the SDK through the request context
	sdk.FromContext(req.Context()).TrackEvent("my.event.two")
	// ...
}

Package sqgin imports 3 packages (graph). Updated 2019-02-07. Refresh now. Tools for package owners.