2018-02-12 00:24:43 +00:00
|
|
|
package blog
|
2017-10-08 04:48:58 +00:00
|
|
|
|
|
|
|
import (
|
2017-12-02 18:47:21 +00:00
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
2017-10-08 04:48:58 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2018-02-10 03:01:56 +00:00
|
|
|
|
2018-02-12 00:24:43 +00:00
|
|
|
"github.com/kirsle/blog/internal/controllers/posts"
|
|
|
|
"github.com/kirsle/blog/internal/log"
|
|
|
|
"github.com/kirsle/blog/internal/markdown"
|
|
|
|
"github.com/kirsle/blog/internal/render"
|
|
|
|
"github.com/kirsle/blog/internal/responses"
|
2017-10-08 04:48:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PageHandler is the catch-all route handler, for serving static web pages.
|
|
|
|
func (b *Blog) PageHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
path := r.URL.Path
|
2017-12-23 02:34:58 +00:00
|
|
|
// log.Debug("Catch-all page handler invoked for request URI: %s", path)
|
2017-10-08 04:48:58 +00:00
|
|
|
|
|
|
|
// Remove trailing slashes by redirecting them away.
|
|
|
|
if len(path) > 1 && path[len(path)-1] == '/' {
|
2018-02-10 19:20:27 +00:00
|
|
|
responses.Redirect(w, strings.TrimRight(path, "/"))
|
2017-10-31 16:42:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restrict special paths.
|
|
|
|
if strings.HasPrefix(strings.ToLower(path), "/.") {
|
2018-02-10 23:07:10 +00:00
|
|
|
responses.Forbidden(w, r, "Forbidden")
|
2017-10-08 04:48:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for a file that matches their URL.
|
2018-02-10 18:08:45 +00:00
|
|
|
filepath, err := render.ResolvePath(path)
|
2017-10-31 16:42:15 +00:00
|
|
|
if err != nil {
|
2017-11-24 19:56:32 +00:00
|
|
|
// See if it resolves as a blog entry.
|
2018-02-10 23:07:10 +00:00
|
|
|
err = postctl.ViewPost(w, r, strings.TrimLeft(path, "/"))
|
2017-11-24 19:56:32 +00:00
|
|
|
if err != nil {
|
2018-02-10 23:16:30 +00:00
|
|
|
log.Error("Post by fragment %s not found: %s", path, err)
|
2018-02-10 23:07:10 +00:00
|
|
|
responses.NotFound(w, r, "The page you were looking for was not found.")
|
2017-11-24 19:56:32 +00:00
|
|
|
}
|
2017-10-31 16:42:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is it a template file?
|
2017-12-23 02:34:58 +00:00
|
|
|
if strings.HasSuffix(filepath.URI, ".gohtml") {
|
2018-02-10 22:05:41 +00:00
|
|
|
render.Template(w, r, filepath.URI, nil)
|
2017-10-31 16:42:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:47:21 +00:00
|
|
|
// Is it a Markdown file?
|
|
|
|
if strings.HasSuffix(filepath.URI, ".md") || strings.HasSuffix(filepath.URI, ".markdown") {
|
|
|
|
source, err := ioutil.ReadFile(filepath.Absolute)
|
|
|
|
if err != nil {
|
2018-02-10 23:07:10 +00:00
|
|
|
responses.Error(w, r, "Couldn't read Markdown source!")
|
2017-12-02 18:47:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render it to HTML and find out its title.
|
|
|
|
body := string(source)
|
2018-02-10 03:01:56 +00:00
|
|
|
html := markdown.RenderTrustedMarkdown(body)
|
|
|
|
title, _ := markdown.TitleFromMarkdown(body)
|
2017-12-02 18:47:21 +00:00
|
|
|
|
2018-02-10 22:05:41 +00:00
|
|
|
render.Template(w, r, ".markdown", map[string]interface{}{
|
2017-12-23 23:29:38 +00:00
|
|
|
"Title": title,
|
|
|
|
"HTML": template.HTML(html),
|
2018-02-10 22:05:41 +00:00
|
|
|
"MarkdownPath": filepath.URI,
|
|
|
|
})
|
2017-12-02 18:47:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-31 16:42:15 +00:00
|
|
|
http.ServeFile(w, r, filepath.Absolute)
|
|
|
|
}
|