startupshutdown.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2015 Light Code Labs, LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package startupshutdown
  15. import (
  16. "fmt"
  17. "strings"
  18. "github.com/google/uuid"
  19. "github.com/mholt/caddy"
  20. "github.com/mholt/caddy/onevent/hook"
  21. )
  22. func init() {
  23. caddy.RegisterPlugin("startup", caddy.Plugin{Action: Startup})
  24. caddy.RegisterPlugin("shutdown", caddy.Plugin{Action: Shutdown})
  25. }
  26. // Startup (an alias for 'on startup') registers a startup callback to execute during server start.
  27. func Startup(c *caddy.Controller) error {
  28. config, err := onParse(c, caddy.InstanceStartupEvent)
  29. if err != nil {
  30. return c.ArgErr()
  31. }
  32. // Register Event Hooks.
  33. c.OncePerServerBlock(func() error {
  34. for _, cfg := range config {
  35. caddy.RegisterEventHook("on-"+cfg.ID, cfg.Hook)
  36. }
  37. return nil
  38. })
  39. fmt.Println("NOTICE: Startup directive will be removed in a later version. Please migrate to 'on startup'")
  40. return nil
  41. }
  42. // Shutdown (an alias for 'on shutdown') registers a shutdown callback to execute during server start.
  43. func Shutdown(c *caddy.Controller) error {
  44. config, err := onParse(c, caddy.ShutdownEvent)
  45. if err != nil {
  46. return c.ArgErr()
  47. }
  48. // Register Event Hooks.
  49. for _, cfg := range config {
  50. caddy.RegisterEventHook("on-"+cfg.ID, cfg.Hook)
  51. }
  52. fmt.Println("NOTICE: Shutdown directive will be removed in a later version. Please migrate to 'on shutdown'")
  53. return nil
  54. }
  55. func onParse(c *caddy.Controller, event caddy.EventName) ([]*hook.Config, error) {
  56. var config []*hook.Config
  57. for c.Next() {
  58. cfg := new(hook.Config)
  59. args := c.RemainingArgs()
  60. if len(args) == 0 {
  61. return config, c.ArgErr()
  62. }
  63. // Configure Event.
  64. cfg.Event = event
  65. // Assign an unique ID.
  66. cfg.ID = uuid.New().String()
  67. // Extract command and arguments.
  68. command, args, err := caddy.SplitCommandAndArgs(strings.Join(args, " "))
  69. if err != nil {
  70. return config, c.Err(err.Error())
  71. }
  72. cfg.Command = command
  73. cfg.Args = args
  74. config = append(config, cfg)
  75. }
  76. return config, nil
  77. }