diff --git a/group.go b/group.go index 710d2af2..799a8f90 100644 --- a/group.go +++ b/group.go @@ -1,8 +1,12 @@ package echo +import ( + "path" +) + type ( // Group is a set of sub-routes for a specified route. It can be used for inner - // routes that share a common middlware or functionality that should be separate + // routes that share a common middleware or functionality that should be separate // from the parent echo instance while still inheriting from it. Group struct { prefix string @@ -16,7 +20,7 @@ func (g *Group) Use(middleware ...MiddlewareFunc) { g.middleware = append(g.middleware, middleware...) // Allow all requests to reach the group as they might get dropped if router // doesn't find a match, making none of the group middleware process. - g.echo.Any(g.prefix+"/*", func(c Context) error { + g.echo.Any(path.Clean(g.prefix+"/*"), func(c Context) error { return ErrNotFound }, g.middleware...) } diff --git a/website/content/guide/static-files.md b/website/content/guide/static-files.md index ae125ff8..66c2dc48 100644 --- a/website/content/guide/static-files.md +++ b/website/content/guide/static-files.md @@ -9,6 +9,8 @@ description = "Serving static files in Echo" Images, JavaScript, CSS, PDF, Fonts and so on... +## [Using Static Middleware]({{< ref "middleware/static.md">}}) + ## Using `Echo#Static()` `Echo#Static(prefix, root string)` registers a new route with path prefix to serve diff --git a/website/content/middleware/basic-auth.md b/website/content/middleware/basic-auth.md index bd853074..265b79a3 100644 --- a/website/content/middleware/basic-auth.md +++ b/website/content/middleware/basic-auth.md @@ -52,6 +52,6 @@ BasicAuthConfig struct { ```go DefaultBasicAuthConfig = BasicAuthConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, } ``` diff --git a/website/content/middleware/body-limit.md b/website/content/middleware/body-limit.md index 52bac5d0..a1f52537 100644 --- a/website/content/middleware/body-limit.md +++ b/website/content/middleware/body-limit.md @@ -48,6 +48,6 @@ BodyLimitConfig struct { ```go DefaultBodyLimitConfig = BodyLimitConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, } ``` diff --git a/website/content/middleware/cors.md b/website/content/middleware/cors.md index fb9aea4f..432a9cee 100644 --- a/website/content/middleware/cors.md +++ b/website/content/middleware/cors.md @@ -72,7 +72,7 @@ CORSConfig struct { ```go DefaultCORSConfig = CORSConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, AllowOrigins: []string{"*"}, AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE}, } diff --git a/website/content/middleware/csrf.md b/website/content/middleware/csrf.md index 819ea929..43aeba94 100644 --- a/website/content/middleware/csrf.md +++ b/website/content/middleware/csrf.md @@ -95,7 +95,7 @@ CSRFConfig struct { ```go DefaultCSRFConfig = CSRFConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, TokenLength: 32, TokenLookup: "header:" + echo.HeaderXCSRFToken, ContextKey: "csrf", diff --git a/website/content/middleware/gzip.md b/website/content/middleware/gzip.md index a527237b..43a0d3e9 100644 --- a/website/content/middleware/gzip.md +++ b/website/content/middleware/gzip.md @@ -41,7 +41,7 @@ GzipConfig struct { ```go DefaultGzipConfig = GzipConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, Level: -1, } ``` diff --git a/website/content/middleware/jwt.md b/website/content/middleware/jwt.md index 1a8c4a9e..f347e44d 100644 --- a/website/content/middleware/jwt.md +++ b/website/content/middleware/jwt.md @@ -71,7 +71,7 @@ JWTConfig struct { ```go DefaultJWTConfig = JWTConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, SigningMethod: AlgorithmHS256, ContextKey: "user", TokenLookup: "header:" + echo.HeaderAuthorization, diff --git a/website/content/middleware/key-auth.md b/website/content/middleware/key-auth.md index 84a58a2e..b30d43c1 100644 --- a/website/content/middleware/key-auth.md +++ b/website/content/middleware/key-auth.md @@ -62,7 +62,7 @@ KeyAuthConfig struct { ```go DefaultKeyAuthConfig = KeyAuthConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, KeyLookup: "header:" + echo.HeaderAuthorization, AuthScheme: "Bearer", } diff --git a/website/content/middleware/logger.md b/website/content/middleware/logger.md index 0ffa0c7f..1674d845 100644 --- a/website/content/middleware/logger.md +++ b/website/content/middleware/logger.md @@ -83,7 +83,7 @@ LoggerConfig struct { ```go DefaultLoggerConfig = LoggerConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, Format: `{"time":"${time_rfc3339_nano}","remote_ip":"${remote_ip}","host":"${host}",` + `"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` + `"latency_human":"${latency_human}","bytes_in":${bytes_in},` + diff --git a/website/content/middleware/method-override.md b/website/content/middleware/method-override.md index d71400e7..dd6d6f5f 100644 --- a/website/content/middleware/method-override.md +++ b/website/content/middleware/method-override.md @@ -44,7 +44,7 @@ MethodOverrideConfig struct { ```go DefaultMethodOverrideConfig = MethodOverrideConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, Getter: MethodFromHeader(echo.HeaderXHTTPMethodOverride), } ``` diff --git a/website/content/middleware/recover.md b/website/content/middleware/recover.md index a2c4d84f..862c3317 100644 --- a/website/content/middleware/recover.md +++ b/website/content/middleware/recover.md @@ -55,7 +55,7 @@ RecoverConfig struct { ```go DefaultRecoverConfig = RecoverConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, StackSize: 4 << 10, // 4 KB DisableStackAll: false, DisablePrintStack: false, diff --git a/website/content/middleware/redirect.md b/website/content/middleware/redirect.md index ec921749..31e99fd5 100644 --- a/website/content/middleware/redirect.md +++ b/website/content/middleware/redirect.md @@ -98,7 +98,7 @@ RedirectConfig struct { ```go DefaultRedirectConfig = RedirectConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, Code: http.StatusMovedPermanently, } ``` diff --git a/website/content/middleware/secure.md b/website/content/middleware/secure.md index d433d608..8ba67956 100644 --- a/website/content/middleware/secure.md +++ b/website/content/middleware/secure.md @@ -88,7 +88,7 @@ SecureConfig struct { ```go DefaultSecureConfig = SecureConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, XSSProtection: "1; mode=block", ContentTypeNosniff: "nosniff", XFrameOptions: "SAMEORIGIN", diff --git a/website/content/middleware/static.md b/website/content/middleware/static.md new file mode 100644 index 00000000..5d34121c --- /dev/null +++ b/website/content/middleware/static.md @@ -0,0 +1,69 @@ ++++ +title = "Static Middleware" +description = "Static middleware for Echo" +[menu.main] + name = "Static" + parent = "middleware" + weight = 5 ++++ + +Static middleware can be used to serve static files from the provided root directory. + +*Usage* + +```go +e := echo.New() +e.Use(middleware.Static("/static")) +``` + +This serves static files from `static` directory. For example, a request to `/js/main.js` +will fetch and serve `static/js/main.js` file. + +## Custom Configuration + +*Usage* + +```go +e := echo.New() +e.Use(middleware.StaticWithConfig(middleware.StaticConfig{ + Root: "static", + Browse: true, +})) +``` + +This serves static files from `static` directory and enables directory browsing. + +## Configuration + +```go +StaticConfig struct { + // Skipper defines a function to skip middleware. + Skipper Skipper + + // Root directory from where the static content is served. + // Required. + Root string `json:"root"` + + // Index file for serving a directory. + // Optional. Default value "index.html". + Index string `json:"index"` + + // Enable HTML5 mode by forwarding all not-found requests to root so that + // SPA (single-page application) can handle the routing. + // Optional. Default value false. + HTML5 bool `json:"html5"` + + // Enable directory browsing. + // Optional. Default value false. + Browse bool `json:"browse"` +} +``` + +*Default Configuration* + +```go +DefaultStaticConfig = StaticConfig{ + Skipper: DefaultSkipper, + Index: "index.html", +} +``` diff --git a/website/content/middleware/trailing-slash.md b/website/content/middleware/trailing-slash.md index 74facc72..2823f0f6 100644 --- a/website/content/middleware/trailing-slash.md +++ b/website/content/middleware/trailing-slash.md @@ -59,6 +59,6 @@ TrailingSlashConfig struct { ```go DefaultTrailingSlashConfig = TrailingSlashConfig{ - Skipper: defaultSkipper, + Skipper: DefaultSkipper, } ```