github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/demo/web/volts_middleware/main.go (about) 1 package main 2 3 import ( 4 "github.com/volts-dev/volts" 5 "github.com/volts-dev/volts-middleware/event" 6 "github.com/volts-dev/volts/router" 7 "github.com/volts-dev/volts/server" 8 ) 9 10 type ( 11 ctrls struct { 12 event.TEvent 13 //这里写中间件 14 } 15 ) 16 17 //func (action ctrls) Init(router *server.TRouter) { 18 // router.Server().Logger().Info("Init") 19 //} 20 21 func (action ctrls) index(hd *router.THttpContext) { 22 hd.Info("Middleware") 23 hd.Respond([]byte("Middleware")) 24 } 25 26 func (action ctrls) Before(hd *router.THttpContext) { 27 hd.Info("Before") 28 hd.Respond([]byte("Before")) 29 } 30 31 func (action ctrls) After(hd *router.THttpContext) { 32 hd.Info("After") 33 hd.Respond([]byte("After")) 34 } 35 36 func (action ctrls) Panic(hd *router.THttpContext) { 37 hd.Info("Panic") 38 hd.Respond([]byte("Panic")) 39 } 40 41 func main() { 42 r := router.New() 43 //r.RegisterMiddleware(event.NewEvent()) 44 r.Url("GET", "/", ctrls.index) 45 46 srv := server.New( 47 server.WithRouter(r), 48 ) 49 50 // serve as a http server 51 app := volts.New(volts.Server(srv)) 52 app.Run() 53 }