2016-01-28 23:46:11 -08:00
|
|
|
package engine
|
|
|
|
|
2016-02-04 14:40:08 -08:00
|
|
|
import (
|
|
|
|
"io"
|
2016-03-12 15:06:52 -08:00
|
|
|
"mime/multipart"
|
2016-02-04 14:40:08 -08:00
|
|
|
"time"
|
2016-02-09 17:16:46 -08:00
|
|
|
|
2016-03-06 09:52:32 -08:00
|
|
|
"github.com/labstack/gommon/log"
|
2016-02-04 14:40:08 -08:00
|
|
|
)
|
2016-01-28 23:46:11 -08:00
|
|
|
|
|
|
|
type (
|
2016-03-14 19:58:46 -07:00
|
|
|
// Engine defines the interface for HTTP server.
|
2016-01-28 23:46:11 -08:00
|
|
|
Engine interface {
|
2016-03-08 08:14:25 -08:00
|
|
|
SetHandler(Handler)
|
2016-03-06 09:52:32 -08:00
|
|
|
SetLogger(*log.Logger)
|
2016-01-28 23:46:11 -08:00
|
|
|
Start()
|
|
|
|
}
|
|
|
|
|
2016-03-14 19:58:46 -07:00
|
|
|
// Request defines the interface for HTTP request.
|
2016-01-28 23:46:11 -08:00
|
|
|
Request interface {
|
2016-03-14 19:58:46 -07:00
|
|
|
// TLS returns true if connection is TLS otherwise false.
|
2016-02-22 21:13:08 -08:00
|
|
|
TLS() bool
|
2016-03-14 19:58:46 -07:00
|
|
|
|
2016-02-22 21:26:36 -08:00
|
|
|
Scheme() string
|
2016-02-09 08:12:37 -08:00
|
|
|
Host() string
|
2016-02-08 16:48:03 -08:00
|
|
|
URI() string
|
|
|
|
URL() URL
|
2016-01-28 23:46:11 -08:00
|
|
|
Header() Header
|
|
|
|
// Proto() string
|
|
|
|
// ProtoMajor() int
|
|
|
|
// ProtoMinor() int
|
2016-03-14 19:58:46 -07:00
|
|
|
UserAgent() string
|
2016-01-28 23:46:11 -08:00
|
|
|
RemoteAddress() string
|
|
|
|
Method() string
|
2016-03-14 22:45:29 -07:00
|
|
|
SetMethod(string)
|
2016-01-28 23:46:11 -08:00
|
|
|
Body() io.ReadCloser
|
|
|
|
FormValue(string) string
|
2016-03-12 15:06:52 -08:00
|
|
|
FormFile(string) (*multipart.FileHeader, error)
|
|
|
|
MultipartForm() (*multipart.Form, error)
|
2016-01-28 23:46:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-14 19:58:46 -07:00
|
|
|
// Response defines the interface for HTTP response.
|
2016-01-28 23:46:11 -08:00
|
|
|
Response interface {
|
|
|
|
Header() Header
|
|
|
|
WriteHeader(int)
|
|
|
|
Write(b []byte) (int, error)
|
|
|
|
Status() int
|
|
|
|
Size() int64
|
|
|
|
Committed() bool
|
2016-03-10 16:35:20 -08:00
|
|
|
Writer() io.Writer
|
2016-03-14 19:58:46 -07:00
|
|
|
SetWriter(io.Writer)
|
2016-01-28 23:46:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-14 19:58:46 -07:00
|
|
|
// Header defines the interface for HTTP header.
|
2016-01-28 23:46:11 -08:00
|
|
|
Header interface {
|
|
|
|
Add(string, string)
|
|
|
|
Del(string)
|
|
|
|
Set(string, string)
|
2016-03-14 19:58:46 -07:00
|
|
|
Get(string) string
|
2016-01-28 23:46:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-14 19:58:46 -07:00
|
|
|
// URL defines the interface for HTTP request url.
|
2016-01-28 23:46:11 -08:00
|
|
|
URL interface {
|
|
|
|
Path() string
|
2016-03-14 19:58:46 -07:00
|
|
|
SetPath(string)
|
2016-01-28 23:46:11 -08:00
|
|
|
QueryValue(string) string
|
2016-03-15 08:50:43 -07:00
|
|
|
QueryString() string
|
2016-01-28 23:46:11 -08:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:24:45 -08:00
|
|
|
// Config defines engine configuration.
|
2016-01-28 23:46:11 -08:00
|
|
|
Config struct {
|
2016-02-04 14:40:08 -08:00
|
|
|
Address string
|
|
|
|
TLSCertfile string
|
|
|
|
TLSKeyfile string
|
2016-02-22 22:24:56 -08:00
|
|
|
ReadTimeout time.Duration
|
|
|
|
WriteTimeout time.Duration
|
2016-01-28 23:46:11 -08:00
|
|
|
}
|
2016-03-08 08:14:25 -08:00
|
|
|
|
2016-03-11 08:47:23 -08:00
|
|
|
// Handler defines an interface to server HTTP requests via `ServeHTTP(Request, Response)`
|
|
|
|
// function.
|
2016-03-08 08:14:25 -08:00
|
|
|
Handler interface {
|
|
|
|
ServeHTTP(Request, Response)
|
|
|
|
}
|
|
|
|
|
2016-03-11 08:47:23 -08:00
|
|
|
// HandlerFunc is an adapter to allow the use of `func(Request, Response)` as HTTP handlers.
|
2016-03-08 08:14:25 -08:00
|
|
|
HandlerFunc func(Request, Response)
|
2016-01-28 23:46:11 -08:00
|
|
|
)
|
2016-03-08 08:14:25 -08:00
|
|
|
|
2016-03-11 08:47:23 -08:00
|
|
|
// ServeHTTP serves HTTP request.
|
2016-03-08 08:14:25 -08:00
|
|
|
func (h HandlerFunc) ServeHTTP(req Request, res Response) {
|
2016-03-10 13:17:43 +08:00
|
|
|
h(req, res)
|
2016-03-08 08:14:25 -08:00
|
|
|
}
|