diff --git a/caddyhttp/caddyhttp.go b/caddyhttp/caddyhttp.go index 8dacb0606..2f1d8dc7b 100644 --- a/caddyhttp/caddyhttp.go +++ b/caddyhttp/caddyhttp.go @@ -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" diff --git a/caddyhttp/caddyhttp_test.go b/caddyhttp/caddyhttp_test.go index 59857beba..f7417eae7 100644 --- a/caddyhttp/caddyhttp_test.go +++ b/caddyhttp/caddyhttp_test.go @@ -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) diff --git a/caddyhttp/httpserver/plugin.go b/caddyhttp/httpserver/plugin.go index 65d0a552c..c53ac767d 100644 --- a/caddyhttp/httpserver/plugin.go +++ b/caddyhttp/httpserver/plugin.go @@ -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", diff --git a/caddyhttp/index/index.go b/caddyhttp/index/index.go new file mode 100644 index 000000000..743f6b9e0 --- /dev/null +++ b/caddyhttp/index/index.go @@ -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 +} diff --git a/caddyhttp/index/index_test.go b/caddyhttp/index/index_test.go new file mode 100644 index 000000000..dfd879f39 --- /dev/null +++ b/caddyhttp/index/index_test.go @@ -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) + } + } +}