本文整理匯總了Golang中github.com/mholt/caddy.Controller.OncePerServerBlock方法的典型用法代碼示例。如果您正苦於以下問題:Golang Controller.OncePerServerBlock方法的具體用法?Golang Controller.OncePerServerBlock怎麽用?Golang Controller.OncePerServerBlock使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/mholt/caddy.Controller
的用法示例。
在下文中一共展示了Controller.OncePerServerBlock方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: setup
// setup configures a new Git service routine.
func setup(c *caddy.Controller) error {
git, err := parse(c)
if err != nil {
return err
}
// repos configured with webhooks
var hookRepos []*Repo
// functions to execute at startup
var startupFuncs []func() error
// loop through all repos and and start monitoring
for i := range git {
repo := git.Repo(i)
// If a HookUrl is set, we switch to event based pulling.
// Install the url handler
if repo.Hook.URL != "" {
hookRepos = append(hookRepos, repo)
startupFuncs = append(startupFuncs, func() error {
return repo.Pull()
})
} else {
startupFuncs = append(startupFuncs, func() error {
// Start service routine in background
Start(repo)
// Do a pull right away to return error
return repo.Pull()
})
}
}
// ensure the functions are executed once per server block
// for cases like server1.com, server2.com { ... }
c.OncePerServerBlock(func() error {
for i := range startupFuncs {
c.OnStartup(startupFuncs[i])
}
return nil
})
// if there are repo(s) with webhook
// return handler
if len(hookRepos) > 0 {
webhook := &WebHook{Repos: hookRepos}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
webhook.Next = next
return webhook
})
}
return nil
}
示例2: registerCallback
// registerCallback registers a callback function to execute by
// using c to parse the directive. It registers the callback
// to be executed using registerFunc.
func registerCallback(c *caddy.Controller, registerFunc func(func() error)) error {
var funcs []func() error
for c.Next() {
args := c.RemainingArgs()
if len(args) == 0 {
return c.ArgErr()
}
nonblock := false
if len(args) > 1 && args[len(args)-1] == "&" {
// Run command in background; non-blocking
nonblock = true
args = args[:len(args)-1]
}
command, args, err := caddy.SplitCommandAndArgs(strings.Join(args, " "))
if err != nil {
return c.Err(err.Error())
}
fn := func() error {
cmd := exec.Command(command, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if nonblock {
log.Printf("[INFO] Nonblocking Command:\"%s %s\"", command, strings.Join(args, " "))
return cmd.Start()
}
log.Printf("[INFO] Blocking Command:\"%s %s\"", command, strings.Join(args, " "))
return cmd.Run()
}
funcs = append(funcs, fn)
}
return c.OncePerServerBlock(func() error {
for _, fn := range funcs {
registerFunc(fn)
}
return nil
})
}