my CMS/Blog engine
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.

main.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package main
  2. /*
  3. * This is freesofware under 2-clause BSD license, See LICENSE file
  4. * Copyright (c) 2013, Martin Angers
  5. * (C)opyright 2018,2019 juju
  6. */
  7. import (
  8. "net/url"
  9. "os"
  10. "path/filepath"
  11. "github.com/jessevdk/go-flags"
  12. )
  13. // This structure holds the command-line options.
  14. type options struct {
  15. Port int `short:"p" long:"port" description:"the port to use for the web server" default:"9000"`
  16. GenOnly bool `short:"g" long:"generate-only" description:"generate the static site and exit"`
  17. NoGen bool `short:"G" long:"no-generation" description:"when set, the site is not automatically generated"`
  18. SiteName string `short:"n" long:"site-name" description:"the name of the site" default:"Site Name"`
  19. TagLine string `short:"t" long:"tag-line" description:"the site's tag line"`
  20. RecentPostsCount int `short:"r" long:"recent-posts" description:"the number of recent posts to send to the templates" default:"5"`
  21. BaseURL string `short:"b" long:"base-url" description:"the base URL of the web site" default:"http://localhost"`
  22. Debug bool `short:"d" long:"debug" description:"Enable debug output"`
  23. Src string `short:"s" long:"src" description:"the source sub-dir name" default:"src"`
  24. Out string `short:"o" long:"out" description:"the output sub-dir name" default:"out"`
  25. Template string `short:"a" long:"template" description:"the template sub-dir name" default:"templates"`
  26. Static string `short:"i" long:"static" description:"static content to be copied to Out/" default:"static"`
  27. }
  28. type siteMeta struct {
  29. meta TemplateData
  30. recentPosts int
  31. }
  32. var (
  33. // The one and only Options parsed from the command-line
  34. Options options
  35. RootDir string // Start root directory
  36. PublicDir string // Public directory path
  37. PostsDir string // Posts directory path
  38. TemplatesDir string // Templates directory path
  39. StaticDirs string // Static contents path
  40. RssURL string // The RSS feed URL, parsed only once and stored for convenience
  41. SiteMeta siteMeta // The site meta data can be used by posts
  42. Debug bool // Enable debug output
  43. )
  44. func init() {
  45. // Parse arguments
  46. _, err := flags.Parse(&Options)
  47. if err != nil {
  48. FATAL("A:%v", err.Error())
  49. }
  50. // RootDir is where arg[0] is launched
  51. RootDir, err := os.Getwd()
  52. if err != nil {
  53. FATAL(err.Error())
  54. }
  55. // Init directories with absolut path or relatives to RootDir
  56. // PublicDir is where the web pages are stored
  57. if Options.Out[0] == '/' {
  58. PublicDir = Options.Out
  59. } else {
  60. PublicDir = filepath.Join(RootDir, Options.Out)
  61. }
  62. // PostsDir is where the author's *.md are stored
  63. if Options.Src[0] == '/' {
  64. PostsDir = Options.Src
  65. } else {
  66. PostsDir = filepath.Join(RootDir, Options.Src)
  67. }
  68. // TemplatesDir is where templates stays
  69. if Options.Template[0] == '/' {
  70. TemplatesDir = Options.Template
  71. } else {
  72. TemplatesDir = filepath.Join(RootDir, Options.Template)
  73. }
  74. // TemplatesDir is where templates stays
  75. if Options.Static[0] == '/' {
  76. StaticDirs = Options.Static
  77. } else {
  78. StaticDirs = filepath.Join(RootDir, Options.Static)
  79. }
  80. initBF()
  81. }
  82. func storeRssURL() {
  83. b, err := url.Parse(Options.BaseURL)
  84. if err != nil {
  85. FATAL(err.Error())
  86. }
  87. r, err := b.Parse("/rss")
  88. if err != nil {
  89. FATAL(err.Error())
  90. }
  91. RssURL = r.String()
  92. }
  93. func copyMeta() {
  94. SiteMeta.recentPosts = Options.RecentPostsCount
  95. SiteMeta.meta = make(TemplateData)
  96. SiteMeta.meta["BaseURL"] = Options.BaseURL
  97. SiteMeta.meta["SiteName"] = Options.SiteName
  98. SiteMeta.meta["TagLine"] = Options.TagLine
  99. SiteMeta.meta["RssURL"] = RssURL
  100. }
  101. func main() {
  102. INFO("Start program......")
  103. storeRssURL()
  104. copyMeta()
  105. if !Options.NoGen {
  106. // Generate the site
  107. if err := generateSite(); err != nil {
  108. INFO("generateSite failed: %v", err)
  109. }
  110. // Terminate if set to generate only
  111. if Options.GenOnly {
  112. return
  113. }
  114. // Start the watcher
  115. go beginWatch(TemplatesDir, PostsDir, StaticDirs)
  116. // Start the web server
  117. run()
  118. }
  119. }