mirror of
https://github.com/umputun/reproxy.git
synced 2024-09-11 08:55:25 +03:00
add support of custom 404 page for assets server
This commit is contained in:
parent
2e8733b152
commit
64f57df860
@ -196,6 +196,8 @@ There are two ways to set cache duration:
|
||||
1. A single value for all static assets. This is as simple as `--assets.cache=48h`.
|
||||
2. Custom duration for different mime types. It should include two parts - the default value and the pairs of mime:duration. In command line this looks like multiple `--assets.cache` options, i.e. `--assets.cache=48h --assets.cache=text/html:24h --assets.cache=image/png:2h`. Environment values should be comma-separated, i.e. `ASSETS_CACHE=48h,text/html:24h,image/png:2h`
|
||||
|
||||
Custom 404 (not found) page can be set with `--assets.404=<path>` parameter. The path should be relative to the assets root.
|
||||
|
||||
## Using reproxy as a base image
|
||||
|
||||
Serving purely static content is one of the popular use cases. Usually this used for the separate frontend container providing UI only. With the assets server such a container is almost trivial to make. This is an example from the container serving [reproxy.io](http://reproxy.io)
|
||||
@ -365,6 +367,7 @@ assets:
|
||||
--assets.root= assets web root (default: /) [$ASSETS_ROOT]
|
||||
--assets.spa spa treatment for assets [$ASSETS_SPA]
|
||||
--assets.cache= cache duration for assets [$ASSETS_CACHE]
|
||||
--assets.not-found= path to file to serve on 404, relative to location [$ASSETS_NOT_FOUND]
|
||||
|
||||
logger:
|
||||
--logger.stdout enable stdout logging [$LOGGER_STDOUT]
|
||||
|
@ -53,6 +53,7 @@ var opts struct {
|
||||
WebRoot string `long:"root" env:"ROOT" default:"/" description:"assets web root"`
|
||||
SPA bool `long:"spa" env:"SPA" description:"spa treatment for assets"`
|
||||
CacheControl []string `long:"cache" env:"CACHE" description:"cache duration for assets" env-delim:","`
|
||||
NotFound string `long:"not-found" env:"NOT_FOUND" description:"path to file to serve on 404, relative to location"`
|
||||
} `group:"assets" namespace:"assets" env-namespace:"ASSETS"`
|
||||
|
||||
Logger struct {
|
||||
@ -240,6 +241,7 @@ func run() error {
|
||||
MaxBodySize: int64(maxBodySize),
|
||||
AssetsLocation: opts.Assets.Location,
|
||||
AssetsWebRoot: opts.Assets.WebRoot,
|
||||
Assets404: opts.Assets.NotFound,
|
||||
AssetsSPA: opts.Assets.SPA,
|
||||
CacheControl: cacheControl,
|
||||
GzEnabled: opts.GzipEnabled,
|
||||
|
@ -1,6 +1,7 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -9,6 +10,8 @@ import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -28,6 +31,7 @@ type Http struct { // nolint golint
|
||||
Address string
|
||||
AssetsLocation string
|
||||
AssetsWebRoot string
|
||||
Assets404 string
|
||||
AssetsSPA bool
|
||||
MaxBodySize int64
|
||||
GzEnabled bool
|
||||
@ -91,6 +95,9 @@ func (h *Http) Run(ctx context.Context) error {
|
||||
|
||||
if h.AssetsLocation != "" {
|
||||
log.Printf("[DEBUG] assets file server enabled for %s, webroot %s", h.AssetsLocation, h.AssetsWebRoot)
|
||||
if h.Assets404 != "" {
|
||||
log.Printf("[DEBUG] assets 404 file enabled for %s", h.Assets404)
|
||||
}
|
||||
}
|
||||
|
||||
if h.LBSelector == nil {
|
||||
@ -260,7 +267,7 @@ func (h *Http) proxyHandler() http.HandlerFunc {
|
||||
h.Reporter.Report(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
fs, err := h.fileServer(ae[0], ae[1], ae[2] == "spa")
|
||||
fs, err := h.fileServer(ae[0], ae[1], ae[2] == "spa", nil)
|
||||
if err != nil {
|
||||
log.Printf("[WARN] file server error, %v", err)
|
||||
h.Reporter.Report(w, http.StatusInternalServerError)
|
||||
@ -327,8 +334,20 @@ func (h *Http) assetsHandler() http.HandlerFunc {
|
||||
if h.AssetsLocation == "" || h.AssetsWebRoot == "" {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {}
|
||||
}
|
||||
log.Printf("[DEBUG] shared assets server enabled for %s %s, spa=%v", h.AssetsWebRoot, h.AssetsLocation, h.AssetsSPA)
|
||||
fs, err := h.fileServer(h.AssetsWebRoot, h.AssetsLocation, h.AssetsSPA)
|
||||
|
||||
var notFound []byte
|
||||
var err error
|
||||
if h.Assets404 != "" {
|
||||
if notFound, err = os.ReadFile(filepath.Join(h.AssetsLocation, h.Assets404)); err != nil {
|
||||
log.Printf("[WARN] can't read 404 file %s, %v", h.Assets404, err)
|
||||
notFound = nil
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[DEBUG] shared assets server enabled for %s %s, spa=%v, not-found=%q",
|
||||
h.AssetsLocation, h.AssetsWebRoot, h.AssetsSPA, h.Assets404)
|
||||
|
||||
fs, err := h.fileServer(h.AssetsWebRoot, h.AssetsLocation, h.AssetsSPA, notFound)
|
||||
if err != nil {
|
||||
log.Printf("[WARN] can't initialize assets server, %v", err)
|
||||
return func(writer http.ResponseWriter, request *http.Request) {}
|
||||
@ -336,11 +355,15 @@ func (h *Http) assetsHandler() http.HandlerFunc {
|
||||
return h.CacheControl.Middleware(fs).ServeHTTP
|
||||
}
|
||||
|
||||
func (h *Http) fileServer(assetsWebRoot, assetsLocation string, spa bool) (http.Handler, error) {
|
||||
if spa {
|
||||
return R.FileServerSPA(assetsWebRoot, assetsLocation, nil)
|
||||
func (h *Http) fileServer(assetsWebRoot, assetsLocation string, spa bool, notFound []byte) (http.Handler, error) {
|
||||
var notFoundReader io.Reader
|
||||
if notFound != nil {
|
||||
notFoundReader = bytes.NewReader(notFound)
|
||||
}
|
||||
return R.FileServer(assetsWebRoot, assetsLocation, nil)
|
||||
if spa {
|
||||
return R.FileServerSPA(assetsWebRoot, assetsLocation, notFoundReader)
|
||||
}
|
||||
return R.FileServer(assetsWebRoot, assetsLocation, notFoundReader)
|
||||
}
|
||||
|
||||
func (h *Http) isAssetRequest(r *http.Request) bool {
|
||||
|
@ -189,6 +189,9 @@ func TestHttp_DoWithAssets(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "404 page not found\n", string(body))
|
||||
}
|
||||
|
||||
{
|
||||
@ -203,6 +206,85 @@ func TestHttp_DoWithAssets(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttp_DoWithAssetsCustom404(t *testing.T) {
|
||||
port := rand.Intn(10000) + 40000
|
||||
cc := NewCacheControl(time.Hour * 12)
|
||||
h := Http{Timeouts: Timeouts{ResponseHeader: 200 * time.Millisecond}, Address: fmt.Sprintf("127.0.0.1:%d", port),
|
||||
AccessLog: io.Discard, AssetsWebRoot: "/static", AssetsLocation: "testdata", Assets404: "404.html",
|
||||
CacheControl: cc, Reporter: &ErrorReporter{Nice: false}}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1000*time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
ds := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Logf("req: %v", r)
|
||||
w.Header().Add("h1", "v1")
|
||||
require.Equal(t, "127.0.0.1", r.Header.Get("X-Real-IP"))
|
||||
fmt.Fprintf(w, "response %s", r.URL.String())
|
||||
}))
|
||||
|
||||
svc := discovery.NewService([]discovery.Provider{
|
||||
&provider.Static{Rules: []string{
|
||||
"localhost,^/api/(.*)," + ds.URL + "/123/$1,",
|
||||
"127.0.0.1,^/api/(.*)," + ds.URL + "/567/$1,",
|
||||
},
|
||||
}}, time.Millisecond*10)
|
||||
|
||||
go func() {
|
||||
_ = svc.Run(context.Background())
|
||||
}()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
h.Matcher = svc
|
||||
h.Metrics = mgmt.NewMetrics()
|
||||
|
||||
go func() {
|
||||
_ = h.Run(ctx)
|
||||
}()
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
client := http.Client{}
|
||||
|
||||
{
|
||||
req, err := http.NewRequest("GET", "http://127.0.0.1:"+strconv.Itoa(port)+"/api/something", http.NoBody)
|
||||
require.NoError(t, err)
|
||||
resp, err := client.Do(req)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
t.Logf("%+v", resp.Header)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "response /567/something", string(body))
|
||||
assert.Equal(t, "", resp.Header.Get("App-Method"))
|
||||
assert.Equal(t, "v1", resp.Header.Get("h1"))
|
||||
}
|
||||
|
||||
{
|
||||
resp, err := client.Get("http://localhost:" + strconv.Itoa(port) + "/static/1.html")
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
t.Logf("%+v", resp.Header)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "test html", string(body))
|
||||
assert.Equal(t, "", resp.Header.Get("App-Method"))
|
||||
assert.Equal(t, "", resp.Header.Get("h1"))
|
||||
assert.Equal(t, "public, max-age=43200", resp.Header.Get("Cache-Control"))
|
||||
}
|
||||
|
||||
{
|
||||
resp, err := client.Get("http://localhost:" + strconv.Itoa(port) + "/static/bad.html")
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "not found! blah blah blah\nthere is no spoon", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttp_DoWithSpaAssets(t *testing.T) {
|
||||
port := rand.Intn(10000) + 40000
|
||||
cc := NewCacheControl(time.Hour * 12)
|
||||
|
2
app/proxy/testdata/404.html
vendored
Normal file
2
app/proxy/testdata/404.html
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
not found! blah blah blah
|
||||
there is no spoon
|
Loading…
Reference in New Issue
Block a user