overflow_server_app/server/server.go
crusader 1639b86706 ing
2017-11-23 17:06:44 +09:00

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)
})
}