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
commit f70914aa38
4 changed files with 23 additions and 4 deletions

View file

@ -49,6 +49,8 @@ func main() {
"Name of index file (index.html or similar)")
rootCmd.Flags().Duration("expiry", 0,
"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 {
fmt.Fprintln(os.Stderr, err)
@ -124,6 +126,12 @@ func run(c *cobra.Command, args []string) error {
return err
}
// optional 404 fallback file
fallback404File, err := c.Flags().GetString("fallback-404")
if err != nil {
return err
}
// verify .htpack specifications
if len(args) == 0 {
return errors.New("must specify one or more .htpack files")
@ -157,6 +165,10 @@ func run(c *cobra.Command, args []string) error {
if 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{
extraHeaders: extraHeaders,