forked from mna/trofaf
/
main.go
71 lines (63 loc) · 1.94 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"log"
"net/url"
"os"
"path/filepath"
"github.com/jessevdk/go-flags"
)
// This structure holds the command-line options.
type options struct {
Port int `short:"p" long:"port" description:"the port to use for the web server" default:"9000"`
NoGen bool `short:"G" long:"no-generation" description:"when set, the site is not automatically generated"`
SiteName string `short:"n" long:"site-name" description:"the name of the site" default:"Site Name"`
TagLine string `short:"t" long:"tag-line" description:"the site's tag line"`
RecentPostsCount int `short:"r" long:"recent-posts" description:"the number of recent posts to send to the templates" default:"5"`
BaseURL string `short:"b" long:"base-url" description:"the base URL of the web site" default:"http://localhost"`
}
var (
// The one and only Options parsed from the command-line
Options options
PublicDir string // Public directory path
PostsDir string // Posts directory path
TemplatesDir string // Templates directory path
RssURL string // The RSS feed URL, parsed only once and stored for convenience
)
func init() {
// Initialize directories
pwd, err := os.Getwd()
if err != nil {
log.Fatal("FATAL ", err)
}
PublicDir = filepath.Join(pwd, "public")
PostsDir = filepath.Join(pwd, "posts")
TemplatesDir = filepath.Join(pwd, "templates")
}
func storeRssURL() {
b, err := url.Parse(Options.BaseURL)
if err != nil {
log.Fatal("FATAL ", err)
}
r, err := b.Parse("/rss")
if err != nil {
log.Fatal("FATAL ", err)
}
RssURL = r.String()
}
func main() {
// Parse the flags
_, err := flags.Parse(&Options)
if err == nil { // err != nil prints the usage automatically
storeRssURL()
if !Options.NoGen {
// Generate the site
if err := generateSite(); err != nil {
log.Fatal("FATAL ", err)
}
// Start the watcher
defer startWatcher().Close()
}
// Start the web server
run()
}
}