forked from piranha/gostatic
/
gostatic.go
111 lines (90 loc) · 2.28 KB
/
gostatic.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// (c) 2012 Alexander Solovyov
// under terms of ISC license
package main
import (
"encoding/json"
"fmt"
goopt "github.com/droundy/goopt"
"net/http"
"path/filepath"
"strings"
)
var Version = "0.1"
var Summary = `gostatic path/to/config
Build a site.
`
var showVersion = goopt.Flag([]string{"-V", "--version"}, []string{},
"show version and exit", "")
var showProcessors = goopt.Flag([]string{"--processors"}, []string{},
"show internal processors", "")
var showSummary = goopt.Flag([]string{"--summary"}, []string{},
"print everything on stdout", "")
var showConfig = goopt.Flag([]string{"--show-config"}, []string{},
"dump config as JSON on stdout", "")
var doWatch = goopt.Flag([]string{"-w", "--watch"}, []string{},
"watch for changes and serve them as http", "")
var port = goopt.String([]string{"-p", "--port"}, "8000",
"port to serve on")
var verbose = goopt.Flag([]string{"-v", "--verbose"}, []string{},
"enable verbose output", "")
// used in Page.Changed()
var force = goopt.Flag([]string{"-f", "--force"}, []string{},
"force building all pages", "")
func main() {
goopt.Version = Version
goopt.Summary = Summary
goopt.Parse(nil)
if *showSummary && *doWatch {
errhandle(fmt.Errorf("--summary and --watch do not mix together well"))
}
if *showVersion {
out("gostatic %s\n", goopt.Version)
return
}
if *showProcessors {
ProcessorSummary()
return
}
if len(goopt.Args) == 0 {
println(goopt.Usage())
return
}
config, err := NewSiteConfig(goopt.Args[0])
errhandle(err)
if *showConfig {
x, err := json.MarshalIndent(config, "", " ")
errhandle(err)
println(string(x))
return
}
site := NewSite(config)
if *showSummary {
site.Summary()
} else {
site.Render()
}
if *doWatch {
StartWatcher(config)
out("Starting server at *:%s...\n", *port)
fs := http.FileServer(http.Dir(config.Output))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
fs.ServeHTTP(w, r)
})
err := http.ListenAndServe(":"+*port, nil)
errhandle(err)
}
}
func StartWatcher(config *SiteConfig) {
filemod, err := DirWatcher(config.Source)
errhandle(err)
go func() {
for {
fn := <-filemod
if !strings.HasPrefix(filepath.Base(fn), ".") {
site := NewSite(config)
site.Render()
}
}
}()
}