mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-27 23:03:37 -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:
parent
6b66b19deb
commit
a56a833423
5 changed files with 75 additions and 1 deletions
|
@ -14,6 +14,7 @@ import (
|
||||||
_ "github.com/mholt/caddy/caddyhttp/fastcgi"
|
_ "github.com/mholt/caddy/caddyhttp/fastcgi"
|
||||||
_ "github.com/mholt/caddy/caddyhttp/gzip"
|
_ "github.com/mholt/caddy/caddyhttp/gzip"
|
||||||
_ "github.com/mholt/caddy/caddyhttp/header"
|
_ "github.com/mholt/caddy/caddyhttp/header"
|
||||||
|
_ "github.com/mholt/caddy/caddyhttp/index"
|
||||||
_ "github.com/mholt/caddy/caddyhttp/internalsrv"
|
_ "github.com/mholt/caddy/caddyhttp/internalsrv"
|
||||||
_ "github.com/mholt/caddy/caddyhttp/log"
|
_ "github.com/mholt/caddy/caddyhttp/log"
|
||||||
_ "github.com/mholt/caddy/caddyhttp/markdown"
|
_ "github.com/mholt/caddy/caddyhttp/markdown"
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
// ensure that the standard plugins are in fact plugged in
|
// ensure that the standard plugins are in fact plugged in
|
||||||
// and registered properly; this is a quick/naive way to do it.
|
// and registered properly; this is a quick/naive way to do it.
|
||||||
func TestStandardPlugins(t *testing.T) {
|
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()
|
s := caddy.DescribePlugins()
|
||||||
if got, want := strings.Count(s, "\n"), numStandardPlugins+5; got != want {
|
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)
|
t.Errorf("Expected all standard plugins to be plugged in, got:\n%s", s)
|
||||||
|
|
|
@ -434,6 +434,7 @@ func RegisterDevDirective(name, before string) {
|
||||||
var directives = []string{
|
var directives = []string{
|
||||||
// primitive actions that set up the fundamental vitals of each config
|
// primitive actions that set up the fundamental vitals of each config
|
||||||
"root",
|
"root",
|
||||||
|
"index",
|
||||||
"bind",
|
"bind",
|
||||||
"maxrequestbody", // TODO: 'limits'
|
"maxrequestbody", // TODO: 'limits'
|
||||||
"timeouts",
|
"timeouts",
|
||||||
|
|
33
caddyhttp/index/index.go
Normal file
33
caddyhttp/index/index.go
Normal 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
|
||||||
|
}
|
39
caddyhttp/index/index_test.go
Normal file
39
caddyhttp/index/index_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue