middleware

package
v0.0.0-...-f356f3c Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

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

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) AuthorizeAccess

func (rs *Resource) AuthorizeAccess(resourceCode, act string) func(next http.Handler) http.Handler

AuthorizeAccess is a middleware to enforce function's access control of the requester

this method should be called after SessionCtx()

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

Jump to

Keyboard shortcuts

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