38 lines
720 B
Go
38 lines
720 B
Go
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
|
|
}
|
|
|
|
func New() Server {
|
|
s := &server{}
|
|
|
|
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) {
|
|
handler(ctx)
|
|
})
|
|
}
|