1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

exposed read timeout, write timeout properties, updated docs

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-12-15 18:44:06 -08:00
parent c848119eef
commit dc92c26084
4 changed files with 96 additions and 22 deletions

View File

@ -34,10 +34,16 @@
**Use issues for everything** **Use issues for everything**
- Report issues - For a small change, just send a PR.
- Discuss on chat before sending a pull request - For bigger changes open an issue for discussion before sending a PR.
- Suggest new features or enhancements - PR should have:
- Improve/fix documentation - Test case
- Documentation
- Recipe (If it makes sense)
- You can also contribute by:
- Reporting issues
- Suggesting new features or enhancements
- Improve/fix documentation
## Credits ## Credits
- [Vishal Rana](https://github.com/vishr) - Author - [Vishal Rana](https://github.com/vishr) - Author

16
echo.go
View File

@ -66,9 +66,12 @@ type (
Validator Validator Validator Validator
Renderer Renderer Renderer Renderer
AutoTLSManager autocert.Manager AutoTLSManager autocert.Manager
ReadTimeout time.Duration
WriteTimeout time.Duration
ShutdownTimeout time.Duration ShutdownTimeout time.Duration
Color *color.Color Color *color.Color
Logger Logger Logger Logger
stdLogger *slog.Logger
server *graceful.Server server *graceful.Server
tlsServer *graceful.Server tlsServer *graceful.Server
premiddleware []MiddlewareFunc premiddleware []MiddlewareFunc
@ -249,6 +252,7 @@ func New() (e *Echo) {
e.HTTPErrorHandler = e.DefaultHTTPErrorHandler e.HTTPErrorHandler = e.DefaultHTTPErrorHandler
e.Binder = &DefaultBinder{} e.Binder = &DefaultBinder{}
e.Logger.SetLevel(log.OFF) e.Logger.SetLevel(log.OFF)
e.stdLogger = slog.New(e.Logger.Output(), e.Logger.Prefix()+": ", 0)
e.pool.New = func() interface{} { e.pool.New = func() interface{} {
return e.NewContext(nil, nil) return e.NewContext(nil, nil)
} }
@ -519,7 +523,12 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Start starts the HTTP server. // Start starts the HTTP server.
func (e *Echo) Start(address string) error { func (e *Echo) Start(address string) error {
return e.StartServer(&http.Server{Addr: address}) return e.StartServer(&http.Server{
Addr: address,
ReadTimeout: e.ReadTimeout,
WriteTimeout: e.WriteTimeout,
ErrorLog: e.stdLogger,
})
} }
// StartTLS starts the HTTPS server. // StartTLS starts the HTTPS server.
@ -549,7 +558,10 @@ func (e *Echo) startTLS(address string, config *tls.Config) error {
} }
return e.StartServer(&http.Server{ return e.StartServer(&http.Server{
Addr: address, Addr: address,
ReadTimeout: e.ReadTimeout,
WriteTimeout: e.WriteTimeout,
TLSConfig: config, TLSConfig: config,
ErrorLog: e.stdLogger,
}) })
} }
@ -559,7 +571,7 @@ func (e *Echo) StartServer(s *http.Server) error {
gs := &graceful.Server{ gs := &graceful.Server{
Server: s, Server: s,
Timeout: e.ShutdownTimeout, Timeout: e.ShutdownTimeout,
Logger: slog.New(e.Logger.Output(), e.Logger.Prefix()+": ", 0), Logger: e.stdLogger,
} }
if s.TLSConfig == nil { if s.TLSConfig == nil {
e.server = gs e.server = gs

View File

@ -7,15 +7,10 @@ description = "Customizing Echo"
weight = 3 weight = 3
+++ +++
## HTTP Error Handler ## Debug
`Echo#HTTPErrorHandler` can be used to set custom http error handler. `Echo#Debug` can be used to enable / disable debug mode. Debug mode sets the log level
to `DEBUG`.
[Learn more](/guide/error-handling)
## Debugging
`Echo#Debug` can be used to enable / disable debug mode.
## Logging ## Logging
@ -24,12 +19,12 @@ description = "Customizing Echo"
`Echo#Logger.SetOutput(io.Writer)` can be used to set the output destination for `Echo#Logger.SetOutput(io.Writer)` can be used to set the output destination for
the logger. Default value is `os.Stdout` the logger. Default value is `os.Stdout`
To completely disable logs use `Echo#Logger.SetOutput(io.Discard)` or `Echo#Logger.SetLevel(log.OFF)` To completely disabl logs use `Echo#Logger.SetOutput(io.Discard)` or `Echo#Logger.SetLevel(log.OFF)`
### Log Level ### Log Level
`Echo#Logger.SetLevel(log.Lvl)` can be used to set the log level for the logger. `Echo#Logger.SetLevel(log.Lvl)` can be used to set the log level for the logger.
Default value `OFF`. Possible values: Default value is `OFF`. Possible values:
- `DEBUG` - `DEBUG`
- `INFO` - `INFO`
@ -37,6 +32,67 @@ Default value `OFF`. Possible values:
- `ERROR` - `ERROR`
- `OFF` - `OFF`
Logging is implemented using `echo.Logger` interface which allows you to use a ### Custom Logger
custom logger. Custom logger can be set using `Echo#Logger`.
Logging is implemented using `echo.Logger` interface which allows you to register
a custom logger using `Echo#Logger`.
## Custom Server
`Echo#StartServer()` can be used to run a custom `http.Server`.
*Example*
```go
s := &http.Server{
Addr: ":1323",
ReadTimeout: 20 * time.Minute,
WriteTimeout: 20 * time.Minute,
}
e.Logger.Fatal(e.StartServer(s))
```
## Disable HTTP/2
`Echo#DisableHTTP2` can be used disable HTTP/2 protocol.
## Read Timeout
`Echo#ReadTimeout` can be used to set the maximum duration before timing out read
of the request.
## Write Timeout
`Echo#WriteTimeout` can be used to set the maximum duration before timing out write
of the response.
## Shutdown Timeout
`Echo#ShutdownTimeout` can be used to set the maximum duration to wait until killing
active requests and stopping the server. If timeout is 0, the server never times
out. It waits for all active requests to finish.
## Validator
`Echo#Validator` can be used to register a validator for performing data validation
on request payload.
[Learn more](/guide/request#validate-data)
## Custom Binder
`Echo#Binder` can be used to register a custom binder for binding request payload.
[Learn more](/guide/request/#custom-binder)
## Renderer
`Echo#Renderer` can be used to register a renderer for template rendering.
[Learn more](/guide/templates)
## HTTP Error Handler
`Echo#HTTPErrorHandler` can be used to register a custom http error handler.
[Learn more](/guide/error-handling)

View File

@ -162,8 +162,8 @@ $ curl http://localhost:1323/users/Joe
## Validate Data ## Validate Data
Echo doesn't have built-in data validation capabilities, however you can set a Echo doesn't have a built-in data validation capabilities, however, you can register
custom validator using `Echo#Validator` and leverage third-party [libraries](https://github.com/avelino/awesome-go#validation). a custom validator using `Echo#Validator` and leverage third-party [libraries](https://github.com/avelino/awesome-go#validation).
Example below uses https://github.com/go-playground/validator framework for validation: Example below uses https://github.com/go-playground/validator framework for validation: