@@ -2,14 +2,19 @@ package don
22
33import (
44 "bytes"
5+ "net"
56 "net/http"
67
78 "github.com/abemedia/httprouter"
89 "github.com/valyala/fasthttp"
910)
1011
12+ // Empty is the type used when an API handler contains either no request or no
13+ // response.
1114type Empty struct {}
1215
16+ // DefaultEncoding contains the mime type of the default encoding to fall
17+ // back on if no `Accept` or `Content-Type` header was provided.
1318var DefaultEncoding = "text/plain"
1419
1520type Middleware func (fasthttp.RequestHandler ) fasthttp.RequestHandler
@@ -38,6 +43,8 @@ type API struct {
3843}
3944
4045type Config struct {
46+ // DefaultEncoding contains the mime type of the default encoding to fall
47+ // back on if no `Accept` or `Content-Type` header was provided.
4148 DefaultEncoding string
4249
4350 // DisableNoContent controls whether a nil or zero value response should
@@ -55,10 +62,8 @@ func New(c *Config) *API {
5562 c .DefaultEncoding = DefaultEncoding
5663 }
5764
58- r := httprouter .New ()
59-
6065 return & API {
61- router : r ,
66+ router : httprouter . New () ,
6267 config : c ,
6368 NotFound : E (ErrNotFound ),
6469 MethodNotAllowed : E (ErrMethodNotAllowed ),
@@ -102,7 +107,7 @@ func (r *API) Handler(method, path string, handle http.Handler) {
102107
103108// HandlerFunc is an adapter which allows the usage of an http.HandlerFunc as a request handle.
104109func (r * API ) HandleFunc (method , path string , handle http.HandlerFunc ) {
105- r .Handler (method , path , handle )
110+ r .router . HandlerFunc (method , path , handle )
106111}
107112
108113func (r * API ) Group (path string ) Router {
@@ -124,8 +129,6 @@ func (r *API) RequestHandler() fasthttp.RequestHandler {
124129 h = mw (h )
125130 }
126131
127- anyEncoding := []byte ("*/*" )
128-
129132 return func (ctx * fasthttp.RequestCtx ) {
130133 ct := ctx .Request .Header .ContentType ()
131134 if len (ct ) == 0 || bytes .HasPrefix (ct , anyEncoding ) {
@@ -156,11 +159,20 @@ func (r *API) RequestHandler() fasthttp.RequestHandler {
156159
157160// ListenAndServe serves HTTP requests from the given TCP4 addr.
158161func (r * API ) ListenAndServe (addr string ) error {
159- s := & fasthttp.Server {
162+ return newServer (r ).ListenAndServe (addr )
163+ }
164+
165+ // Serve serves incoming connections from the given listener.
166+ func (r * API ) Serve (ln net.Listener ) error {
167+ return newServer (r ).Serve (ln )
168+ }
169+
170+ func newServer (r * API ) * fasthttp.Server {
171+ return & fasthttp.Server {
160172 Handler : r .RequestHandler (),
161173 StreamRequestBody : true ,
162174 NoDefaultContentType : true ,
163175 }
164-
165- return s .ListenAndServe (addr )
166176}
177+
178+ var anyEncoding = []byte ("*/*" )
0 commit comments