simplified middleware interface
This commit is contained in:
parent
5bcaa4c89f
commit
b7822c1b50
18
auth/auth.go
18
auth/auth.go
@ -10,8 +10,7 @@ const UserCtxKey = "user"
|
|||||||
|
|
||||||
// Middleware adds authentication handling
|
// Middleware adds authentication handling
|
||||||
func Middleware(userLookup func(int) any) sushi.Middleware {
|
func Middleware(userLookup func(int) any) sushi.Middleware {
|
||||||
return func(next sushi.Handler) sushi.Handler {
|
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||||
return func(ctx sushi.Ctx, params []string) {
|
|
||||||
sess := session.GetCurrentSession(ctx)
|
sess := session.GetCurrentSession(ctx)
|
||||||
if sess != nil && sess.UserID > 0 && userLookup != nil {
|
if sess != nil && sess.UserID > 0 && userLookup != nil {
|
||||||
user := userLookup(sess.UserID)
|
user := userLookup(sess.UserID)
|
||||||
@ -22,8 +21,7 @@ func Middleware(userLookup func(int) any) sushi.Middleware {
|
|||||||
session.StoreSession(sess)
|
session.StoreSession(sess)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
next(ctx, params)
|
next()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,14 +32,12 @@ func RequireAuth(redirectPath ...string) sushi.Middleware {
|
|||||||
redirect = redirectPath[0]
|
redirect = redirectPath[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(next sushi.Handler) sushi.Handler {
|
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||||
return func(ctx sushi.Ctx, params []string) {
|
|
||||||
if !IsAuthenticated(ctx) {
|
if !IsAuthenticated(ctx) {
|
||||||
ctx.Redirect(redirect, fasthttp.StatusFound)
|
ctx.Redirect(redirect, fasthttp.StatusFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
next(ctx, params)
|
next()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,14 +48,12 @@ func RequireGuest(redirectPath ...string) sushi.Middleware {
|
|||||||
redirect = redirectPath[0]
|
redirect = redirectPath[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return func(next sushi.Handler) sushi.Handler {
|
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||||
return func(ctx sushi.Ctx, params []string) {
|
|
||||||
if IsAuthenticated(ctx) {
|
if IsAuthenticated(ctx) {
|
||||||
ctx.Redirect(redirect, fasthttp.StatusFound)
|
ctx.Redirect(redirect, fasthttp.StatusFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
next(ctx, params)
|
next()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,8 +119,7 @@ func ValidateFormCSRFToken(ctx sushi.Ctx) bool {
|
|||||||
|
|
||||||
// Middleware returns middleware that automatically validates CSRF tokens
|
// Middleware returns middleware that automatically validates CSRF tokens
|
||||||
func Middleware() sushi.Middleware {
|
func Middleware() sushi.Middleware {
|
||||||
return func(next sushi.Handler) sushi.Handler {
|
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||||
return func(ctx sushi.Ctx, params []string) {
|
|
||||||
method := string(ctx.Method())
|
method := string(ctx.Method())
|
||||||
|
|
||||||
if method == "POST" || method == "PUT" || method == "PATCH" || method == "DELETE" {
|
if method == "POST" || method == "PUT" || method == "PATCH" || method == "DELETE" {
|
||||||
@ -132,7 +131,6 @@ func Middleware() sushi.Middleware {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
next(ctx, params)
|
next()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
23
router.go
23
router.go
@ -141,10 +141,27 @@ func (r *Router) methodNode(method string) *node {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func applyMiddleware(h Handler, mw []Middleware) Handler {
|
func applyMiddleware(h Handler, mw []Middleware) Handler {
|
||||||
for i := len(mw) - 1; i >= 0; i-- {
|
if len(mw) == 0 {
|
||||||
h = mw[i](h)
|
|
||||||
}
|
|
||||||
return h
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(ctx Ctx, params []string) {
|
||||||
|
var index int
|
||||||
|
var next func()
|
||||||
|
|
||||||
|
next = func() {
|
||||||
|
if index >= len(mw) {
|
||||||
|
h(ctx, params)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
currentMW := mw[index]
|
||||||
|
index++
|
||||||
|
currentMW(ctx, params, next)
|
||||||
|
}
|
||||||
|
|
||||||
|
next()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func readSegment(path string, start int) (segment string, end int, hasMore bool) {
|
func readSegment(path string, start int) (segment string, end int, hasMore bool) {
|
||||||
|
@ -4,8 +4,7 @@ import sushi "git.sharkk.net/Sharkk/Sushi"
|
|||||||
|
|
||||||
// Middleware provides session handling
|
// Middleware provides session handling
|
||||||
func Middleware() sushi.Middleware {
|
func Middleware() sushi.Middleware {
|
||||||
return func(next sushi.Handler) sushi.Handler {
|
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||||
return func(ctx sushi.Ctx, params []string) {
|
|
||||||
sessionID := sushi.GetCookie(ctx, SessionCookieName)
|
sessionID := sushi.GetCookie(ctx, SessionCookieName)
|
||||||
var sess *Session
|
var sess *Session
|
||||||
|
|
||||||
@ -24,7 +23,6 @@ func Middleware() sushi.Middleware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.SetUserValue(SessionCtxKey, sess)
|
ctx.SetUserValue(SessionCtxKey, sess)
|
||||||
next(ctx, params)
|
next()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,12 +11,10 @@ const RequestTimerKey = "request_start_time"
|
|||||||
|
|
||||||
// Middleware adds request timing functionality
|
// Middleware adds request timing functionality
|
||||||
func Middleware() sushi.Middleware {
|
func Middleware() sushi.Middleware {
|
||||||
return func(next sushi.Handler) sushi.Handler {
|
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||||
return func(ctx sushi.Ctx, params []string) {
|
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
ctx.SetUserValue(RequestTimerKey, startTime)
|
ctx.SetUserValue(RequestTimerKey, startTime)
|
||||||
next(ctx, params)
|
next()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
types.go
2
types.go
@ -4,4 +4,4 @@ import "github.com/valyala/fasthttp"
|
|||||||
|
|
||||||
type Ctx = *fasthttp.RequestCtx
|
type Ctx = *fasthttp.RequestCtx
|
||||||
type Handler func(ctx Ctx, params []string)
|
type Handler func(ctx Ctx, params []string)
|
||||||
type Middleware func(Handler) Handler
|
type Middleware func(ctx Ctx, params []string, next func())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user