switch to master go-pkgz/rest #71

This commit is contained in:
Umputun 2021-05-14 13:47:44 -05:00
parent c3bb113305
commit 137cf692c4
7 changed files with 50 additions and 9 deletions

View File

@ -228,7 +228,7 @@ func (h *Http) proxyHandler() http.HandlerFunc {
h.Reporter.Report(w, http.StatusInternalServerError)
return
}
fs, err := R.FileServer(ae[0], ae[1])
fs, err := R.FileServer(ae[0], ae[1], nil)
if err != nil {
h.Reporter.Report(w, http.StatusInternalServerError)
return
@ -242,7 +242,7 @@ func (h *Http) assetsHandler() http.HandlerFunc {
if h.AssetsLocation == "" || h.AssetsWebRoot == "" {
return func(writer http.ResponseWriter, request *http.Request) {}
}
fs, err := R.FileServer(h.AssetsWebRoot, h.AssetsLocation)
fs, err := R.FileServer(h.AssetsWebRoot, h.AssetsLocation, nil)
if err != nil {
log.Printf("[WARN] can't initialize assets server, %v", err)
return func(writer http.ResponseWriter, request *http.Request) {}

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.16
require (
github.com/go-pkgz/lgr v0.10.4
github.com/go-pkgz/rest v1.9.2
github.com/go-pkgz/rest v1.9.3-0.20210514184429-77a1bddb51db
github.com/gorilla/handlers v1.5.1
github.com/prometheus/client_golang v1.10.0
github.com/stretchr/testify v1.7.0

2
go.sum
View File

@ -69,6 +69,8 @@ github.com/go-pkgz/lgr v0.10.4 h1:l7qyFjqEZgwRgaQQSEp6tve4A3OU80VrfzpvtEX8ngw=
github.com/go-pkgz/lgr v0.10.4/go.mod h1:CD0s1z6EFpIUplV067gitF77tn25JItzwHNKAPqeCF0=
github.com/go-pkgz/rest v1.9.2 h1:RyBBRXBYY6eBgTW3UGYOyT4VQPDiBBFh/tesELWsryQ=
github.com/go-pkgz/rest v1.9.2/go.mod h1:wZ/dGipZUaF9to0vIQl7PwDHgWQDB0jsrFg1xnAKLDw=
github.com/go-pkgz/rest v1.9.3-0.20210514184429-77a1bddb51db h1:PoIO+kDPc0A6m5xlRao4No1P9Ew4hdyZ4UFnX9fbanc=
github.com/go-pkgz/rest v1.9.3-0.20210514184429-77a1bddb51db/go.mod h1:wZ/dGipZUaF9to0vIQl7PwDHgWQDB0jsrFg1xnAKLDw=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s=

View File

@ -44,7 +44,7 @@ linters:
- varcheck
- stylecheck
- gochecknoinits
- scopelint
- exportloopref
- gocritic
- nakedret
- gosimple

View File

@ -2,6 +2,8 @@ package rest
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@ -12,7 +14,8 @@ import (
// prevents directory listing.
// - public defines base path of the url, i.e. for http://example.com/static/* it should be /static
// - local for the local path to the root of the served directory
func FileServer(public, local string) (http.Handler, error) {
// - notFound is the reader for the custom 404 html, can be nil for default
func FileServer(public, local string, notFound io.Reader) (http.Handler, error) {
root, err := filepath.Abs(local)
if err != nil {
@ -22,7 +25,8 @@ func FileServer(public, local string) (http.Handler, error) {
return nil, fmt.Errorf("local path %s doesn't exist: %w", root, err)
}
return http.StripPrefix(public, http.FileServer(noDirListingFS{http.Dir(root)})), nil
fs := http.StripPrefix(public, http.FileServer(noDirListingFS{http.Dir(root)}))
return custom404Handler(fs, notFound)
}
type noDirListingFS struct{ fs http.FileSystem }
@ -47,3 +51,38 @@ func (fs noDirListingFS) Open(name string) (http.File, error) {
}
return f, nil
}
// respWriter404 intercept Write to provide custom 404 response
type respWriter404 struct {
http.ResponseWriter
status int
msg []byte
}
func (w *respWriter404) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}
func (w *respWriter404) Write(p []byte) (n int, err error) {
if w.status != http.StatusNotFound || w.msg == nil {
return w.ResponseWriter.Write(p)
}
_, err = w.ResponseWriter.Write(w.msg)
return len(p), err
}
func custom404Handler(next http.Handler, notFound io.Reader) (http.Handler, error) {
if notFound == nil {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { next.ServeHTTP(w, r) }), nil
}
body, err := ioutil.ReadAll(notFound)
if err != nil {
return nil, err
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(&respWriter404{ResponseWriter: w, msg: body}, r)
}), nil
}

View File

@ -26,6 +26,8 @@ func SizeLimit(size int64) func(http.Handler) http.Handler {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
_ = r.Body.Close() // the original body already consumed
if int64(len(content)) > size {
w.WriteHeader(http.StatusRequestEntityTooLarge)
return

4
vendor/modules.txt vendored
View File

@ -1,5 +1,3 @@
# github.com/BurntSushi/toml v0.3.1
## explicit
# github.com/beorn7/perks v1.0.1
github.com/beorn7/perks/quantile
# github.com/cespare/xxhash/v2 v2.1.1
@ -11,7 +9,7 @@ github.com/felixge/httpsnoop
# github.com/go-pkgz/lgr v0.10.4
## explicit
github.com/go-pkgz/lgr
# github.com/go-pkgz/rest v1.9.2
# github.com/go-pkgz/rest v1.9.3-0.20210514184429-77a1bddb51db
## explicit
github.com/go-pkgz/rest
github.com/go-pkgz/rest/logger