cmd/packserver: add --fallback-404 arg for Angular-style SPA support

This commit is contained in:
Laurence Withers 2020-02-15 12:31:42 +00:00
parent ca54fb8fbb
commit f70914aa38
4 changed files with 23 additions and 4 deletions

View File

@ -41,8 +41,15 @@ document.
This can be achieved with a number of methods. This can be achieved with a number of methods.
The simplest method is to tell `packserver` itself which resource to use
instead of returning an HTTP 404. Use the command line argument
`--fallback-404 /index.html` (or whichever named resource). The filename must
match a packed resource, so it will be preceded with a `/`. It must exist in
all packfiles being served.
If you have an nginx instance reverse proxying in front of `htpack`, then you If you have an nginx instance reverse proxying in front of `htpack`, then you
can use a couple of extra directives, for example: can use a couple of extra directives. This is very flexible as it lets you
override the resource for different routes. For example:
# prevent page loaded at "http://server.example/my-application" from # prevent page loaded at "http://server.example/my-application" from
# requesting resources at "/*" when it should request them at # requesting resources at "/*" when it should request them at

View File

@ -4,5 +4,5 @@ go 1.13
require ( require (
github.com/spf13/cobra v0.0.5 github.com/spf13/cobra v0.0.5
src.lwithers.me.uk/go/htpack v1.1.5 src.lwithers.me.uk/go/htpack v1.2.0
) )

View File

@ -38,5 +38,5 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
src.lwithers.me.uk/go/htpack v1.1.5 h1:2JzgqLZ1ROYc53+96NezfJ3S9fkwHZNd6QgJhMXnlSE= src.lwithers.me.uk/go/htpack v1.2.0 h1:z0bI8KalSD5kPYOcnuqZRPun80Ww0wbnjMkYYuv+xAI=
src.lwithers.me.uk/go/htpack v1.1.5/go.mod h1:JWofpm01RJbCTIyKfIPftUsxk6KlFkrYwyHgCVdKY+s= src.lwithers.me.uk/go/htpack v1.2.0/go.mod h1:JWofpm01RJbCTIyKfIPftUsxk6KlFkrYwyHgCVdKY+s=

View File

@ -49,6 +49,8 @@ func main() {
"Name of index file (index.html or similar)") "Name of index file (index.html or similar)")
rootCmd.Flags().Duration("expiry", 0, rootCmd.Flags().Duration("expiry", 0,
"Tell client how long it can cache data for; 0 means no caching") "Tell client how long it can cache data for; 0 means no caching")
rootCmd.Flags().String("fallback-404", "",
"Name of file to return if response would be 404 (spa.html or similar)")
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
@ -124,6 +126,12 @@ func run(c *cobra.Command, args []string) error {
return err return err
} }
// optional 404 fallback file
fallback404File, err := c.Flags().GetString("fallback-404")
if err != nil {
return err
}
// verify .htpack specifications // verify .htpack specifications
if len(args) == 0 { if len(args) == 0 {
return errors.New("must specify one or more .htpack files") return errors.New("must specify one or more .htpack files")
@ -157,6 +165,10 @@ func run(c *cobra.Command, args []string) error {
if indexFile != "" { if indexFile != "" {
packHandler.SetIndex(indexFile) packHandler.SetIndex(indexFile)
} }
if err = packHandler.SetNotFound(fallback404File); err != nil {
return fmt.Errorf("%s: fallback-404 resource %q "+
"not found in packfile", prefix, fallback404File)
}
handler := &addHeaders{ handler := &addHeaders{
extraHeaders: extraHeaders, extraHeaders: extraHeaders,