Bumps the go-deps group with 11 updates in the / directory: | Package | From | To | | --- | --- | --- | | [cloud.google.com/go/compute](https://github.com/googleapis/google-cloud-go) | `1.25.1` | `1.26.0` | | [github.com/Azure/azure-sdk-for-go/sdk/azidentity](https://github.com/Azure/azure-sdk-for-go) | `1.5.1` | `1.5.2` | | [github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5](https://github.com/Azure/azure-sdk-for-go) | `5.5.0` | `5.7.0` | | [github.com/aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) | `1.51.25` | `1.52.0` | | [github.com/getsentry/sentry-go](https://github.com/getsentry/sentry-go) | `0.26.0` | `0.27.0` | | [github.com/gophercloud/gophercloud](https://github.com/gophercloud/gophercloud) | `1.10.0` | `1.11.0` | | [github.com/jackc/pgtype](https://github.com/jackc/pgtype) | `1.14.1` | `1.14.3` | | [github.com/labstack/echo/v4](https://github.com/labstack/echo) | `4.11.4` | `4.12.0` | | [github.com/openshift-online/ocm-sdk-go](https://github.com/openshift-online/ocm-sdk-go) | `0.1.398` | `0.1.418` | | [github.com/osbuild/images](https://github.com/osbuild/images) | `0.56.0` | `0.58.0` | | [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) | `1.18.0` | `1.19.0` | Updates `cloud.google.com/go/compute` from 1.25.1 to 1.26.0 - [Release notes](https://github.com/googleapis/google-cloud-go/releases) - [Changelog](https://github.com/googleapis/google-cloud-go/blob/main/documentai/CHANGES.md) - [Commits](https://github.com/googleapis/google-cloud-go/compare/pubsub/v1.25.1...pubsub/v1.26.0) Updates `github.com/Azure/azure-sdk-for-go/sdk/azidentity` from 1.5.1 to 1.5.2 - [Release notes](https://github.com/Azure/azure-sdk-for-go/releases) - [Changelog](https://github.com/Azure/azure-sdk-for-go/blob/main/documentation/release.md) - [Commits](https://github.com/Azure/azure-sdk-for-go/compare/sdk/internal/v1.5.1...sdk/internal/v1.5.2) Updates `github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5` from 5.5.0 to 5.7.0 - [Release notes](https://github.com/Azure/azure-sdk-for-go/releases) - [Changelog](https://github.com/Azure/azure-sdk-for-go/blob/main/documentation/release.md) - [Commits](https://github.com/Azure/azure-sdk-for-go/compare/sdk/resourcemanager/compute/armcompute/v5.5.0...sdk/resourcemanager/compute/armcompute/v5.7.0) Updates `github.com/aws/aws-sdk-go` from 1.51.25 to 1.52.0 - [Release notes](https://github.com/aws/aws-sdk-go/releases) - [Commits](https://github.com/aws/aws-sdk-go/compare/v1.51.25...v1.52.0) Updates `github.com/getsentry/sentry-go` from 0.26.0 to 0.27.0 - [Release notes](https://github.com/getsentry/sentry-go/releases) - [Changelog](https://github.com/getsentry/sentry-go/blob/master/CHANGELOG.md) - [Commits](https://github.com/getsentry/sentry-go/compare/v0.26.0...v0.27.0) Updates `github.com/gophercloud/gophercloud` from 1.10.0 to 1.11.0 - [Release notes](https://github.com/gophercloud/gophercloud/releases) - [Changelog](https://github.com/gophercloud/gophercloud/blob/v1.11.0/CHANGELOG.md) - [Commits](https://github.com/gophercloud/gophercloud/compare/v1.10.0...v1.11.0) Updates `github.com/jackc/pgtype` from 1.14.1 to 1.14.3 - [Changelog](https://github.com/jackc/pgtype/blob/master/CHANGELOG.md) - [Commits](https://github.com/jackc/pgtype/compare/v1.14.1...v1.14.3) Updates `github.com/jackc/pgx/v4` from 4.18.1 to 4.18.2 - [Changelog](https://github.com/jackc/pgx/blob/v4.18.2/CHANGELOG.md) - [Commits](https://github.com/jackc/pgx/compare/v4.18.1...v4.18.2) Updates `github.com/labstack/echo/v4` from 4.11.4 to 4.12.0 - [Release notes](https://github.com/labstack/echo/releases) - [Changelog](https://github.com/labstack/echo/blob/master/CHANGELOG.md) - [Commits](https://github.com/labstack/echo/compare/v4.11.4...v4.12.0) Updates `github.com/openshift-online/ocm-sdk-go` from 0.1.398 to 0.1.418 - [Release notes](https://github.com/openshift-online/ocm-sdk-go/releases) - [Changelog](https://github.com/openshift-online/ocm-sdk-go/blob/main/CHANGES.md) - [Commits](https://github.com/openshift-online/ocm-sdk-go/compare/v0.1.398...v0.1.418) Updates `github.com/osbuild/images` from 0.56.0 to 0.58.0 - [Release notes](https://github.com/osbuild/images/releases) - [Commits](https://github.com/osbuild/images/compare/v0.56.0...v0.58.0) Updates `github.com/prometheus/client_golang` from 1.18.0 to 1.19.0 - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md) - [Commits](https://github.com/prometheus/client_golang/compare/v1.18.0...v1.19.0) Updates `golang.org/x/sync` from 0.6.0 to 0.7.0 - [Commits](https://github.com/golang/sync/compare/v0.6.0...v0.7.0) Updates `google.golang.org/api` from 0.175.0 to 0.177.0 - [Release notes](https://github.com/googleapis/google-api-go-client/releases) - [Changelog](https://github.com/googleapis/google-api-go-client/blob/main/CHANGES.md) - [Commits](https://github.com/googleapis/google-api-go-client/compare/v0.175.0...v0.177.0) --- updated-dependencies: - dependency-name: cloud.google.com/go/compute dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-deps - dependency-name: github.com/Azure/azure-sdk-for-go/sdk/azidentity dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-deps - dependency-name: github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-deps - dependency-name: github.com/aws/aws-sdk-go dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-deps - dependency-name: github.com/getsentry/sentry-go dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-deps - dependency-name: github.com/gophercloud/gophercloud dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-deps - dependency-name: github.com/jackc/pgtype dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-deps - dependency-name: github.com/jackc/pgx/v4 dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-deps - dependency-name: github.com/labstack/echo/v4 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-deps - dependency-name: github.com/openshift-online/ocm-sdk-go dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-deps - dependency-name: github.com/osbuild/images dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-deps - dependency-name: github.com/prometheus/client_golang dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-deps - dependency-name: golang.org/x/sync dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-deps - dependency-name: google.golang.org/api dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-deps ... Signed-off-by: dependabot[bot] <support@github.com>
270 lines
5.9 KiB
Go
270 lines
5.9 KiB
Go
// SPDX-License-Identifier: MIT
|
|
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
|
|
|
|
package middleware
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"html/template"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/gommon/bytes"
|
|
)
|
|
|
|
// StaticConfig defines the config for Static middleware.
|
|
type StaticConfig struct {
|
|
// Skipper defines a function to skip middleware.
|
|
Skipper Skipper
|
|
|
|
// Root directory from where the static content is served.
|
|
// Required.
|
|
Root string `yaml:"root"`
|
|
|
|
// Index file for serving a directory.
|
|
// Optional. Default value "index.html".
|
|
Index string `yaml:"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 `yaml:"html5"`
|
|
|
|
// Enable directory browsing.
|
|
// Optional. Default value false.
|
|
Browse bool `yaml:"browse"`
|
|
|
|
// Enable ignoring of the base of the URL path.
|
|
// Example: when assigning a static middleware to a non root path group,
|
|
// the filesystem path is not doubled
|
|
// Optional. Default value false.
|
|
IgnoreBase bool `yaml:"ignoreBase"`
|
|
|
|
// Filesystem provides access to the static content.
|
|
// Optional. Defaults to http.Dir(config.Root)
|
|
Filesystem http.FileSystem `yaml:"-"`
|
|
}
|
|
|
|
const html = `
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
|
<title>{{ .Name }}</title>
|
|
<style>
|
|
body {
|
|
font-family: Menlo, Consolas, monospace;
|
|
padding: 48px;
|
|
}
|
|
header {
|
|
padding: 4px 16px;
|
|
font-size: 24px;
|
|
}
|
|
ul {
|
|
list-style-type: none;
|
|
margin: 0;
|
|
padding: 20px 0 0 0;
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
}
|
|
li {
|
|
width: 300px;
|
|
padding: 16px;
|
|
}
|
|
li a {
|
|
display: block;
|
|
overflow: hidden;
|
|
white-space: nowrap;
|
|
text-overflow: ellipsis;
|
|
text-decoration: none;
|
|
transition: opacity 0.25s;
|
|
}
|
|
li span {
|
|
color: #707070;
|
|
font-size: 12px;
|
|
}
|
|
li a:hover {
|
|
opacity: 0.50;
|
|
}
|
|
.dir {
|
|
color: #E91E63;
|
|
}
|
|
.file {
|
|
color: #673AB7;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<header>
|
|
{{ .Name }}
|
|
</header>
|
|
<ul>
|
|
{{ range .Files }}
|
|
<li>
|
|
{{ if .Dir }}
|
|
{{ $name := print .Name "/" }}
|
|
<a class="dir" href="{{ $name }}">{{ $name }}</a>
|
|
{{ else }}
|
|
<a class="file" href="{{ .Name }}">{{ .Name }}</a>
|
|
<span>{{ .Size }}</span>
|
|
{{ end }}
|
|
</li>
|
|
{{ end }}
|
|
</ul>
|
|
</body>
|
|
</html>
|
|
`
|
|
|
|
// DefaultStaticConfig is the default Static middleware config.
|
|
var DefaultStaticConfig = StaticConfig{
|
|
Skipper: DefaultSkipper,
|
|
Index: "index.html",
|
|
}
|
|
|
|
// Static returns a Static middleware to serves static content from the provided
|
|
// root directory.
|
|
func Static(root string) echo.MiddlewareFunc {
|
|
c := DefaultStaticConfig
|
|
c.Root = root
|
|
return StaticWithConfig(c)
|
|
}
|
|
|
|
// StaticWithConfig returns a Static middleware with config.
|
|
// See `Static()`.
|
|
func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {
|
|
// Defaults
|
|
if config.Root == "" {
|
|
config.Root = "." // For security we want to restrict to CWD.
|
|
}
|
|
if config.Skipper == nil {
|
|
config.Skipper = DefaultStaticConfig.Skipper
|
|
}
|
|
if config.Index == "" {
|
|
config.Index = DefaultStaticConfig.Index
|
|
}
|
|
if config.Filesystem == nil {
|
|
config.Filesystem = http.Dir(config.Root)
|
|
config.Root = "."
|
|
}
|
|
|
|
// Index template
|
|
t, tErr := template.New("index").Parse(html)
|
|
if tErr != nil {
|
|
panic(fmt.Errorf("echo: %w", tErr))
|
|
}
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c echo.Context) (err error) {
|
|
if config.Skipper(c) {
|
|
return next(c)
|
|
}
|
|
|
|
p := c.Request().URL.Path
|
|
if strings.HasSuffix(c.Path(), "*") { // When serving from a group, e.g. `/static*`.
|
|
p = c.Param("*")
|
|
}
|
|
p, err = url.PathUnescape(p)
|
|
if err != nil {
|
|
return
|
|
}
|
|
name := path.Join(config.Root, path.Clean("/"+p)) // "/"+ for security
|
|
|
|
if config.IgnoreBase {
|
|
routePath := path.Base(strings.TrimRight(c.Path(), "/*"))
|
|
baseURLPath := path.Base(p)
|
|
if baseURLPath == routePath {
|
|
i := strings.LastIndex(name, routePath)
|
|
name = name[:i] + strings.Replace(name[i:], routePath, "", 1)
|
|
}
|
|
}
|
|
|
|
file, err := config.Filesystem.Open(name)
|
|
if err != nil {
|
|
if !isIgnorableOpenFileError(err) {
|
|
return err
|
|
}
|
|
|
|
// file with that path did not exist, so we continue down in middleware/handler chain, hoping that we end up in
|
|
// handler that is meant to handle this request
|
|
if err = next(c); err == nil {
|
|
return err
|
|
}
|
|
|
|
var he *echo.HTTPError
|
|
if !(errors.As(err, &he) && config.HTML5 && he.Code == http.StatusNotFound) {
|
|
return err
|
|
}
|
|
|
|
file, err = config.Filesystem.Open(path.Join(config.Root, config.Index))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
info, err := file.Stat()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if info.IsDir() {
|
|
index, err := config.Filesystem.Open(path.Join(name, config.Index))
|
|
if err != nil {
|
|
if config.Browse {
|
|
return listDir(t, name, file, c.Response())
|
|
}
|
|
|
|
return next(c)
|
|
}
|
|
|
|
defer index.Close()
|
|
|
|
info, err = index.Stat()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return serveFile(c, index, info)
|
|
}
|
|
|
|
return serveFile(c, file, info)
|
|
}
|
|
}
|
|
}
|
|
|
|
func serveFile(c echo.Context, file http.File, info os.FileInfo) error {
|
|
http.ServeContent(c.Response(), c.Request(), info.Name(), info.ModTime(), file)
|
|
return nil
|
|
}
|
|
|
|
func listDir(t *template.Template, name string, dir http.File, res *echo.Response) (err error) {
|
|
files, err := dir.Readdir(-1)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// Create directory index
|
|
res.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
|
|
data := struct {
|
|
Name string
|
|
Files []interface{}
|
|
}{
|
|
Name: name,
|
|
}
|
|
for _, f := range files {
|
|
data.Files = append(data.Files, struct {
|
|
Name string
|
|
Dir bool
|
|
Size string
|
|
}{f.Name(), f.IsDir(), bytes.Format(f.Size())})
|
|
}
|
|
return t.Execute(res, data)
|
|
}
|