controller.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 caddy
  15. import (
  16. "strings"
  17. "github.com/mholt/caddy/caddyfile"
  18. )
  19. // Controller is given to the setup function of directives which
  20. // gives them access to be able to read tokens with which to
  21. // configure themselves. It also stores state for the setup
  22. // functions, can get the current context, and can be used to
  23. // identify a particular server block using the Key field.
  24. type Controller struct {
  25. caddyfile.Dispenser
  26. // The instance in which the setup is occurring
  27. instance *Instance
  28. // Key is the key from the top of the server block, usually
  29. // an address, hostname, or identifier of some sort.
  30. Key string
  31. // OncePerServerBlock is a function that executes f
  32. // exactly once per server block, no matter how many
  33. // hosts are associated with it. If it is the first
  34. // time, the function f is executed immediately
  35. // (not deferred) and may return an error which is
  36. // returned by OncePerServerBlock.
  37. OncePerServerBlock func(f func() error) error
  38. // ServerBlockIndex is the 0-based index of the
  39. // server block as it appeared in the input.
  40. ServerBlockIndex int
  41. // ServerBlockKeyIndex is the 0-based index of this
  42. // key as it appeared in the input at the head of the
  43. // server block.
  44. ServerBlockKeyIndex int
  45. // ServerBlockKeys is a list of keys that are
  46. // associated with this server block. All these
  47. // keys, consequently, share the same tokens.
  48. ServerBlockKeys []string
  49. // ServerBlockStorage is used by a directive's
  50. // setup function to persist state between all
  51. // the keys on a server block.
  52. ServerBlockStorage interface{}
  53. }
  54. // ServerType gets the name of the server type that is being set up.
  55. func (c *Controller) ServerType() string {
  56. return c.instance.serverType
  57. }
  58. // OnFirstStartup adds fn to the list of callback functions to execute
  59. // when the server is about to be started NOT as part of a restart.
  60. func (c *Controller) OnFirstStartup(fn func() error) {
  61. c.instance.onFirstStartup = append(c.instance.onFirstStartup, fn)
  62. }
  63. // OnStartup adds fn to the list of callback functions to execute
  64. // when the server is about to be started (including restarts).
  65. func (c *Controller) OnStartup(fn func() error) {
  66. c.instance.onStartup = append(c.instance.onStartup, fn)
  67. }
  68. // OnRestart adds fn to the list of callback functions to execute
  69. // when the server is about to be restarted.
  70. func (c *Controller) OnRestart(fn func() error) {
  71. c.instance.onRestart = append(c.instance.onRestart, fn)
  72. }
  73. // OnShutdown adds fn to the list of callback functions to execute
  74. // when the server is about to be shut down (including restarts).
  75. func (c *Controller) OnShutdown(fn func() error) {
  76. c.instance.onShutdown = append(c.instance.onShutdown, fn)
  77. }
  78. // OnFinalShutdown adds fn to the list of callback functions to execute
  79. // when the server is about to be shut down NOT as part of a restart.
  80. func (c *Controller) OnFinalShutdown(fn func() error) {
  81. c.instance.onFinalShutdown = append(c.instance.onFinalShutdown, fn)
  82. }
  83. // Context gets the context associated with the instance associated with c.
  84. func (c *Controller) Context() Context {
  85. return c.instance.context
  86. }
  87. // Get safely gets a value from the Instance's storage.
  88. func (c *Controller) Get(key interface{}) interface{} {
  89. c.instance.StorageMu.RLock()
  90. defer c.instance.StorageMu.RUnlock()
  91. return c.instance.Storage[key]
  92. }
  93. // Set safely sets a value on the Instance's storage.
  94. func (c *Controller) Set(key, val interface{}) {
  95. c.instance.StorageMu.Lock()
  96. c.instance.Storage[key] = val
  97. c.instance.StorageMu.Unlock()
  98. }
  99. // NewTestController creates a new Controller for
  100. // the server type and input specified. The filename
  101. // is "Testfile". If the server type is not empty and
  102. // is plugged in, a context will be created so that
  103. // the results of setup functions can be checked for
  104. // correctness.
  105. //
  106. // Used only for testing, but exported so plugins can
  107. // use this for convenience.
  108. func NewTestController(serverType, input string) *Controller {
  109. testInst := &Instance{serverType: serverType, Storage: make(map[interface{}]interface{})}
  110. if stype, err := getServerType(serverType); err == nil {
  111. testInst.context = stype.NewContext(testInst)
  112. }
  113. return &Controller{
  114. instance: testInst,
  115. Dispenser: caddyfile.NewDispenser("Testfile", strings.NewReader(input)),
  116. OncePerServerBlock: func(f func() error) error { return f() },
  117. }
  118. }