mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-29 20:24:11 +08:00
43 lines
948 B
Go
43 lines
948 B
Go
package markdown
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
// DefaultInterval is the default interval at which the markdown watcher
|
|
// checks for changes.
|
|
const DefaultInterval = time.Second * 60
|
|
|
|
// Watch monitors the configured markdown directory for changes. It calls GenerateLinks
|
|
// when there are changes.
|
|
func Watch(md Markdown, c *Config, interval time.Duration) (stopChan chan struct{}) {
|
|
return TickerFunc(interval, func() {
|
|
if err := GenerateStatic(md, c); err != nil {
|
|
log.Printf("[ERROR] markdown: Re-generating static site: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TickerFunc runs f at interval. A message to the returned channel will stop the
|
|
// executing goroutine.
|
|
func TickerFunc(interval time.Duration, f func()) chan struct{} {
|
|
stopChan := make(chan struct{})
|
|
|
|
ticker := time.NewTicker(interval)
|
|
go func() {
|
|
loop:
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
f()
|
|
case <-stopChan:
|
|
ticker.Stop()
|
|
break loop
|
|
}
|
|
}
|
|
}()
|
|
|
|
return stopChan
|
|
}
|