0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2025-01-20 22:52:58 -05:00

caddyhttp: New index directive for alternate index file names (#1567)

* caddyhttp: Allow to alternate Index

* Move Index directive

* Fix misspelling outside this PR
This commit is contained in:
elcore 2017-04-17 19:02:44 +02:00 committed by Matt Holt
parent 6b66b19deb
commit a56a833423
5 changed files with 75 additions and 1 deletions

View file

@ -14,6 +14,7 @@ import (
_ "github.com/mholt/caddy/caddyhttp/fastcgi"
_ "github.com/mholt/caddy/caddyhttp/gzip"
_ "github.com/mholt/caddy/caddyhttp/header"
_ "github.com/mholt/caddy/caddyhttp/index"
_ "github.com/mholt/caddy/caddyhttp/internalsrv"
_ "github.com/mholt/caddy/caddyhttp/log"
_ "github.com/mholt/caddy/caddyhttp/markdown"

View file

@ -11,7 +11,7 @@ import (
// ensure that the standard plugins are in fact plugged in
// and registered properly; this is a quick/naive way to do it.
func TestStandardPlugins(t *testing.T) {
numStandardPlugins := 30 // importing caddyhttp plugs in this many plugins
numStandardPlugins := 31 // importing caddyhttp plugs in this many plugins
s := caddy.DescribePlugins()
if got, want := strings.Count(s, "\n"), numStandardPlugins+5; got != want {
t.Errorf("Expected all standard plugins to be plugged in, got:\n%s", s)

View file

@ -434,6 +434,7 @@ func RegisterDevDirective(name, before string) {
var directives = []string{
// primitive actions that set up the fundamental vitals of each config
"root",
"index",
"bind",
"maxrequestbody", // TODO: 'limits'
"timeouts",

33
caddyhttp/index/index.go Normal file
View file

@ -0,0 +1,33 @@
package index
import (
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/staticfiles"
)
func init() {
caddy.RegisterPlugin("index", caddy.Plugin{
ServerType: "http",
Action: setupIndex,
})
}
func setupIndex(c *caddy.Controller) error {
var index []string
for c.Next() {
args := c.RemainingArgs()
if len(args) == 0 {
return c.Errf("Expected at least one index")
}
for _, in := range args {
index = append(index, in)
}
staticfiles.IndexPages = index
}
return nil
}

View file

@ -0,0 +1,39 @@
package index
import (
"testing"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/staticfiles"
)
func TestIndexIncompleteParams(t *testing.T) {
c := caddy.NewTestController("", "index")
err := setupIndex(c)
if err == nil {
t.Error("Expected an error, but didn't get one")
}
}
func TestIndex(t *testing.T) {
c := caddy.NewTestController("", "index a.html b.html c.html")
err := setupIndex(c)
if err != nil {
t.Errorf("Expected no errors, got: %v", err)
}
expectedIndex := []string{"a.html", "b.html", "c.html"}
if len(staticfiles.IndexPages) != 3 {
t.Errorf("Expected 3 values, got %v", len(staticfiles.IndexPages))
}
// Ensure ordering is correct
for i, actual := range staticfiles.IndexPages {
if actual != expectedIndex[i] {
t.Errorf("Expected value in position %d to be %v, got %v", i, expectedIndex[i], actual)
}
}
}