mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-24 01:26:47 +01:00
Markdown: Modify development mode to generate links on page requests.
This commit is contained in:
parent
851026d3fa
commit
2ab466599d
5 changed files with 37 additions and 37 deletions
|
@ -36,10 +36,8 @@ func Markdown(c *Controller) (middleware.Middleware, error) {
|
||||||
if err := markdown.GenerateLinks(md, cfg); err != nil {
|
if err := markdown.GenerateLinks(md, cfg); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Watch file changes for links generation.
|
// Watch file changes for links generation if not in development mode.
|
||||||
if cfg.Development {
|
if !cfg.Development {
|
||||||
markdown.Watch(md, cfg, 0)
|
|
||||||
} else {
|
|
||||||
markdown.Watch(md, cfg, markdown.DefaultInterval)
|
markdown.Watch(md, cfg, markdown.DefaultInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ package markdown
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -119,6 +120,13 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
||||||
return http.StatusNotFound, nil
|
return http.StatusNotFound, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if development is set, scan directory for file changes for links.
|
||||||
|
if m.Development {
|
||||||
|
if err := GenerateLinks(md, m); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if static site is generated, attempt to use it
|
// if static site is generated, attempt to use it
|
||||||
if filepath, ok := m.StaticFiles[fpath]; ok {
|
if filepath, ok := m.StaticFiles[fpath]; ok {
|
||||||
if fs1, err := os.Stat(filepath); err == nil {
|
if fs1, err := os.Stat(filepath); err == nil {
|
||||||
|
|
|
@ -199,7 +199,7 @@ func getTrue() bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// attempt to trigger race condition
|
// attempt to trigger race conditions
|
||||||
var w sync.WaitGroup
|
var w sync.WaitGroup
|
||||||
f := func() {
|
f := func() {
|
||||||
req, err := http.NewRequest("GET", "/log/test.md", nil)
|
req, err := http.NewRequest("GET", "/log/test.md", nil)
|
||||||
|
@ -217,6 +217,16 @@ func getTrue() bool {
|
||||||
}
|
}
|
||||||
w.Wait()
|
w.Wait()
|
||||||
|
|
||||||
|
f = func() {
|
||||||
|
GenerateLinks(md, &md.Configs[0])
|
||||||
|
w.Done()
|
||||||
|
}
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
w.Add(1)
|
||||||
|
go f()
|
||||||
|
}
|
||||||
|
w.Wait()
|
||||||
|
|
||||||
if err = os.RemoveAll(DefaultStaticDir); err != nil {
|
if err = os.RemoveAll(DefaultStaticDir); err != nil {
|
||||||
t.Errorf("Error while removing the generated static files: %v", err)
|
t.Errorf("Error while removing the generated static files: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,6 +79,7 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) {
|
||||||
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
if _, err := os.Stat(fp); os.IsNotExist(err) {
|
||||||
l.Lock()
|
l.Lock()
|
||||||
l.lastErr = err
|
l.lastErr = err
|
||||||
|
l.generating = false
|
||||||
l.Unlock()
|
l.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -87,6 +88,9 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) {
|
||||||
|
|
||||||
// same hash, return.
|
// same hash, return.
|
||||||
if err == nil && hash == cfg.linksHash {
|
if err == nil && hash == cfg.linksHash {
|
||||||
|
l.Lock()
|
||||||
|
l.generating = false
|
||||||
|
l.Unlock()
|
||||||
return
|
return
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
log.Println("Error:", err)
|
log.Println("Error:", err)
|
||||||
|
|
|
@ -20,7 +20,6 @@ func Watch(md Markdown, c *Config, interval time.Duration) (stopChan chan struct
|
||||||
func TickerFunc(interval time.Duration, f func()) chan struct{} {
|
func TickerFunc(interval time.Duration, f func()) chan struct{} {
|
||||||
stopChan := make(chan struct{})
|
stopChan := make(chan struct{})
|
||||||
|
|
||||||
if interval > 0 {
|
|
||||||
ticker := time.NewTicker(interval)
|
ticker := time.NewTicker(interval)
|
||||||
go func() {
|
go func() {
|
||||||
loop:
|
loop:
|
||||||
|
@ -34,25 +33,6 @@ func TickerFunc(interval time.Duration, f func()) chan struct{} {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
} else {
|
|
||||||
go func() {
|
|
||||||
loop:
|
|
||||||
for {
|
|
||||||
m := make(chan struct{})
|
|
||||||
go func() {
|
|
||||||
f()
|
|
||||||
m <- struct{}{}
|
|
||||||
}()
|
|
||||||
select {
|
|
||||||
case <-m:
|
|
||||||
continue loop
|
|
||||||
case <-stopChan:
|
|
||||||
break loop
|
|
||||||
}
|
|
||||||
time.Sleep(DevInterval)
|
|
||||||
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
return stopChan
|
return stopChan
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue