No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package caddyhugo
  2. import (
  3. "fmt"
  4. "html/template"
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "git.stephensearles.com/stephen/caddy-hugo2/comments"
  9. "git.stephensearles.com/stephen/caddy-hugo2/media"
  10. "github.com/gohugoio/hugo/hugofs"
  11. "github.com/mholt/caddy"
  12. "github.com/mholt/caddy/caddyhttp/httpserver"
  13. )
  14. var eventHookCounter uint64
  15. func SetupCaddy(c *caddy.Controller) error {
  16. ch := &CaddyHugo{}
  17. ch.Site = httpserver.GetConfig(c)
  18. root, err := filepath.Abs(ch.Site.Root)
  19. if err != nil {
  20. return err
  21. }
  22. err = ch.Setup(root)
  23. c.OnShutdown(func() error {
  24. return ch.persistAllEdits()
  25. })
  26. ch.Site.AddMiddleware(ch.Middleware(c))
  27. ch.commentsSetting(c)
  28. return err
  29. }
  30. func (ch *CaddyHugo) commentsSetting(c *caddy.Controller) {
  31. for c.NextLine() {
  32. if c.Val() == "hugo" {
  33. for c.NextBlock() {
  34. if c.Val() == "comments" {
  35. ch.Comments = comments.WithStorage(comments.NewDiskv(path.Join(ch.Site.Root, "comments")))
  36. if c.NextArg() {
  37. ch.Comments.Password = c.Val()
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. func (ch *CaddyHugo) Setup(dir string) error {
  45. var err error
  46. ch.Dir = dir
  47. ch.docs = make(map[string]*editSession)
  48. ch.confirmingToClient = make(map[uint64]struct{})
  49. ch.HugoSites, ch.HugoCfg, err = ch.configWithFs(hugofs.Os)
  50. if err != nil {
  51. return fmt.Errorf("error setting up hugo : %v", err)
  52. }
  53. err = ch.Build()
  54. if err != nil {
  55. return fmt.Errorf("error building initial hugo: %v", err)
  56. }
  57. ch.authorTmpl, err = template.New("").Parse(AuthorPage)
  58. if err != nil {
  59. return fmt.Errorf("author template invalid: %v", err)
  60. }
  61. ch.adminTmpl, err = template.New("").Parse(AdminPage)
  62. if err != nil {
  63. return fmt.Errorf("admin template invalid: %v", err)
  64. }
  65. ch.editTmpl, err = template.New("").Parse(EditPage)
  66. if err != nil {
  67. return fmt.Errorf("edit template invalid: %v", err)
  68. }
  69. ch.Media = &media.MediaSource{
  70. StorageDir: path.Join(dir, "media"),
  71. ThumbDir: path.Join(dir, "thumbs"),
  72. }
  73. err = os.MkdirAll(ch.Media.StorageDir, 0755)
  74. if err != nil {
  75. return fmt.Errorf("couldn't initialize media: %v", err)
  76. }
  77. err = writeThemeFiles(ch.Dir)
  78. if err != nil {
  79. fmt.Println("error installing theme files:", err)
  80. }
  81. err = ch.Publish()
  82. if err != nil {
  83. fmt.Println("error with initial publish of hugo site:", err)
  84. }
  85. return nil
  86. }