-
Notifications
You must be signed in to change notification settings - Fork 1
/
calvados.go
495 lines (439 loc) · 13.9 KB
/
calvados.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package calvados
import (
"errors"
"html/template"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/Sirupsen/logrus"
"github.com/blevesearch/bleve"
"github.com/gdevillele/frontparser"
"github.com/gin-gonic/contrib/gzip"
"github.com/gin-gonic/contrib/static"
"github.com/gin-gonic/gin"
"github.com/shurcooL/github_flavored_markdown"
)
// TODO: gdevillele:
//
// - language should be detected by a middleware and stored in the *gin.Context
//
type PreprocFunc func(c *Calvados) error
type CustomRouteFunc func(*gin.Context, *Calvados)
type CustomRoute struct {
method string
path string
function CustomRouteFunc
}
func NewCustomRoute(method, path string, function CustomRouteFunc) CustomRoute {
return CustomRoute{method, path, function}
}
type Config struct {
DefaultLanguage string
DefaultPageTitle string
SearchBar bool
}
// use this function to create a Config object please
func NewConfig(defaultLanguage string, defaultPageTitle string, searchBar bool) Config {
return Config{
DefaultLanguage: defaultLanguage,
DefaultPageTitle: defaultPageTitle,
SearchBar: searchBar,
}
}
type Calvados struct {
redirections map[string]string
templateDirs []string
preprocFuncs []PreprocFunc
searchIndex bleve.Index
config Config
customRoutes []CustomRoute
}
// Default returns a Calvados server with default configuration
func Default() *Calvados {
mapping := bleve.NewIndexMapping()
index, _ := bleve.NewMemOnly(mapping)
// TODO: handle error
return &Calvados{
redirections: make(map[string]string),
templateDirs: make([]string, 0),
preprocFuncs: make([]PreprocFunc, 0),
config: NewConfig("en", "Default Title", false),
searchIndex: index,
customRoutes: []CustomRoute{},
}
}
// WithConfig returns a Calvados server with given Config
func WithConfig(config Config) *Calvados {
mapping := bleve.NewIndexMapping()
index, _ := bleve.NewMemOnly(mapping)
// TODO: handle error
return &Calvados{
redirections: make(map[string]string),
templateDirs: make([]string, 0),
preprocFuncs: make([]PreprocFunc, 0),
config: config,
searchIndex: index,
customRoutes: []CustomRoute{},
}
}
func (c *Calvados) AddCustomRoute(customRoute CustomRoute) {
c.customRoutes = append(c.customRoutes, customRoute)
}
func (c *Calvados) AddPreprocessorFunc(f PreprocFunc) {
c.preprocFuncs = append(c.preprocFuncs, f)
}
func (c *Calvados) AddTemplateDir(path string) {
c.templateDirs = append(c.templateDirs, path)
}
// serves static and markdown content in /www
// serves static content in /style
func (calva *Calvados) Run(hostAndPort string) error {
// Execute preprocessor functions
err := calva.executePreprocessorFunctions()
if err != nil {
logrus.Fatalln("[calvados] [preprocessor]", err.Error())
}
// Create gin router
router := gin.Default()
// Load templates
err = calva.loadTemplates(router)
if err != nil {
logrus.Fatalln("[calvados] [loadTemplates]", err.Error())
}
router.Use(gzip.Gzip(gzip.DefaultCompression))
router.Use(calva.MWAddSecurityHTTPHeaders)
router.Use(calva.MWCheckForUnexposedPath)
router.Use(static.ServeRoot("/style", "/style"))
for _, customRoute := range calva.customRoutes {
switch method := customRoute.method; method {
case "GET":
router.GET(customRoute.path, func(c *gin.Context) {
customRoute.function(c, calva)
})
case "POST":
router.POST(customRoute.path, func(c *gin.Context) {
customRoute.function(c, calva)
})
default:
logrus.Errorln("custom route method not supported:", method)
}
}
router.GET("/*path", calva.replyForPath)
router.NoRoute(calva.replyNotFound)
return router.Run(hostAndPort)
}
//
func (c *Calvados) SetRedirection(alias, canonical string) {
c.redirections[alias] = canonical
}
////////////////////////////////////////////////////////////
///
/// Unexposed functions
///
////////////////////////////////////////////////////////////
//
func (c *Calvados) executePreprocessorFunctions() error {
for _, f := range c.preprocFuncs {
err := f(c)
if err != nil {
return err
}
}
return nil
}
// loadTemplates loads as templates all the files (direct children only)
// present in the directories listed in c.templateDirs.
func (c *Calvados) loadTemplates(r *gin.Engine) error {
tmplFiles := make([]string, 0)
for _, templateDir := range c.templateDirs {
files, err := ioutil.ReadDir(templateDir)
if err != nil {
return err
}
for _, file := range files {
tmplFiles = append(tmplFiles, filepath.Join(templateDir, file.Name()))
}
}
r.LoadHTMLFiles(tmplFiles...)
return nil
}
////////////////////////////////////////////////////////////
///
/// requests handling (generating HTTP responses)
///
////////////////////////////////////////////////////////////
//
func (calva *Calvados) replyForPath(c *gin.Context) {
requestPath := c.Param("path")
logrus.Println("request path:", requestPath)
requestPathEndsWithSlash := strings.HasSuffix(requestPath, "/")
// WARNING: this call, removes the trailing '/' if there is any
resourcePath := filepath.Join("/www", requestPath)
logrus.Println("resource path:", resourcePath)
// redirect user if request path can be cleaned
cleanedPath := cleanPath(requestPath)
if cleanedPath != requestPath {
c.Redirect(http.StatusSeeOther, cleanedPath)
return
}
dirExistsAtResourcePath := directoryExists(resourcePath)
// here, path has been cleaned and trailing '/' removed if directory not found
if requestPathEndsWithSlash {
// - request path ends with a '/'
if dirExistsAtResourcePath {
// - request path ends with a '/'
// - a directory exists at that path
if mdIndexPath := filepath.Join(resourcePath, "index.md"); regularFileExists(mdIndexPath) {
// - directory exists
// - child index.md file found
// > render that file
resourcePath = mdIndexPath
} else if htmlIndexPath := filepath.Join(resourcePath, "index.html"); regularFileExists(htmlIndexPath) {
// - directory exists
// - child index.md file not found
// - child index.html file found
// > serve that file directly
replyFile(c, resourcePath)
return
} else {
// - directory exists
// - child index.md file not found
// - child index.html file not found
// > we reply 404 not found
calva.replyNotFound(c)
return
}
} else if canonicalPath, ok := calva.redirections[requestPath]; ok {
// - request path ends with a '/'
// - no directory exists at that path
// - request path is actually an alias
// > we redirect the user to the alias' canonical path
c.Redirect(http.StatusMovedPermanently, canonicalPath) // HTTP 301
return
} else {
// - request path ends with a '/'
// - no directory exists at that path
// - request path is not an alias
// > we redirect to the same path, minus the trailing '/'
// [[ TEMPORARY HACK because of mistakes in the content ]]
c.Redirect(http.StatusSeeOther, strings.TrimSuffix(requestPath, "/"))
return
}
} else {
// request path doesn't end with a '/'
if regularFileExists(resourcePath) {
// request path points to an existing file
replyFile(c, resourcePath)
return
} else {
// request path doesn't point to a regular file
if mdResource := resourcePath + ".md"; regularFileExists(mdResource) {
// try adding ".md" to the resource path
resourcePath = mdResource
} else if dirExistsAtResourcePath {
// Request path doesn't end with '/' but it points to a directory.
// We redirect to the same path but adding a trailing '/'
c.Redirect(http.StatusSeeOther, requestPath+"/")
return
} else if canonicalPath, ok := calva.redirections[requestPath]; ok {
// request path doesn't end with '/'
// request path + ".md" doesn't point to an existing file
// request path doesn't point to an existing directory
// request path is an alias
c.Redirect(http.StatusSeeOther, canonicalPath)
return
} else {
// resource path doesn't point to an existing file
// even with the ".md" suffix
calva.replyNotFound(c)
return
}
}
}
// if we reach this point the file at <resourcePath> should be a markdown file
calva.ReplyMardown(c, http.StatusOK, resourcePath, nil)
}
// 404 Not Found
func (calva *Calvados) replyNotFound(c *gin.Context) {
// TODO: gdevillele: maybe use a generic 404 with a "back to homepage" button
// (also, maybe a 404 that is not a .md file but a regular html template .tmpl)
calva.ReplyMardown(c, http.StatusNotFound, "/www/_404.md", nil)
}
// 500 Internal Server Error
func (calva *Calvados) replyInternalServerError(c *gin.Context, errorMessage string) {
c.HTML(http.StatusInternalServerError, "500.tmpl", gin.H{
"language": calva.config.DefaultLanguage, // TODO: gdevillele: make this dynamic (multilang support)
"content": errorMessage,
})
}
//
func replyFile(c *gin.Context, path string) {
c.File(path)
}
//
func (calva *Calvados) ReplyMardown(c *gin.Context, httpStatus int, resourcePath string, params map[string]interface{}) {
// read markdown file
mdFileBytes, err := ioutil.ReadFile(resourcePath)
if err != nil {
calva.replyInternalServerError(c, err.Error())
return
}
// page's default info
pageLanguage := calva.config.DefaultLanguage // TODO: gdevillele: make this dynamic (multilang support)
pageTitle := calva.config.DefaultPageTitle
pageTemplate := "default.tmpl" // TODO: gdevillele: make this customizable
pageKeywords := ""
pageDescription := ""
pageMdContent := mdFileBytes
pageFrontmatter := make(map[string]interface{})
// frontmatter parsing
// check if file has a frontmatter header
if frontparser.HasFrontmatterHeader(mdFileBytes) {
fm, md, err := frontparser.ParseFrontmatterAndContent(mdFileBytes)
if err != nil {
calva.replyInternalServerError(c, err.Error())
return
}
pageFrontmatter = fm
pageMdContent = md
// find title in frontmatter
if titleIface, ok := pageFrontmatter["title"]; ok {
titleStr, err := toString(titleIface)
if err != nil {
logrus.Println("ERROR: frontmatter title value is not a string.")
} else {
pageTitle = titleStr
}
}
// find keywords in frontmatter
if keywordsIface, ok := pageFrontmatter["keywords"]; ok {
keywordsStr, err := toString(keywordsIface)
if err != nil {
logrus.Println("ERROR: frontmatter keywords value is not a string")
} else {
pageKeywords = keywordsStr
}
}
// find description in frontmatter
if descriptionIface, ok := pageFrontmatter["description"]; ok {
descriptionStr, err := toString(descriptionIface)
if err != nil {
logrus.Println("ERROR: frontmatter description value is not a string")
} else {
pageDescription = descriptionStr
}
}
// find template in frontmatter
if templateIface, ok := pageFrontmatter["template"]; ok {
templateStr, err := toString(templateIface)
if err != nil {
logrus.Println("ERROR: frontmatter template value is not a string")
} else {
pageTemplate = templateStr
}
}
}
contentHtmlBytes := github_flavored_markdown.Markdown(pageMdContent)
htmlParams := gin.H{
"language": pageLanguage, // string (must not be empty)
"title": pageTitle,
"content": template.HTML(contentHtmlBytes),
"metaKeywords": pageKeywords, // string (can be empty)
"metaDescription": pageDescription, // string (can be empty)
"config": calva.config,
"resourcePath": strings.TrimPrefix(resourcePath, "/www"),
}
if params != nil {
for k, v := range params {
htmlParams[k] = v
}
}
c.HTML(httpStatus, pageTemplate, htmlParams)
}
////////////////////////////////////////////////////////////
///
/// Middlewares
///
////////////////////////////////////////////////////////////
// Adds the security HTTP headers to all HTTP responses
func (calva *Calvados) MWAddSecurityHTTPHeaders(c *gin.Context) {
// add CSP header to HTTP responses
c.Header("Content-Security-Policy", "script-src 'self' 'unsafe-inline'")
// add X-Frame-Options header to HTTP responses
c.Header("X-Frame-Options", "DENY")
// add X-XSS-Protection header to HTTP responses
c.Header("X-XSS-Protection", "1; mode=block")
// add X-Content-Type-Options header to HTTP responses
c.Header("X-Content-Type-Options", "nosniff")
}
// MWCheckForUnexposedPath returns a middleware that sends a 404 response if the request concerns a
// resource that is not exposed. (if one of the path components starts with '_')
func (calva *Calvados) MWCheckForUnexposedPath(c *gin.Context) {
path := c.Request.URL.String() // "/style/main.css"
pathComponents := splitPath(path) // ["style", "main.css"]
for _, pathComponent := range pathComponents {
if strings.HasPrefix(pathComponent, "_") {
calva.replyNotFound(c)
c.Abort()
}
}
}
////////////////////////////////////////////////////////////
///
/// Utility functions
///
////////////////////////////////////////////////////////////
func toString(i interface{}) (string, error) {
if str, ok := i.(string); ok {
return str, nil
}
return "", errors.New("interface is not a string")
}
// splitPath takes a '/' separated path and
// returns a slice containing the path elements
// TODO: gdevillele: mabye use filepath.Split
func splitPath(path string) []string {
result := make([]string, 0)
for {
if len(path) == 0 {
break
} else if path[len(path)-1:] == "/" {
path = path[:len(path)-1]
}
dir, file := filepath.Split(path)
if len(file) > 0 {
result = append([]string{file}, result...)
}
path = dir
}
return result
}
// removes trailing ".md" or trailing "index.md"
func cleanPath(path string) string {
// check for trailing ".md"
path = strings.TrimSuffix(path, ".md")
if strings.HasSuffix(path, "/index") {
path = strings.TrimSuffix(path, "index")
}
return path
}
func regularFileExists(absPath string) bool {
fd, err := os.Open(absPath)
if err != nil {
return false
}
fi, err := fd.Stat()
if err != nil {
return false
}
return fi.IsDir() == false
}
func directoryExists(absPath string) bool {
s, err := os.Stat(absPath)
if err != nil {
return false
}
return s.IsDir()
}