forked from mkaz/lanyon
/
main.go
executable file
·439 lines (371 loc) · 10.7 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* _
* | | __ _ _ __ _ _ ___ _ __
* | |/ _` | '_ \| | | |/ _ \| '_ \
* | | (_| | | | | |_| | (_) | | | |
* |_|\__,_|_| |_|\__, |\___/|_| |_|
* |___/
*
* markdown web server
*
* Author : Marcus Kazmierczak
* Source : http://github.com/mkaz/lanyon
* License: MIT
*
*/
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"sort"
"strings"
"text/template"
"time"
"github.com/russross/blackfriday"
)
// globals
var config struct {
PortNum int
PublicDir string
TemplateDir string
RedirectDomain []string
}
var configFile string
var ServerVersion = "0.2.4"
var staticExpire = 30 // (days) default 1 month
var ts *template.Template
type PagesSlice []Page
func (p PagesSlice) Get(i int) Page { return p[i] }
func (p PagesSlice) Len() int { return len(p) }
func (p PagesSlice) Less(i, j int) bool { return p[i].Date.Unix() > p[j].Date.Unix() }
func (p PagesSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p PagesSlice) Sort() { sort.Sort(p) }
func (p PagesSlice) Limit(n int) PagesSlice { return p[0:n] }
// Page struct holds all data required for a web page
// Pages array is used for category pages with list of pages
type Page struct {
Title, Content, Category, Layout, Url, File string
Size int64
Date time.Time
Pages PagesSlice
Params map[string]string
}
func startup() {
loadConfig()
log.Printf("Lanyon listening on http://localhost:%d", config.PortNum)
// add trailing slashes to config directories
if !strings.HasSuffix(config.PublicDir, "/") {
config.PublicDir = config.PublicDir + "/"
}
// verify public directory exists
if _, err := os.Stat(config.PublicDir); err != nil {
log.Fatalln("Public directory does not exist")
}
// add trailing slashes to config directories
if !strings.HasSuffix(config.TemplateDir, "/") {
config.TemplateDir = config.TemplateDir + "/"
}
// verify template directory exists
if _, err := os.Stat(config.TemplateDir); err != nil {
log.Fatalln("Template directory does not exist")
}
if err := loadTemplates(); err != nil {
log.Fatalln("Error Parsing Templates: ", err.Error())
}
}
func loadTemplates() (err error) {
ts, err = template.ParseGlob(config.TemplateDir + "*.html")
return err
}
func main() {
flag.StringVar(&configFile, "config", "lanyon.json", "specify a config file")
flag.Parse()
startup()
http.HandleFunc("/", getRequest)
colonport := fmt.Sprintf(":%d", config.PortNum)
log.Fatal(http.ListenAndServe(colonport, nil))
}
// handler for all requests
func getRequest(w http.ResponseWriter, r *http.Request) {
// check domain redirect returns true on redirect
if domainRedirect(w, r) {
return
}
// add default headers
w.Header().Add("Server", ServerVersion)
w.Header().Add("Vary", "Accept-Encoding")
logRequest(r)
html := ""
fullpath := filepath.Clean(config.PublicDir + r.URL.Path)
ext := filepath.Ext(fullpath)
// check if file exists on filesystem
if fi, err := os.Stat(fullpath); err == nil {
if fi.IsDir() {
html, err = getDirectoryListing(fullpath)
if err != nil {
showFourOhFour(w, r)
return
}
} else {
setCacheExpirationDays(w, staticExpire)
http.ServeFile(w, r, fullpath)
return
}
} else { // file does not exist
// check if false extension and an actual
// file should be generated, or 404
switch ext {
case ".html":
html, err = getMarkdownFile(fullpath)
if err != nil {
showFourOhFour(w, r)
return
}
setCacheExpirationDays(w, 3)
case ".css":
html = getLessFile(fullpath)
w.Header().Set("Content-Type", "text/css")
setCacheExpirationDays(w, 3)
default:
showFourOhFour(w, r)
return
}
}
fmt.Fprint(w, html)
return
}
// directory listing checks for existing index file
// and if exists processes like any other markdown file
// otherwise gets directory listing of html and md files
// and creates a "category" page using the category.html
// template file with array of .Pages
func getDirectoryListing(dir string) (html string, err error) {
// check for index.md
indexfile := dir + "/index.md"
if _, err := os.Stat(indexfile); err == nil {
return getMarkdownFile(indexfile)
}
page := Page{}
page.Title = filepath.Base(dir)
page.Layout = "category"
page.Category = filepath.Base(dir)
var files []string
dirlist, _ := ioutil.ReadDir(dir)
for _, fi := range dirlist {
f := filepath.Join(dir, fi.Name())
ext := filepath.Ext(f)
if ext == ".html" || ext == ".md" || fi.Mode().IsDir() == true {
files = append(files, f)
}
}
// read markdown files to get title, date
for _, f := range files {
pg := readParseFile(f)
filename := strings.Replace(f, ".md", ".html", 1)
pg.Url = "/" + strings.Replace(filename, config.PublicDir, "", 1)
fi, _ := os.Stat(f)
if fi.Mode().IsDir() {
pg.Size = -1
} else {
pg.Size = fi.Size()
}
pg.File = path.Base(pg.Url)
page.Pages = append(page.Pages, pg)
}
page.Pages.Sort()
html = applyTemplates(page)
return html, err
}
// reads markdown file, parse front matter and render content
// using template file defined by layout: param or by default
// uses post.html template
func getMarkdownFile(fullpath string) (html string, err error) {
mdfile := strings.Replace(fullpath, ".html", ".md", 1)
page := Page{}
if _, err := os.Stat(mdfile); err == nil {
page = readParseFile(mdfile)
} else {
return "", fmt.Errorf("Error reading file %s ", mdfile)
}
html = applyTemplates(page)
return html, err
}
func getLessFile(fullpath string) string {
lessfile := strings.Replace(fullpath, ".css", ".less", 1)
path, err := exec.LookPath("lessc")
if err != nil {
return "/* Less is not installed */"
}
// requires lessc binary
cmd := exec.Command(path, "--no-color", "--no-ie-compat", "--silent", "-su=off", "-sm=on", "--clean-css", lessfile)
output, err := cmd.Output()
if err != nil {
return ""
}
return string(output)
}
func showFourOhFour(w http.ResponseWriter, r *http.Request) {
md404 := config.PublicDir + "404.md"
html, err := getMarkdownFile(md404)
if err != nil {
http.NotFound(w, r)
return
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, html)
}
// checks config.RedirectDomain for ["domain1", "domain2"]
// If config is set, checks if request domain matches domain1
// if request does not match, issues 301 redirect to "domain2"
// Used to handle non "www.mkaz.com" requests to redirect
// to www.mkaz.com
// @return bool - true if redirected, false otherwise
//
func domainRedirect(w http.ResponseWriter, r *http.Request) bool {
if len(config.RedirectDomain) != 2 {
return false
}
if r.Host == config.RedirectDomain[0] {
return false
}
redirect_url := fmt.Sprintf("http://%s/%s", config.RedirectDomain[1], r.RequestURI)
http.Redirect(w, r, redirect_url, 301)
return true
}
// read and parse markdown filename
func readParseFile(filename string) (page Page) {
// setup default page struct
page = Page{
Title: "",
Content: "",
Category: getDirName(filename),
Layout: "post",
Date: time.Now(),
Params: make(map[string]string),
}
var data, err = ioutil.ReadFile(filename)
if err != nil {
return
}
// parse front matter from --- to ---
var lines = strings.Split(string(data), "\n")
var found = 0
for i, line := range lines {
line = strings.TrimSpace(line)
if found == 1 {
// parse line for param
colonIndex := strings.Index(line, ":")
if colonIndex > 0 {
key := strings.TrimSpace(line[:colonIndex])
value := strings.TrimSpace(line[colonIndex+1:])
value = strings.Trim(value, "\"") //remove quotes
switch key {
case "title":
page.Title = value
case "layout":
page.Layout = value
case "date":
page.Date, _ = time.Parse("2006-01-02", value)
default:
page.Params[key] = value
}
}
} else if found >= 2 {
// params over
lines = lines[i:]
break
}
if line == "---" {
found += 1
}
}
// convert markdown content
content := strings.Join(lines, "\n")
output := markdownRender([]byte(content))
page.Content = string(output)
return page
}
func applyTemplates(page Page) string {
buffer := new(bytes.Buffer)
templateFile := ""
if page.Layout == "" {
templateFile = "post.html"
} else {
templateFile = page.Layout + ".html"
}
ts.ExecuteTemplate(buffer, templateFile, page)
return buffer.String()
}
// configure markdown render options
// See blackfriday markdown source for details
func markdownRender(content []byte) []byte {
htmlFlags := 0
//htmlFlags |= blackfriday.HTML_SKIP_SCRIPT
htmlFlags |= blackfriday.HTML_USE_XHTML
htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
renderer := blackfriday.HtmlRenderer(htmlFlags, "", "")
extensions := 0
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
extensions |= blackfriday.EXTENSION_TABLES
extensions |= blackfriday.EXTENSION_FENCED_CODE
extensions |= blackfriday.EXTENSION_AUTOLINK
extensions |= blackfriday.EXTENSION_STRIKETHROUGH
extensions |= blackfriday.EXTENSION_SPACE_HEADERS
return blackfriday.Markdown(content, renderer, extensions)
}
func loadConfig() {
// checks if specified file exists, either flag passed in
// or default lanyon.json in current directory
if _, err := os.Stat(configFile); os.IsNotExist(err) {
// config file not found
// lets check one more spot in /etc/
configFile = "/etc/lanyon.json"
if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Fatalln("Config file not found, /etc/lanyon.json or specify with --config=FILENAME")
}
}
file, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatalln("Error reading config file:", err)
}
if err := json.Unmarshal(file, &config); err != nil {
log.Fatalln("Error parsing config lanyon.json: ", err)
}
}
// returns directory name from filepath
func getDirName(fullpath string) (dir string) {
dir = filepath.Dir(fullpath)
dir = filepath.Base(dir)
if strings.Trim(dir, "/") == strings.Trim(config.PublicDir, "/") {
dir = ""
}
return
}
func setCacheExpirationDays(w http.ResponseWriter, days int) {
d := time.Duration(int64(time.Hour) * 24 * int64(days))
expireDate := time.Now().Add(d)
expireSecs := days * 24 * 60 * 60
w.Header().Add("Cache-Control", fmt.Sprintf("max-age=%d, public", expireSecs))
w.Header().Add("Expires", expireDate.Format(time.RFC1123))
}
func logRequest(req *http.Request) {
now := time.Now()
log.Printf("%s - %s [%s] \"%s %s %s\" ",
req.RemoteAddr,
"",
now.Format("02/Jan/2006:15:04:05 -0700"),
req.Method,
req.URL.RequestURI(),
req.Proto)
}