mirror of
https://github.com/labstack/echo.git
synced 2025-03-23 21:29:26 +02:00
parent
00bf0d651f
commit
b10c93cd17
22
context.go
22
context.go
@ -26,8 +26,8 @@ type (
|
|||||||
// response objects, path parameters, data and registered handler.
|
// response objects, path parameters, data and registered handler.
|
||||||
Context interface {
|
Context interface {
|
||||||
netContext.Context
|
netContext.Context
|
||||||
SetNetContext(netContext.Context)
|
|
||||||
NetContext() netContext.Context
|
NetContext() netContext.Context
|
||||||
|
SetNetContext(netContext.Context)
|
||||||
Request() engine.Request
|
Request() engine.Request
|
||||||
Response() engine.Response
|
Response() engine.Response
|
||||||
Path() string
|
Path() string
|
||||||
@ -36,8 +36,8 @@ type (
|
|||||||
ParamNames() []string
|
ParamNames() []string
|
||||||
Query(string) string
|
Query(string) string
|
||||||
Form(string) string
|
Form(string) string
|
||||||
Set(string, interface{})
|
|
||||||
Get(string) interface{}
|
Get(string) interface{}
|
||||||
|
Set(string, interface{})
|
||||||
Bind(interface{}) error
|
Bind(interface{}) error
|
||||||
Render(int, string, interface{}) error
|
Render(int, string, interface{}) error
|
||||||
HTML(int, string) error
|
HTML(int, string) error
|
||||||
@ -90,14 +90,14 @@ func NewContext(req engine.Request, res engine.Response, e *Echo) Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) SetNetContext(ctx netContext.Context) {
|
|
||||||
c.netContext = ctx
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *context) NetContext() netContext.Context {
|
func (c *context) NetContext() netContext.Context {
|
||||||
return c.netContext
|
return c.netContext
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *context) SetNetContext(ctx netContext.Context) {
|
||||||
|
c.netContext = ctx
|
||||||
|
}
|
||||||
|
|
||||||
func (c *context) Deadline() (deadline time.Time, ok bool) {
|
func (c *context) Deadline() (deadline time.Time, ok bool) {
|
||||||
return c.netContext.Deadline()
|
return c.netContext.Deadline()
|
||||||
}
|
}
|
||||||
@ -169,11 +169,6 @@ func (c *context) Form(name string) string {
|
|||||||
return c.request.FormValue(name)
|
return c.request.FormValue(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get retrieves data from the context.
|
|
||||||
func (c *context) Get(key string) interface{} {
|
|
||||||
return c.store[key]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set saves data in the context.
|
// Set saves data in the context.
|
||||||
func (c *context) Set(key string, val interface{}) {
|
func (c *context) Set(key string, val interface{}) {
|
||||||
if c.store == nil {
|
if c.store == nil {
|
||||||
@ -182,6 +177,11 @@ func (c *context) Set(key string, val interface{}) {
|
|||||||
c.store[key] = val
|
c.store[key] = val
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get retrieves data from the context.
|
||||||
|
func (c *context) Get(key string) interface{} {
|
||||||
|
return c.store[key]
|
||||||
|
}
|
||||||
|
|
||||||
// Bind binds the request body into specified type `i`. The default binder does
|
// Bind binds the request body into specified type `i`. The default binder does
|
||||||
// it based on Content-Type header.
|
// it based on Content-Type header.
|
||||||
func (c *context) Bind(i interface{}) error {
|
func (c *context) Bind(i interface{}) error {
|
||||||
|
@ -9,16 +9,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Engine defines an interface for HTTP server.
|
// Engine defines the interface for HTTP server.
|
||||||
Engine interface {
|
Engine interface {
|
||||||
SetHandler(Handler)
|
SetHandler(Handler)
|
||||||
SetLogger(*log.Logger)
|
SetLogger(*log.Logger)
|
||||||
Start()
|
Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request defines an interface for HTTP request.
|
// Request defines the interface for HTTP request.
|
||||||
Request interface {
|
Request interface {
|
||||||
|
// TLS returns true if connection is TLS otherwise false.
|
||||||
TLS() bool
|
TLS() bool
|
||||||
|
|
||||||
Scheme() string
|
Scheme() string
|
||||||
Host() string
|
Host() string
|
||||||
URI() string
|
URI() string
|
||||||
@ -27,6 +29,7 @@ type (
|
|||||||
// Proto() string
|
// Proto() string
|
||||||
// ProtoMajor() int
|
// ProtoMajor() int
|
||||||
// ProtoMinor() int
|
// ProtoMinor() int
|
||||||
|
UserAgent() string
|
||||||
RemoteAddress() string
|
RemoteAddress() string
|
||||||
Method() string
|
Method() string
|
||||||
Body() io.ReadCloser
|
Body() io.ReadCloser
|
||||||
@ -35,7 +38,7 @@ type (
|
|||||||
MultipartForm() (*multipart.Form, error)
|
MultipartForm() (*multipart.Form, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response defines an interface for HTTP response.
|
// Response defines the interface for HTTP response.
|
||||||
Response interface {
|
Response interface {
|
||||||
Header() Header
|
Header() Header
|
||||||
WriteHeader(int)
|
WriteHeader(int)
|
||||||
@ -43,22 +46,22 @@ type (
|
|||||||
Status() int
|
Status() int
|
||||||
Size() int64
|
Size() int64
|
||||||
Committed() bool
|
Committed() bool
|
||||||
SetWriter(io.Writer)
|
|
||||||
Writer() io.Writer
|
Writer() io.Writer
|
||||||
|
SetWriter(io.Writer)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Header defines an interface for HTTP header.
|
// Header defines the interface for HTTP header.
|
||||||
Header interface {
|
Header interface {
|
||||||
Add(string, string)
|
Add(string, string)
|
||||||
Del(string)
|
Del(string)
|
||||||
Get(string) string
|
|
||||||
Set(string, string)
|
Set(string, string)
|
||||||
|
Get(string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
// URL defines an interface for HTTP request url.
|
// URL defines the interface for HTTP request url.
|
||||||
URL interface {
|
URL interface {
|
||||||
SetPath(string)
|
|
||||||
Path() string
|
Path() string
|
||||||
|
SetPath(string)
|
||||||
QueryValue(string) string
|
QueryValue(string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,47 +5,57 @@ package fasthttp
|
|||||||
import "github.com/valyala/fasthttp"
|
import "github.com/valyala/fasthttp"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// RequestHeader holds `fasthttp.RequestHeader`.
|
||||||
RequestHeader struct {
|
RequestHeader struct {
|
||||||
*fasthttp.RequestHeader
|
*fasthttp.RequestHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResponseHeader holds `fasthttp.ResponseHeader`.
|
||||||
ResponseHeader struct {
|
ResponseHeader struct {
|
||||||
*fasthttp.ResponseHeader
|
*fasthttp.ResponseHeader
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Add implements `Header#Add` method.
|
||||||
func (h *RequestHeader) Add(key, val string) {
|
func (h *RequestHeader) Add(key, val string) {
|
||||||
// h.RequestHeader.Add(key, val)
|
// h.RequestHeader.Add(key, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Del implements `Header#Del` method.
|
||||||
func (h *RequestHeader) Del(key string) {
|
func (h *RequestHeader) Del(key string) {
|
||||||
h.RequestHeader.Del(key)
|
h.RequestHeader.Del(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RequestHeader) Get(key string) string {
|
// Set implements `Header#Set` method.
|
||||||
return string(h.Peek(key))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *RequestHeader) Set(key, val string) {
|
func (h *RequestHeader) Set(key, val string) {
|
||||||
h.RequestHeader.Set(key, val)
|
h.RequestHeader.Set(key, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get implements `Header#Get` method.
|
||||||
|
func (h *RequestHeader) Get(key string) string {
|
||||||
|
return string(h.Peek(key))
|
||||||
|
}
|
||||||
|
|
||||||
func (h *RequestHeader) reset(hdr *fasthttp.RequestHeader) {
|
func (h *RequestHeader) reset(hdr *fasthttp.RequestHeader) {
|
||||||
h.RequestHeader = hdr
|
h.RequestHeader = hdr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add implements `Header#Add` method.
|
||||||
func (h *ResponseHeader) Add(key, val string) {
|
func (h *ResponseHeader) Add(key, val string) {
|
||||||
// h.header.Add(key, val)
|
// h.header.Add(key, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Del implements `Header#Del` method.
|
||||||
func (h *ResponseHeader) Del(key string) {
|
func (h *ResponseHeader) Del(key string) {
|
||||||
h.ResponseHeader.Del(key)
|
h.ResponseHeader.Del(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get implements `Header#Get` method.
|
||||||
func (h *ResponseHeader) Get(key string) string {
|
func (h *ResponseHeader) Get(key string) string {
|
||||||
return string(h.Peek(key))
|
return string(h.Peek(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set implements `Header#Set` method.
|
||||||
func (h *ResponseHeader) Set(key, val string) {
|
func (h *ResponseHeader) Set(key, val string) {
|
||||||
h.ResponseHeader.Set(key, val)
|
h.ResponseHeader.Set(key, val)
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// Request implements `engine.Request`.
|
||||||
Request struct {
|
Request struct {
|
||||||
*fasthttp.RequestCtx
|
*fasthttp.RequestCtx
|
||||||
url engine.URL
|
url engine.URL
|
||||||
@ -22,58 +23,67 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRequest(c *fasthttp.RequestCtx) *Request {
|
// TLS implements `Request#TLS` method.
|
||||||
return &Request{
|
|
||||||
RequestCtx: c,
|
|
||||||
url: &URL{URI: c.URI()},
|
|
||||||
header: &RequestHeader{&c.Request.Header},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Request) TLS() bool {
|
func (r *Request) TLS() bool {
|
||||||
return r.IsTLS()
|
return r.IsTLS()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Scheme implements `Request#Scheme` method.
|
||||||
func (r *Request) Scheme() string {
|
func (r *Request) Scheme() string {
|
||||||
return string(r.RequestCtx.URI().Scheme())
|
return string(r.RequestCtx.URI().Scheme())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Host implements `Request#Host` method.
|
||||||
func (r *Request) Host() string {
|
func (r *Request) Host() string {
|
||||||
return string(r.RequestCtx.Host())
|
return string(r.RequestCtx.Host())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) URI() string {
|
// URL implements `Request#URL` method.
|
||||||
return string(r.RequestURI())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Request) URL() engine.URL {
|
func (r *Request) URL() engine.URL {
|
||||||
return r.url
|
return r.url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Header implements `Request#Header` method.
|
||||||
func (r *Request) Header() engine.Header {
|
func (r *Request) Header() engine.Header {
|
||||||
return r.header
|
return r.header
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserAgent implements `Request#UserAgent` method.
|
||||||
|
func (r *Request) UserAgent() string {
|
||||||
|
return string(r.RequestCtx.UserAgent())
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoteAddress implements `Request#RemoteAddress` method.
|
||||||
func (r *Request) RemoteAddress() string {
|
func (r *Request) RemoteAddress() string {
|
||||||
return r.RemoteAddr().String()
|
return r.RemoteAddr().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Method implements `Request#Method` method.
|
||||||
func (r *Request) Method() string {
|
func (r *Request) Method() string {
|
||||||
return string(r.RequestCtx.Method())
|
return string(r.RequestCtx.Method())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// URI implements `Request#URI` method.
|
||||||
|
func (r *Request) URI() string {
|
||||||
|
return string(r.RequestURI())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Body implements `Request#Body` method.
|
||||||
func (r *Request) Body() io.ReadCloser {
|
func (r *Request) Body() io.ReadCloser {
|
||||||
return ioutil.NopCloser(bytes.NewBuffer(r.PostBody()))
|
return ioutil.NopCloser(bytes.NewBuffer(r.PostBody()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FormValue implements `Request#FormValue` method.
|
||||||
func (r *Request) FormValue(name string) string {
|
func (r *Request) FormValue(name string) string {
|
||||||
return string(r.RequestCtx.FormValue(name))
|
return string(r.RequestCtx.FormValue(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FormFile implements `Request#FormFile` method.
|
||||||
func (r *Request) FormFile(name string) (*multipart.FileHeader, error) {
|
func (r *Request) FormFile(name string) (*multipart.FileHeader, error) {
|
||||||
return r.RequestCtx.FormFile(name)
|
return r.RequestCtx.FormFile(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MultipartForm implements `Request#MultipartForm` method.
|
||||||
func (r *Request) MultipartForm() (*multipart.Form, error) {
|
func (r *Request) MultipartForm() (*multipart.Form, error) {
|
||||||
return r.RequestCtx.MultipartForm()
|
return r.RequestCtx.MultipartForm()
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// Response implements `engine.Response`.
|
||||||
Response struct {
|
Response struct {
|
||||||
*fasthttp.RequestCtx
|
*fasthttp.RequestCtx
|
||||||
header engine.Header
|
header engine.Header
|
||||||
@ -23,19 +24,12 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewResponse(c *fasthttp.RequestCtx) *Response {
|
// Header implements `Response#Header` method.
|
||||||
return &Response{
|
|
||||||
RequestCtx: c,
|
|
||||||
header: &ResponseHeader{&c.Response.Header},
|
|
||||||
writer: c,
|
|
||||||
logger: log.New("test"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Response) Header() engine.Header {
|
func (r *Response) Header() engine.Header {
|
||||||
return r.header
|
return r.header
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteHeader implements `Response#WriteHeader` method.
|
||||||
func (r *Response) WriteHeader(code int) {
|
func (r *Response) WriteHeader(code int) {
|
||||||
if r.committed {
|
if r.committed {
|
||||||
r.logger.Warn("response already committed")
|
r.logger.Warn("response already committed")
|
||||||
@ -46,30 +40,36 @@ func (r *Response) WriteHeader(code int) {
|
|||||||
r.committed = true
|
r.committed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write implements `Response#Write` method.
|
||||||
func (r *Response) Write(b []byte) (int, error) {
|
func (r *Response) Write(b []byte) (int, error) {
|
||||||
return r.RequestCtx.Write(b)
|
return r.RequestCtx.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Status implements `Response#Status` method.
|
||||||
func (r *Response) Status() int {
|
func (r *Response) Status() int {
|
||||||
return r.status
|
return r.status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Size implements `Response#Size` method.
|
||||||
func (r *Response) Size() int64 {
|
func (r *Response) Size() int64 {
|
||||||
return r.size
|
return r.size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Committed implements `Response#Committed` method.
|
||||||
func (r *Response) Committed() bool {
|
func (r *Response) Committed() bool {
|
||||||
return r.committed
|
return r.committed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) SetWriter(w io.Writer) {
|
// Writer implements `Response#Writer` method.
|
||||||
r.writer = w
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Response) Writer() io.Writer {
|
func (r *Response) Writer() io.Writer {
|
||||||
return r.writer
|
return r.writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetWriter implements `Response#SetWriter` method.
|
||||||
|
func (r *Response) SetWriter(w io.Writer) {
|
||||||
|
r.writer = w
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Response) reset(c *fasthttp.RequestCtx, h engine.Header) {
|
func (r *Response) reset(c *fasthttp.RequestCtx, h engine.Header) {
|
||||||
r.RequestCtx = c
|
r.RequestCtx = c
|
||||||
r.header = h
|
r.header = h
|
||||||
|
@ -12,14 +12,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// Server implements `engine.Engine`.
|
||||||
Server struct {
|
Server struct {
|
||||||
config engine.Config
|
config engine.Config
|
||||||
handler engine.Handler
|
handler engine.Handler
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
pool *Pool
|
pool *pool
|
||||||
}
|
}
|
||||||
|
|
||||||
Pool struct {
|
pool struct {
|
||||||
request sync.Pool
|
request sync.Pool
|
||||||
response sync.Pool
|
response sync.Pool
|
||||||
requestHeader sync.Pool
|
requestHeader sync.Pool
|
||||||
@ -28,11 +29,13 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// New returns an instance of `fasthttp.Server` with specified listen address.
|
||||||
func New(addr string) *Server {
|
func New(addr string) *Server {
|
||||||
c := engine.Config{Address: addr}
|
c := engine.Config{Address: addr}
|
||||||
return NewFromConfig(c)
|
return NewFromConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFromTLS returns an instance of `fasthttp.Server` from TLS config.
|
||||||
func NewFromTLS(addr, certfile, keyfile string) *Server {
|
func NewFromTLS(addr, certfile, keyfile string) *Server {
|
||||||
c := engine.Config{
|
c := engine.Config{
|
||||||
Address: addr,
|
Address: addr,
|
||||||
@ -42,10 +45,11 @@ func NewFromTLS(addr, certfile, keyfile string) *Server {
|
|||||||
return NewFromConfig(c)
|
return NewFromConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFromConfig returns an instance of `standard.Server` from config.
|
||||||
func NewFromConfig(c engine.Config) (s *Server) {
|
func NewFromConfig(c engine.Config) (s *Server) {
|
||||||
s = &Server{
|
s = &Server{
|
||||||
config: c,
|
config: c,
|
||||||
pool: &Pool{
|
pool: &pool{
|
||||||
request: sync.Pool{
|
request: sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return &Request{}
|
return &Request{}
|
||||||
@ -80,14 +84,17 @@ func NewFromConfig(c engine.Config) (s *Server) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetHandler implements `Engine#SetHandler` method.
|
||||||
func (s *Server) SetHandler(h engine.Handler) {
|
func (s *Server) SetHandler(h engine.Handler) {
|
||||||
s.handler = h
|
s.handler = h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetLogger implements `Engine#SetLogger` method.
|
||||||
func (s *Server) SetLogger(l *log.Logger) {
|
func (s *Server) SetLogger(l *log.Logger) {
|
||||||
s.logger = l
|
s.logger = l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start implements `Engine#Start` method.
|
||||||
func (s *Server) Start() {
|
func (s *Server) Start() {
|
||||||
handler := func(c *fasthttp.RequestCtx) {
|
handler := func(c *fasthttp.RequestCtx) {
|
||||||
// Request
|
// Request
|
||||||
|
@ -5,19 +5,23 @@ package fasthttp
|
|||||||
import "github.com/valyala/fasthttp"
|
import "github.com/valyala/fasthttp"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// URL implements `engine.URL`.
|
||||||
URL struct {
|
URL struct {
|
||||||
*fasthttp.URI
|
*fasthttp.URI
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (u *URL) SetPath(path string) {
|
// Path implements `URL#Path` method.
|
||||||
// return string(u.URI.Path())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *URL) Path() string {
|
func (u *URL) Path() string {
|
||||||
return string(u.URI.Path())
|
return string(u.URI.Path())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetPath implements `URL#SetPath` method.
|
||||||
|
func (u *URL) SetPath(path string) {
|
||||||
|
// return string(u.URI.Path())
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryValue implements `URL#QueryValue` method.
|
||||||
func (u *URL) QueryValue(name string) string {
|
func (u *URL) QueryValue(name string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
@ -3,27 +3,32 @@ package standard
|
|||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// Header implements `engine.Header`.
|
||||||
Header struct {
|
Header struct {
|
||||||
http.Header
|
http.Header
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Add implements `Header#Add` method.
|
||||||
func (h *Header) Add(key, val string) {
|
func (h *Header) Add(key, val string) {
|
||||||
h.Header.Add(key, val)
|
h.Header.Add(key, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Del implements `Header#Del` method.
|
||||||
func (h *Header) Del(key string) {
|
func (h *Header) Del(key string) {
|
||||||
h.Header.Del(key)
|
h.Header.Del(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Header) Get(key string) string {
|
// Set implements `Header#Set` method.
|
||||||
return h.Header.Get(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Header) Set(key, val string) {
|
func (h *Header) Set(key, val string) {
|
||||||
h.Header.Set(key, val)
|
h.Header.Set(key, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get implements `Header#Get` method.
|
||||||
|
func (h *Header) Get(key string) string {
|
||||||
|
return h.Header.Get(key)
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Header) reset(hdr http.Header) {
|
func (h *Header) reset(hdr http.Header) {
|
||||||
h.Header = hdr
|
h.Header = hdr
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// Request implements `engine.Request`.
|
||||||
Request struct {
|
Request struct {
|
||||||
*http.Request
|
*http.Request
|
||||||
url engine.URL
|
url engine.URL
|
||||||
@ -16,18 +17,12 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewRequest(r *http.Request) *Request {
|
// TLS implements `Request#TLS` method.
|
||||||
return &Request{
|
|
||||||
Request: r,
|
|
||||||
url: &URL{URL: r.URL},
|
|
||||||
header: &Header{r.Header},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Request) TLS() bool {
|
func (r *Request) TLS() bool {
|
||||||
return r.Request.TLS != nil
|
return r.Request.TLS != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Scheme implements `Request#Scheme` method.
|
||||||
func (r *Request) Scheme() string {
|
func (r *Request) Scheme() string {
|
||||||
if r.TLS() {
|
if r.TLS() {
|
||||||
return "https"
|
return "https"
|
||||||
@ -35,14 +30,17 @@ func (r *Request) Scheme() string {
|
|||||||
return "http"
|
return "http"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Host implements `Request#Host` method.
|
||||||
func (r *Request) Host() string {
|
func (r *Request) Host() string {
|
||||||
return r.Request.Host
|
return r.Request.Host
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// URL implements `Request#URL` method.
|
||||||
func (r *Request) URL() engine.URL {
|
func (r *Request) URL() engine.URL {
|
||||||
return r.url
|
return r.url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Header implements `Request#URL` method.
|
||||||
func (r *Request) Header() engine.Header {
|
func (r *Request) Header() engine.Header {
|
||||||
return r.header
|
return r.header
|
||||||
}
|
}
|
||||||
@ -59,31 +57,43 @@ func (r *Request) Header() engine.Header {
|
|||||||
// return r.request.ProtoMinor()
|
// return r.request.ProtoMinor()
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// UserAgent implements `Request#UserAgent` method.
|
||||||
|
func (r *Request) UserAgent() string {
|
||||||
|
return r.Request.UserAgent()
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoteAddress implements `Request#RemoteAddress` method.
|
||||||
func (r *Request) RemoteAddress() string {
|
func (r *Request) RemoteAddress() string {
|
||||||
return r.RemoteAddr
|
return r.RemoteAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Method implements `Request#Method` method.
|
||||||
func (r *Request) Method() string {
|
func (r *Request) Method() string {
|
||||||
return r.Request.Method
|
return r.Request.Method
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// URI implements `Request#URI` method.
|
||||||
func (r *Request) URI() string {
|
func (r *Request) URI() string {
|
||||||
return r.RequestURI
|
return r.RequestURI
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Body implements `Request#Body` method.
|
||||||
func (r *Request) Body() io.ReadCloser {
|
func (r *Request) Body() io.ReadCloser {
|
||||||
return r.Request.Body
|
return r.Request.Body
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FormValue implements `Request#FormValue` method.
|
||||||
func (r *Request) FormValue(name string) string {
|
func (r *Request) FormValue(name string) string {
|
||||||
return r.Request.FormValue(name)
|
return r.Request.FormValue(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FormFile implements `Request#FormFile` method.
|
||||||
func (r *Request) FormFile(name string) (*multipart.FileHeader, error) {
|
func (r *Request) FormFile(name string) (*multipart.FileHeader, error) {
|
||||||
_, fh, err := r.Request.FormFile(name)
|
_, fh, err := r.Request.FormFile(name)
|
||||||
return fh, err
|
return fh, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MultipartForm implements `Request#MultipartForm` method.
|
||||||
func (r *Request) MultipartForm() (*multipart.Form, error) {
|
func (r *Request) MultipartForm() (*multipart.Form, error) {
|
||||||
r.Request.ParseMultipartForm(32 << 20) // 32 MB
|
r.Request.ParseMultipartForm(32 << 20) // 32 MB
|
||||||
return r.Request.MultipartForm, nil
|
return r.Request.MultipartForm, nil
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// Response implements `engine.Response`.
|
||||||
Response struct {
|
Response struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
header engine.Header
|
header engine.Header
|
||||||
@ -20,19 +21,12 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewResponse(w http.ResponseWriter, l *log.Logger) *Response {
|
// Header implements `Response#Header` method.
|
||||||
return &Response{
|
|
||||||
ResponseWriter: w,
|
|
||||||
header: &Header{w.Header()},
|
|
||||||
writer: w,
|
|
||||||
logger: l,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Response) Header() engine.Header {
|
func (r *Response) Header() engine.Header {
|
||||||
return r.header
|
return r.header
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteHeader implements `Response#WriteHeader` method.
|
||||||
func (r *Response) WriteHeader(code int) {
|
func (r *Response) WriteHeader(code int) {
|
||||||
if r.committed {
|
if r.committed {
|
||||||
r.logger.Warn("response already committed")
|
r.logger.Warn("response already committed")
|
||||||
@ -43,32 +37,38 @@ func (r *Response) WriteHeader(code int) {
|
|||||||
r.committed = true
|
r.committed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write implements `Response#Write` method.
|
||||||
func (r *Response) Write(b []byte) (n int, err error) {
|
func (r *Response) Write(b []byte) (n int, err error) {
|
||||||
n, err = r.writer.Write(b)
|
n, err = r.writer.Write(b)
|
||||||
r.size += int64(n)
|
r.size += int64(n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Status implements `Response#Status` method.
|
||||||
func (r *Response) Status() int {
|
func (r *Response) Status() int {
|
||||||
return r.status
|
return r.status
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Size implements `Response#Size` method.
|
||||||
func (r *Response) Size() int64 {
|
func (r *Response) Size() int64 {
|
||||||
return r.size
|
return r.size
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Committed implements `Response#Committed` method.
|
||||||
func (r *Response) Committed() bool {
|
func (r *Response) Committed() bool {
|
||||||
return r.committed
|
return r.committed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) SetWriter(w io.Writer) {
|
// Writer implements `Response#Writer` method.
|
||||||
r.writer = w
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Response) Writer() io.Writer {
|
func (r *Response) Writer() io.Writer {
|
||||||
return r.writer
|
return r.writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetWriter implements `Response#SetWriter` method.
|
||||||
|
func (r *Response) SetWriter(w io.Writer) {
|
||||||
|
r.writer = w
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Response) reset(w http.ResponseWriter, h engine.Header) {
|
func (r *Response) reset(w http.ResponseWriter, h engine.Header) {
|
||||||
r.ResponseWriter = w
|
r.ResponseWriter = w
|
||||||
r.header = h
|
r.header = h
|
||||||
|
@ -10,15 +10,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// Server implements `engine.Engine`.
|
||||||
Server struct {
|
Server struct {
|
||||||
*http.Server
|
*http.Server
|
||||||
config engine.Config
|
config engine.Config
|
||||||
handler engine.Handler
|
handler engine.Handler
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
pool *Pool
|
pool *pool
|
||||||
}
|
}
|
||||||
|
|
||||||
Pool struct {
|
pool struct {
|
||||||
request sync.Pool
|
request sync.Pool
|
||||||
response sync.Pool
|
response sync.Pool
|
||||||
header sync.Pool
|
header sync.Pool
|
||||||
@ -26,11 +27,13 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// New returns an instance of `standard.Server` with specified listen address.
|
||||||
func New(addr string) *Server {
|
func New(addr string) *Server {
|
||||||
c := engine.Config{Address: addr}
|
c := engine.Config{Address: addr}
|
||||||
return NewFromConfig(c)
|
return NewFromConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFromTLS returns an instance of `standard.Server` from TLS config.
|
||||||
func NewFromTLS(addr, certfile, keyfile string) *Server {
|
func NewFromTLS(addr, certfile, keyfile string) *Server {
|
||||||
c := engine.Config{
|
c := engine.Config{
|
||||||
Address: addr,
|
Address: addr,
|
||||||
@ -40,11 +43,12 @@ func NewFromTLS(addr, certfile, keyfile string) *Server {
|
|||||||
return NewFromConfig(c)
|
return NewFromConfig(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewFromConfig returns an instance of `standard.Server` from config.
|
||||||
func NewFromConfig(c engine.Config) (s *Server) {
|
func NewFromConfig(c engine.Config) (s *Server) {
|
||||||
s = &Server{
|
s = &Server{
|
||||||
Server: new(http.Server),
|
Server: new(http.Server),
|
||||||
config: c,
|
config: c,
|
||||||
pool: &Pool{
|
pool: &pool{
|
||||||
request: sync.Pool{
|
request: sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return &Request{}
|
return &Request{}
|
||||||
@ -76,14 +80,17 @@ func NewFromConfig(c engine.Config) (s *Server) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetHandler implements `Engine#SetHandler` method.
|
||||||
func (s *Server) SetHandler(h engine.Handler) {
|
func (s *Server) SetHandler(h engine.Handler) {
|
||||||
s.handler = h
|
s.handler = h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetLogger implements `Engine#SetLogger` method.
|
||||||
func (s *Server) SetLogger(l *log.Logger) {
|
func (s *Server) SetLogger(l *log.Logger) {
|
||||||
s.logger = l
|
s.logger = l
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start implements `Engine#Start` method.
|
||||||
func (s *Server) Start() {
|
func (s *Server) Start() {
|
||||||
certfile := s.config.TLSCertfile
|
certfile := s.config.TLSCertfile
|
||||||
keyfile := s.config.TLSKeyfile
|
keyfile := s.config.TLSKeyfile
|
||||||
@ -94,6 +101,7 @@ func (s *Server) Start() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ServeHTTP implements `http.Handler` interface.
|
||||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// Request
|
// Request
|
||||||
req := s.pool.request.Get().(*Request)
|
req := s.pool.request.Get().(*Request)
|
||||||
|
@ -3,20 +3,24 @@ package standard
|
|||||||
import "net/url"
|
import "net/url"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// URL implements `engine.URL`.
|
||||||
URL struct {
|
URL struct {
|
||||||
*url.URL
|
*url.URL
|
||||||
query url.Values
|
query url.Values
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (u *URL) SetPath(path string) {
|
// Path implements `URL#Path` method.
|
||||||
u.URL.Path = path
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *URL) Path() string {
|
func (u *URL) Path() string {
|
||||||
return u.URL.Path
|
return u.URL.Path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetPath implements `URL#SetPath` method.
|
||||||
|
func (u *URL) SetPath(path string) {
|
||||||
|
u.URL.Path = path
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryValue implements `URL#QueryValue` method.
|
||||||
func (u *URL) QueryValue(name string) string {
|
func (u *URL) QueryValue(name string) string {
|
||||||
if u.query == nil {
|
if u.query == nil {
|
||||||
u.query = u.Query()
|
u.query = u.Query()
|
||||||
|
2
glide.lock
generated
2
glide.lock
generated
@ -1,5 +1,5 @@
|
|||||||
hash: f220137e9ccd9aaf3051be33c3c23ea8abd2c40df6cf96175c3994d482b5e007
|
hash: f220137e9ccd9aaf3051be33c3c23ea8abd2c40df6cf96175c3994d482b5e007
|
||||||
updated: 2016-03-14T08:16:01.763706114-07:00
|
updated: 2016-03-14T18:03:44.054071074-07:00
|
||||||
imports:
|
imports:
|
||||||
- name: github.com/klauspost/compress
|
- name: github.com/klauspost/compress
|
||||||
version: 006acde2c5d283d2f8b8aa03d8f0cd2891c680cf
|
version: 006acde2c5d283d2f8b8aa03d8f0cd2891c680cf
|
||||||
|
@ -60,6 +60,10 @@ func (r *Request) Header() engine.Header {
|
|||||||
// return r.request.ProtoMinor()
|
// return r.request.ProtoMinor()
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
func (r *Request) UserAgent() string {
|
||||||
|
return r.request.UserAgent()
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Request) RemoteAddress() string {
|
func (r *Request) RemoteAddress() string {
|
||||||
return r.request.RemoteAddr
|
return r.request.RemoteAddr
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user