Added router benchmarks
This commit is contained in:
13
Server.go
13
Server.go
@ -8,9 +8,6 @@ import (
|
||||
"git.akyoto.dev/go/router"
|
||||
)
|
||||
|
||||
// Handler is a function that deals with the given request/response context.
|
||||
type Handler func(Context) error
|
||||
|
||||
// Server represents a single web service.
|
||||
type Server struct {
|
||||
router *router.Router[Handler]
|
||||
@ -43,15 +40,21 @@ func (server *Server) Put(path string, handler Handler) {
|
||||
server.router.Add(http.MethodPut, path, handler)
|
||||
}
|
||||
|
||||
// Router returns the router used by the server.
|
||||
func (server *Server) Router() *router.Router[Handler] {
|
||||
return server.router
|
||||
}
|
||||
|
||||
// ServeHTTP responds to the given request.
|
||||
func (server *Server) ServeHTTP(response http.ResponseWriter, request *http.Request) {
|
||||
ctx := newContext(request, response)
|
||||
defer contextPool.Put(ctx)
|
||||
|
||||
handler := server.router.LookupNoAlloc(request.Method, request.URL.Path, ctx.addParameter)
|
||||
|
||||
if handler == nil {
|
||||
response.WriteHeader(http.StatusNotFound)
|
||||
response.(io.StringWriter).WriteString(http.StatusText(http.StatusNotFound))
|
||||
contextPool.Put(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
@ -61,6 +64,4 @@ func (server *Server) ServeHTTP(response http.ResponseWriter, request *http.Requ
|
||||
response.(io.StringWriter).WriteString(err.Error())
|
||||
log.Println(request.URL, err)
|
||||
}
|
||||
|
||||
contextPool.Put(ctx)
|
||||
}
|
||||
|
Reference in New Issue
Block a user