diff --git a/caddyhttp/caddyhttp.go b/caddyhttp/caddyhttp.go index 3c9c81ef..d9d97e9d 100644 --- a/caddyhttp/caddyhttp.go +++ b/caddyhttp/caddyhttp.go @@ -46,5 +46,4 @@ import ( _ "github.com/mholt/caddy/caddyhttp/timeouts" _ "github.com/mholt/caddy/caddyhttp/websocket" _ "github.com/mholt/caddy/onevent" - _ "github.com/mholt/caddy/startupshutdown" ) diff --git a/caddyhttp/caddyhttp_test.go b/caddyhttp/caddyhttp_test.go index 56ec884e..ca3b97e1 100644 --- a/caddyhttp/caddyhttp_test.go +++ b/caddyhttp/caddyhttp_test.go @@ -25,7 +25,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 := 33 // 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/startupshutdown/startupshutdown.go b/startupshutdown/startupshutdown.go deleted file mode 100644 index 72fe9225..00000000 --- a/startupshutdown/startupshutdown.go +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2015 Light Code Labs, LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package startupshutdown - -import ( - "fmt" - "strings" - - "github.com/google/uuid" - "github.com/mholt/caddy" - "github.com/mholt/caddy/onevent/hook" -) - -func init() { - caddy.RegisterPlugin("startup", caddy.Plugin{Action: Startup}) - caddy.RegisterPlugin("shutdown", caddy.Plugin{Action: Shutdown}) -} - -// Startup (an alias for 'on startup') registers a startup callback to execute during server start. -func Startup(c *caddy.Controller) error { - config, err := onParse(c, caddy.InstanceStartupEvent) - if err != nil { - return c.ArgErr() - } - - // Register Event Hooks. - c.OncePerServerBlock(func() error { - for _, cfg := range config { - caddy.RegisterEventHook("on-"+cfg.ID, cfg.Hook) - } - return nil - }) - - fmt.Println("NOTICE: Startup directive will be removed in a later version. Please migrate to 'on startup'") - - return nil -} - -// Shutdown (an alias for 'on shutdown') registers a shutdown callback to execute during server start. -func Shutdown(c *caddy.Controller) error { - config, err := onParse(c, caddy.ShutdownEvent) - if err != nil { - return c.ArgErr() - } - - // Register Event Hooks. - for _, cfg := range config { - caddy.RegisterEventHook("on-"+cfg.ID, cfg.Hook) - } - - fmt.Println("NOTICE: Shutdown directive will be removed in a later version. Please migrate to 'on shutdown'") - - return nil -} - -func onParse(c *caddy.Controller, event caddy.EventName) ([]*hook.Config, error) { - var config []*hook.Config - - for c.Next() { - cfg := new(hook.Config) - - args := c.RemainingArgs() - if len(args) == 0 { - return config, c.ArgErr() - } - - // Configure Event. - cfg.Event = event - - // Assign an unique ID. - cfg.ID = uuid.New().String() - - // Extract command and arguments. - command, args, err := caddy.SplitCommandAndArgs(strings.Join(args, " ")) - if err != nil { - return config, c.Err(err.Error()) - } - - cfg.Command = command - cfg.Args = args - - config = append(config, cfg) - } - - return config, nil -} diff --git a/startupshutdown/startupshutdown_test.go b/startupshutdown/startupshutdown_test.go deleted file mode 100644 index e68afd17..00000000 --- a/startupshutdown/startupshutdown_test.go +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2015 Light Code Labs, LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package startupshutdown - -import ( - "testing" - - "github.com/mholt/caddy" -) - -func TestStartup(t *testing.T) { - tests := []struct { - name string - input string - shouldErr bool - }{ - {name: "noInput", input: "startup", shouldErr: true}, - {name: "startup", input: "startup cmd arg", shouldErr: false}, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - c := caddy.NewTestController("", test.input) - - err := Startup(c) - if err == nil && test.shouldErr { - t.Error("Test didn't error, but it should have") - } else if err != nil && !test.shouldErr { - t.Errorf("Test errored, but it shouldn't have; got '%v'", err) - } - }) - } -} - -func TestShutdown(t *testing.T) { - tests := []struct { - name string - input string - shouldErr bool - }{ - {name: "noInput", input: "shutdown", shouldErr: true}, - {name: "shutdown", input: "shutdown cmd arg", shouldErr: false}, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - c := caddy.NewTestController("", test.input) - - err := Shutdown(c) - if err == nil && test.shouldErr { - t.Error("Test didn't error, but it should have") - } else if err != nil && !test.shouldErr { - t.Errorf("Test errored, but it shouldn't have; got '%v'", err) - } - }) - } -}