|
|
@ -22,6 +22,10 @@ import ( |
|
|
|
"github.com/gorilla/websocket" |
|
|
|
"github.com/gorilla/websocket" |
|
|
|
"github.com/mholt/caddy" |
|
|
|
"github.com/mholt/caddy" |
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver" |
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver" |
|
|
|
|
|
|
|
"github.com/spf13/afero" |
|
|
|
|
|
|
|
"github.com/spf13/hugo/deps" |
|
|
|
|
|
|
|
"github.com/spf13/hugo/hugofs" |
|
|
|
|
|
|
|
"github.com/spf13/hugo/hugolib" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
const ( |
|
|
@ -51,6 +55,8 @@ type docref struct { |
|
|
|
type CaddyHugo struct { |
|
|
|
type CaddyHugo struct { |
|
|
|
ServerType string |
|
|
|
ServerType string |
|
|
|
Site *httpserver.SiteConfig |
|
|
|
Site *httpserver.SiteConfig |
|
|
|
|
|
|
|
HugoSites *hugolib.HugoSites |
|
|
|
|
|
|
|
HugoCfg *deps.DepsCfg |
|
|
|
|
|
|
|
|
|
|
|
Media *MediaSource |
|
|
|
Media *MediaSource |
|
|
|
|
|
|
|
|
|
|
@ -80,12 +86,38 @@ func (ch *CaddyHugo) LTime() uint64 { |
|
|
|
return ch.ltime |
|
|
|
return ch.ltime |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) Build() error { |
|
|
|
|
|
|
|
err := ch.HugoSites.Build(hugolib.BuildCfg{ResetState: true}) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("error building hugo sites: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) Setup(c *caddy.Controller) error { |
|
|
|
func (ch *CaddyHugo) Setup(c *caddy.Controller) error { |
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
|
|
|
|
ch.docs = make(map[string]*docref) |
|
|
|
ch.docs = make(map[string]*docref) |
|
|
|
ch.Site = httpserver.GetConfig(c) |
|
|
|
ch.Site = httpserver.GetConfig(c) |
|
|
|
|
|
|
|
|
|
|
|
var err error |
|
|
|
ch.HugoCfg = &deps.DepsCfg{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ch.HugoCfg.Cfg, err = hugolib.LoadConfig(hugofs.Os, ch.Site.Root, "") |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("error loading hugo config: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ch.HugoCfg.Cfg.Set("workingdir", ch.Site.Root) |
|
|
|
|
|
|
|
ch.HugoSites, err = hugolib.NewHugoSites(*ch.HugoCfg) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("error intializing hugo: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = ch.Build() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("error building initial hugo: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ch.authorTmpl, err = template.New("").Parse(AuthorPage) |
|
|
|
ch.authorTmpl, err = template.New("").Parse(AuthorPage) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -222,7 +254,8 @@ func (ch CaddyHugo) Admin() httpserver.Handler { |
|
|
|
|
|
|
|
|
|
|
|
func (ch CaddyHugo) AuthorHome() httpserver.Handler { |
|
|
|
func (ch CaddyHugo) AuthorHome() httpserver.Handler { |
|
|
|
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { |
|
|
|
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { |
|
|
|
err := ch.authorTmpl.Execute(w, ch.TmplData(r, nil)) |
|
|
|
td := ch.TmplData(r, nil) |
|
|
|
|
|
|
|
err := ch.authorTmpl.Execute(w, td) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err) |
|
|
|
fmt.Println(err) |
|
|
|
return http.StatusInternalServerError, err |
|
|
|
return http.StatusInternalServerError, err |
|
|
@ -383,8 +416,11 @@ func (ch *CaddyHugo) Publish() error { |
|
|
|
cmd := exec.Command("hugo") |
|
|
|
cmd := exec.Command("hugo") |
|
|
|
cmd.Dir = ch.Site.Root |
|
|
|
cmd.Dir = ch.Site.Root |
|
|
|
_, err := cmd.CombinedOutput() |
|
|
|
_, err := cmd.CombinedOutput() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return err |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) { |
|
|
|
func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) { |
|
|
@ -444,7 +480,6 @@ func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int |
|
|
|
return http.StatusBadRequest, err |
|
|
|
return http.StatusBadRequest, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fmt.Println(message) |
|
|
|
|
|
|
|
ch.ObserveLTime(message.LTime) |
|
|
|
ch.ObserveLTime(message.LTime) |
|
|
|
timer.Reset(idlePingShort) |
|
|
|
timer.Reset(idlePingShort) |
|
|
|
|
|
|
|
|
|
|
@ -492,5 +527,9 @@ func (ch CaddyHugo) TmplData(r *http.Request, docref *docref) interface{} { |
|
|
|
if docref != nil { |
|
|
|
if docref != nil { |
|
|
|
doc = docref.doc |
|
|
|
doc = docref.doc |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ch.HugoSites != nil && ch.HugoSites.Fs != nil { |
|
|
|
|
|
|
|
ch.HugoSites.Fs.Destination = afero.NewMemMapFs() |
|
|
|
|
|
|
|
ch.Build() |
|
|
|
|
|
|
|
} |
|
|
|
return tmplData{ch.Site, r, ch, doc, docref} |
|
|
|
return tmplData{ch.Site, r, ch, doc, docref} |
|
|
|
} |
|
|
|
} |
|
|
|