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. In protection mode, it can block and redirect requests according to its IP address or identified user using `Identify()` and `MatchSecurityResponse()` methods during from the request handler.

SDK methods can be called from request handlers by using the request event record. It can be accessed using `sdk.FromContext()` on a request context. The middleware function stores it into the request context.

Usage example:

fn := func(w http.ResponseWriter, r *http.Request) {
	// Get the request 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)
	sqUser.Identify() // Globally associate this user to the current request
	if match, _ := sqUser.MatchSecurityResponse(); match {
		// Return to stop further handling the request and let Sqreen's
		// middleware apply and abort the request.
		return
	}
	// Not blocked.
	fmt.Fprintf(w, "OK")
}
http.Handle("/foo", sqhttp.Middleware(http.HandlerFunc(fn)))

Package sqhttp imports 2 packages (graph). Updated 2019-06-17. Refresh now. Tools for package owners.