overflow_server_app/server/server.go

43 lines
789 B
Go
Raw Normal View History

2017-08-23 09:19:21 +00:00
package server
import (
2017-09-05 06:37:33 +00:00
"context"
2017-08-23 09:19:21 +00:00
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
)
type Server interface {
Handler(ctx *fasthttp.RequestCtx)
Route(method, path string, handler RequestHandler)
}
type server struct {
2017-09-05 06:37:33 +00:00
ctx context.Context
2017-08-23 09:19:21 +00:00
router *fasthttprouter.Router
}
2017-09-05 06:37:33 +00:00
func New(ctx context.Context) Server {
s := &server{
ctx: ctx,
}
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
})
}