1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-03 00:56:59 +02:00

API changes from to with

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2016-04-07 21:20:50 -07:00
parent a8cf473ee7
commit 1a7bc677da
9 changed files with 35 additions and 35 deletions

View File

@ -30,24 +30,24 @@ type (
} }
) )
// New returns an instance of `fasthttp.Server` with provided listen address. // New returns `fasthttp.Server` with provided 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 WithConfig(c)
} }
// NewFromTLS returns an instance of `fasthttp.Server` from TLS config. // WithTLS returns `fasthttp.Server` with TLS config.
func NewFromTLS(addr, certfile, keyfile string) *Server { func WithTLS(addr, certfile, keyfile string) *Server {
c := engine.Config{ c := engine.Config{
Address: addr, Address: addr,
TLSCertfile: certfile, TLSCertfile: certfile,
TLSKeyfile: keyfile, TLSKeyfile: keyfile,
} }
return NewFromConfig(c) return WithConfig(c)
} }
// NewFromConfig returns an instance of `standard.Server` from config. // WithConfig returns `standard.Server` with config.
func NewFromConfig(c engine.Config) (s *Server) { func WithConfig(c engine.Config) (s *Server) {
s = &Server{ s = &Server{
Server: new(fasthttp.Server), Server: new(fasthttp.Server),
config: c, config: c,

View File

@ -28,24 +28,24 @@ type (
} }
) )
// New returns an instance of `standard.Server` with provided listen address. // New returns `standard.Server` with provided 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 WithConfig(c)
} }
// NewFromTLS returns an instance of `standard.Server` from TLS config. // WithTLS returns `standard.Server` with TLS config.
func NewFromTLS(addr, certfile, keyfile string) *Server { func WithTLS(addr, certfile, keyfile string) *Server {
c := engine.Config{ c := engine.Config{
Address: addr, Address: addr,
TLSCertfile: certfile, TLSCertfile: certfile,
TLSKeyfile: keyfile, TLSKeyfile: keyfile,
} }
return NewFromConfig(c) return WithConfig(c)
} }
// NewFromConfig returns an instance of `standard.Server` from config. // WithConfig returns `standard.Server` with config.
func NewFromConfig(c engine.Config) (s *Server) { func WithConfig(c engine.Config) (s *Server) {
s = &Server{ s = &Server{
Server: new(http.Server), Server: new(http.Server),
config: c, config: c,

View File

@ -33,12 +33,12 @@ var (
func BasicAuth(f BasicAuthFunc) echo.MiddlewareFunc { func BasicAuth(f BasicAuthFunc) echo.MiddlewareFunc {
c := DefaultBasicAuthConfig c := DefaultBasicAuthConfig
c.AuthFunc = f c.AuthFunc = f
return BasicAuthFromConfig(c) return BasicAuthWithConfig(c)
} }
// BasicAuthFromConfig returns an HTTP basic auth middleware from config. // BasicAuthWithConfig returns an HTTP basic auth middleware from config.
// See `BasicAuth()`. // See `BasicAuth()`.
func BasicAuthFromConfig(config BasicAuthConfig) echo.MiddlewareFunc { func BasicAuthWithConfig(config BasicAuthConfig) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc { return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
auth := c.Request().Header().Get(echo.HeaderAuthorization) auth := c.Request().Header().Get(echo.HeaderAuthorization)

View File

@ -36,12 +36,12 @@ var (
// Gzip returns a middleware which compresses HTTP response using gzip compression // Gzip returns a middleware which compresses HTTP response using gzip compression
// scheme. // scheme.
func Gzip() echo.MiddlewareFunc { func Gzip() echo.MiddlewareFunc {
return GzipFromConfig(DefaultGzipConfig) return GzipWithConfig(DefaultGzipConfig)
} }
// GzipFromConfig return gzip middleware from config. // GzipWithConfig return gzip middleware from config.
// See `Gzip()`. // See `Gzip()`.
func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc { func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
// Defaults // Defaults
if config.Level == 0 { if config.Level == 0 {
config.Level = DefaultGzipConfig.Level config.Level = DefaultGzipConfig.Level

View File

@ -55,12 +55,12 @@ var (
// CORS returns a Cross-Origin Resource Sharing (CORS) middleware. // CORS returns a Cross-Origin Resource Sharing (CORS) middleware.
// See https://developer.mozilla.org/en/docs/Web/HTTP/Access_control_CORS // See https://developer.mozilla.org/en/docs/Web/HTTP/Access_control_CORS
func CORS() echo.MiddlewareFunc { func CORS() echo.MiddlewareFunc {
return CORSFromConfig(DefaultCORSConfig) return CORSWithConfig(DefaultCORSConfig)
} }
// CORSFromConfig returns a CORS middleware from config. // CORSWithConfig returns a CORS middleware from config.
// See `CORS()`. // See `CORS()`.
func CORSFromConfig(config CORSConfig) echo.MiddlewareFunc { func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
// Defaults // Defaults
if len(config.AllowOrigins) == 0 { if len(config.AllowOrigins) == 0 {
config.AllowOrigins = DefaultCORSConfig.AllowOrigins config.AllowOrigins = DefaultCORSConfig.AllowOrigins

View File

@ -14,7 +14,7 @@ func TestCORS(t *testing.T) {
rq := test.NewRequest(echo.GET, "/", nil) rq := test.NewRequest(echo.GET, "/", nil)
rc := test.NewResponseRecorder() rc := test.NewResponseRecorder()
c := echo.NewContext(rq, rc, e) c := echo.NewContext(rq, rc, e)
cors := CORSFromConfig(CORSConfig{ cors := CORSWithConfig(CORSConfig{
AllowCredentials: true, AllowCredentials: true,
}) })
h := cors(func(c echo.Context) error { h := cors(func(c echo.Context) error {
@ -38,7 +38,7 @@ func TestCORS(t *testing.T) {
rc = test.NewResponseRecorder() rc = test.NewResponseRecorder()
c = echo.NewContext(rq, rc, e) c = echo.NewContext(rq, rc, e)
rq.Header().Set(echo.HeaderOrigin, "localhost") rq.Header().Set(echo.HeaderOrigin, "localhost")
cors = CORSFromConfig(CORSConfig{ cors = CORSWithConfig(CORSConfig{
AllowOrigins: []string{"localhost"}, AllowOrigins: []string{"localhost"},
AllowCredentials: true, AllowCredentials: true,
MaxAge: 3600, MaxAge: 3600,

View File

@ -54,12 +54,12 @@ var (
// Logger returns a middleware that logs HTTP requests. // Logger returns a middleware that logs HTTP requests.
func Logger() echo.MiddlewareFunc { func Logger() echo.MiddlewareFunc {
return LoggerFromConfig(DefaultLoggerConfig) return LoggerWithConfig(DefaultLoggerConfig)
} }
// LoggerFromConfig returns a logger middleware from config. // LoggerWithConfig returns a logger middleware from config.
// See `Logger()`. // See `Logger()`.
func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc { func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
// Defaults // Defaults
if config.Format == "" { if config.Format == "" {
config.Format = DefaultLoggerConfig.Format config.Format = DefaultLoggerConfig.Format

View File

@ -38,12 +38,12 @@ var (
// Recover returns a middleware which recovers from panics anywhere in the chain // Recover returns a middleware which recovers from panics anywhere in the chain
// and handles the control to the centralized HTTPErrorHandler. // and handles the control to the centralized HTTPErrorHandler.
func Recover() echo.MiddlewareFunc { func Recover() echo.MiddlewareFunc {
return RecoverFromConfig(DefaultRecoverConfig) return RecoverWithConfig(DefaultRecoverConfig)
} }
// RecoverFromConfig returns a recover middleware from config. // RecoverWithConfig returns a recover middleware from config.
// See `Recover()`. // See `Recover()`.
func RecoverFromConfig(config RecoverConfig) echo.MiddlewareFunc { func RecoverWithConfig(config RecoverConfig) echo.MiddlewareFunc {
// Defaults // Defaults
if config.StackSize == 0 { if config.StackSize == 0 {
config.StackSize = DefaultRecoverConfig.StackSize config.StackSize = DefaultRecoverConfig.StackSize

View File

@ -12,7 +12,7 @@ type (
// StaticConfig defines the config for static middleware. // StaticConfig defines the config for static middleware.
StaticConfig struct { StaticConfig struct {
// Root is the directory from where the static content is served. // Root is the directory from where the static content is served.
// Optional with default value as "". // Required.
Root string `json:"root"` Root string `json:"root"`
// Index is the list of index files to be searched and used when serving // Index is the list of index files to be searched and used when serving
@ -40,12 +40,12 @@ var (
func Static(root string) echo.MiddlewareFunc { func Static(root string) echo.MiddlewareFunc {
c := DefaultStaticConfig c := DefaultStaticConfig
c.Root = root c.Root = root
return StaticFromConfig(c) return StaticWithConfig(c)
} }
// StaticFromConfig returns a static middleware from config. // StaticWithConfig returns a static middleware from config.
// See `Static()`. // See `Static()`.
func StaticFromConfig(config StaticConfig) echo.MiddlewareFunc { func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
// Defaults // Defaults
if config.Index == nil { if config.Index == nil {
config.Index = DefaultStaticConfig.Index config.Index = DefaultStaticConfig.Index