You've already forked openaccounting-server
mirror of
https://github.com/openaccounting/oa-server.git
synced 2025-12-09 00:50:59 +13:00
initial commit
This commit is contained in:
53
vendor/github.com/ant0ine/go-json-rest/rest/if.go
generated
vendored
Normal file
53
vendor/github.com/ant0ine/go-json-rest/rest/if.go
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
package rest
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
// IfMiddleware evaluates at runtime a condition based on the current request, and decides to
|
||||
// execute one of the other Middleware based on this boolean.
|
||||
type IfMiddleware struct {
|
||||
|
||||
// Runtime condition that decides of the execution of IfTrue of IfFalse.
|
||||
Condition func(r *Request) bool
|
||||
|
||||
// Middleware to run when the condition is true. Note that the middleware is initialized
|
||||
// weather if will be used or not. (Optional, pass-through if not set)
|
||||
IfTrue Middleware
|
||||
|
||||
// Middleware to run when the condition is false. Note that the middleware is initialized
|
||||
// weather if will be used or not. (Optional, pass-through if not set)
|
||||
IfFalse Middleware
|
||||
}
|
||||
|
||||
// MiddlewareFunc makes TimerMiddleware implement the Middleware interface.
|
||||
func (mw *IfMiddleware) MiddlewareFunc(h HandlerFunc) HandlerFunc {
|
||||
|
||||
if mw.Condition == nil {
|
||||
log.Fatal("IfMiddleware Condition is required")
|
||||
}
|
||||
|
||||
var ifTrueHandler HandlerFunc
|
||||
if mw.IfTrue != nil {
|
||||
ifTrueHandler = mw.IfTrue.MiddlewareFunc(h)
|
||||
} else {
|
||||
ifTrueHandler = h
|
||||
}
|
||||
|
||||
var ifFalseHandler HandlerFunc
|
||||
if mw.IfFalse != nil {
|
||||
ifFalseHandler = mw.IfFalse.MiddlewareFunc(h)
|
||||
} else {
|
||||
ifFalseHandler = h
|
||||
}
|
||||
|
||||
return func(w ResponseWriter, r *Request) {
|
||||
|
||||
if mw.Condition(r) {
|
||||
ifTrueHandler(w, r)
|
||||
} else {
|
||||
ifFalseHandler(w, r)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user