Improved API
This commit is contained in:
63
Request.go
Normal file
63
Request.go
Normal file
@ -0,0 +1,63 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Request is an interface for HTTP requests.
|
||||
type Request interface {
|
||||
Context() context.Context
|
||||
Header(key string) string
|
||||
Host() string
|
||||
Method() string
|
||||
Path() string
|
||||
Protocol() string
|
||||
Read([]byte) (int, error)
|
||||
Scheme() string
|
||||
}
|
||||
|
||||
// request represents the HTTP request used in the given context.
|
||||
type request struct {
|
||||
*http.Request
|
||||
}
|
||||
|
||||
// Context returns the request context.
|
||||
func (req request) Context() context.Context {
|
||||
return req.Request.Context()
|
||||
}
|
||||
|
||||
// Header returns the header value for the given key.
|
||||
func (req request) Header(key string) string {
|
||||
return req.Request.Header.Get(key)
|
||||
}
|
||||
|
||||
// Method returns the request method.
|
||||
func (req request) Method() string {
|
||||
return req.Request.Method
|
||||
}
|
||||
|
||||
// Protocol returns the request protocol.
|
||||
func (req request) Protocol() string {
|
||||
return req.Request.Proto
|
||||
}
|
||||
|
||||
// Host returns the requested host.
|
||||
func (req request) Host() string {
|
||||
return req.Request.Host
|
||||
}
|
||||
|
||||
// Path returns the requested path.
|
||||
func (req request) Path() string {
|
||||
return req.Request.URL.Path
|
||||
}
|
||||
|
||||
// // Read implements the io.Reader interface and reads the request body.
|
||||
func (req request) Read(buffer []byte) (int, error) {
|
||||
return req.Request.Body.Read(buffer)
|
||||
}
|
||||
|
||||
// Scheme returns either `http` or `https`.
|
||||
func (req request) Scheme() string {
|
||||
return req.Request.URL.Scheme
|
||||
}
|
Reference in New Issue
Block a user