overflow_server_app/server/server.go

38 lines
720 B
Go
Raw Normal View History

2017-08-23 09:19:21 +00:00
package server
import (
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
)
type Server interface {
Handler(ctx *fasthttp.RequestCtx)
Route(method, path string, handler RequestHandler)
}
type server struct {
router *fasthttprouter.Router
}
2017-11-23 08:06:44 +00:00
func New() Server {
s := &server{}
2017-08-23 09:19:21 +00:00
s.router = fasthttprouter.New()
return s
}
func (s *server) Handler(ctx *fasthttp.RequestCtx) {
s.router.Handler(ctx)
}
func (s *server) Route(method, path string, handler RequestHandler) {
s.router.Handle(method, path, s.wrapHandler(handler))
}
func (s *server) wrapHandler(handler RequestHandler) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
2017-09-05 06:37:33 +00:00
handler(ctx)
2017-08-23 09:19:21 +00:00
})
}