middleware

package
v0.0.0-...-b8d2545 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SessionKey is the context key to store JWT private claims which is captured from the request
	SessionKey             = "session"
	PublicFunctionKey      = "public-function"
	RequestFunctionNameKey = "function-name"
)
View Source
const (
	RequestId = "X-Request-Id"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Resource

type Resource struct {
	// contains filtered or unexported fields
}

func NewMiddleware

func NewMiddleware(utility utility, session session) *Resource

NewMiddleware creates a new middleware

func (*Resource) Authenticator

func (rs *Resource) Authenticator() func(http.Handler) http.Handler

func (*Resource) AuthorizeResolver

func (rs *Resource) AuthorizeResolver(act string) func(next http.Handler) http.Handler

AuthorizeResolver is a middleware to manage resolver function's access control based on the captured session

this method should be called after SessionCtx()

func (*Resource) GraphQueryReader

func (rs *Resource) GraphQueryReader(whitelistFunctions []string) func(http.Handler) http.Handler

func (*Resource) SessionCtx

func (rs *Resource) SessionCtx(next http.Handler) http.Handler

SessionCtx authorizes session credential

func (*Resource) Timeout

func (rs *Resource) Timeout(timeout time.Duration) func(next http.Handler) http.Handler

Timeout is a middleware that cancels ctx after a given timeout and return a 504 Gateway Timeout error to the client.

It's required that you select the ctx.Done() channel to check for the signal if the context has reached its deadline and return, otherwise the timeout signal will be just ignored.

i.e. a route/handler may look like:

r.Get("/long", func(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	processTime := time.Duration(rand.Intn(4)+1) * time.Second

	select {
	case <-ctx.Done():
		return

	case <-time.After(processTime):
		// The above channel simulates some hard work.
	}

	w.Write([]byte("done"))
})

FYI: it adopted: https://github.com/go-chi/chi/blob/master/middleware/timeout.go

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL