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

package sqhttp

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

Index

Package Files

http.go

func Middleware Uses

func Middleware(next http.Handler) http.Handler

Middleware is Sqreen's middleware function for `net/http` to monitor and protect received requests.

SDK methods can be called from request handlers by using the request context. It can be retrieved from the request context using `sdk.FromContext()`.

Usage example:

fn := func(w http.ResponseWriter, r *http.Request) {
	// Get the requestImplType record.
	sqreen := sdk.FromContext(r.Context())

	// Example of sending a custom event.
	sqreen.TrackEvent("my.event")

	// Example of globally identifying a user and checking if the request
	// should be aborted.
	uid := sdk.EventUserIdentifiersMap{"uid": "my-uid"}
	sqUser := sqreen.ForUser(uid)
	// Globally associate this user to the current request and check if it got
	// blocked.
	if err := sqUser.Identify(); err != nil {
		// Return to stop further handling the request
		return
	}
	// User not blocked
	fmt.Fprintf(w, "OK")
}
http.Handle("/foo", sqhttp.Middleware(http.HandlerFunc(fn)))

Package sqhttp imports 12 packages (graph). Updated 2020-05-21. Refresh now. Tools for package owners.