cmd/packserver: add --index-file argument

This commit is contained in:
Laurence Withers 2019-04-25 14:45:18 +01:00
parent be86cbaed3
commit 246aefbec4
3 changed files with 18 additions and 5 deletions

View File

@ -5,7 +5,7 @@ go 1.12
require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kisielk/errcheck v1.2.0 // indirect
github.com/lwithers/htpack v1.0.0
github.com/lwithers/htpack v1.1.0
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a // indirect

View File

@ -9,6 +9,8 @@ github.com/lwithers/htpack v0.0.0-20190412081623-ea77f42dc393 h1:h++VdZ2eeJC9hf+
github.com/lwithers/htpack v0.0.0-20190412081623-ea77f42dc393/go.mod h1:+9noAoJ9IIiHkwn2Z2Po5upZOKItKKFgYr/cMESGYrc=
github.com/lwithers/htpack v1.0.0 h1:opBavUAl6QKjvlxNaOwMAvO+Q+ytZpKSl0iDCYam1Uk=
github.com/lwithers/htpack v1.0.0/go.mod h1:4dNHChTcK0SzOTVnFt4b0SuK7OMSo8Ge7o1XXYV4xUk=
github.com/lwithers/htpack v1.1.0 h1:pURTwBKgcmLYpN8M+qT9/Ks2+kLy8cbQqgJZa6/QPaw=
github.com/lwithers/htpack v1.1.0/go.mod h1:4dNHChTcK0SzOTVnFt4b0SuK7OMSo8Ge7o1XXYV4xUk=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=

View File

@ -45,6 +45,8 @@ func main() {
"Extra headers; use flag once for each, in form -H header=value")
rootCmd.Flags().String("header-file", "",
"Path to text file containing one line for each header=value to add")
rootCmd.Flags().String("index-file", "",
"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")
@ -115,6 +117,13 @@ func run(c *cobra.Command, args []string) error {
fmt.Sprintf("public, max-age=%d", expiry/1e9))
}
// optional index file
// NB: this is set below, as the handlers are instantiated
indexFile, err := c.Flags().GetString("index-file")
if err != nil {
return err
}
// verify .htpack specifications
if len(args) == 0 {
return errors.New("must specify one or more .htpack files")
@ -141,15 +150,17 @@ func run(c *cobra.Command, args []string) error {
// load packfiles, registering handlers as we go
for prefix, packfile := range packPaths {
var handler http.Handler
handler, err = htpack.New(packfile)
packHandler, err := htpack.New(packfile)
if err != nil {
return err
}
if indexFile != "" {
packHandler.SetIndex(indexFile)
}
handler = &addHeaders{
handler := &addHeaders{
extraHeaders: extraHeaders,
handler: handler,
handler: packHandler,
}
if prefix != "/" {