mirror of
https://github.com/bitmagnet-io/bitmagnet.git
synced 2026-07-29 07:23:38 -04:00
58 lines
821 B
Go
58 lines
821 B
Go
package httpserver
|
|
|
|
import (
|
|
"cmp"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Phase int
|
|
|
|
const (
|
|
PhasePre = -1
|
|
PhaseDefault = 0
|
|
PhasePost = 1
|
|
)
|
|
|
|
type Option interface {
|
|
Key() string
|
|
Phase() Phase
|
|
Compare(other Option) int
|
|
Apply(engine *gin.Engine)
|
|
}
|
|
|
|
func NewOption(key string, phase Phase, fn func(engine *gin.Engine)) Option {
|
|
return option{
|
|
key: key,
|
|
phase: phase,
|
|
fn: fn,
|
|
}
|
|
}
|
|
|
|
type option struct {
|
|
key string
|
|
phase Phase
|
|
fn func(engine *gin.Engine)
|
|
}
|
|
|
|
func (o option) Key() string {
|
|
return o.key
|
|
}
|
|
|
|
func (o option) Phase() Phase {
|
|
return o.phase
|
|
}
|
|
|
|
func (o option) Apply(engine *gin.Engine) {
|
|
o.fn(engine)
|
|
}
|
|
|
|
func (o option) Compare(other Option) int {
|
|
result := cmp.Compare(o.Phase(), other.Phase())
|
|
if result != 0 {
|
|
return result
|
|
}
|
|
|
|
return cmp.Compare(o.Key(), other.Key())
|
|
}
|