mirror of https://github.com/jlelse/GoBlog
Custom pages
This commit is contained in:
parent
2e358e2de8
commit
6624ac9ecd
|
@ -50,6 +50,7 @@ type configBlog struct {
|
||||||
Photos *photos `mapstructure:"photos"`
|
Photos *photos `mapstructure:"photos"`
|
||||||
ActivityStreams *activityStreams `mapstructure:"activitystreams"`
|
ActivityStreams *activityStreams `mapstructure:"activitystreams"`
|
||||||
DefaultSection string `mapstructure:"defaultsection"`
|
DefaultSection string `mapstructure:"defaultsection"`
|
||||||
|
CustomPages []*customPage `mapstructure:"custompages"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type section struct {
|
type section struct {
|
||||||
|
@ -88,6 +89,13 @@ type activityStreams struct {
|
||||||
ImagesParameter string `mapstructure:"imagesParameter"`
|
ImagesParameter string `mapstructure:"imagesParameter"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type customPage struct {
|
||||||
|
Path string `mapstructure:"path"`
|
||||||
|
Template string `mapstructure:"template"`
|
||||||
|
Cache bool `mapstructure:"cache"`
|
||||||
|
Data *interface{} `mapstructure:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
type configUser struct {
|
type configUser struct {
|
||||||
Nick string `mapstructure:"nick"`
|
Nick string `mapstructure:"nick"`
|
||||||
Name string `mapstructure:"name"`
|
Name string `mapstructure:"name"`
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
|
func serveCustomPage(blog *configBlog, page *customPage) func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
render(w, page.Template, &renderData{
|
||||||
|
Blog: blog,
|
||||||
|
Data: page.Data,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
12
feeds.go
12
feeds.go
|
@ -45,26 +45,22 @@ func generateFeed(blog string, f feedType, w http.ResponseWriter, r *http.Reques
|
||||||
var err error
|
var err error
|
||||||
switch f {
|
switch f {
|
||||||
case rssFeed:
|
case rssFeed:
|
||||||
|
w.Header().Add(contentType, "application/rss+xml; charset=utf-8")
|
||||||
feedStr, err = feed.ToRss()
|
feedStr, err = feed.ToRss()
|
||||||
case atomFeed:
|
case atomFeed:
|
||||||
|
w.Header().Add(contentType, "application/atom+xml; charset=utf-8")
|
||||||
feedStr, err = feed.ToAtom()
|
feedStr, err = feed.ToAtom()
|
||||||
case jsonFeed:
|
case jsonFeed:
|
||||||
|
w.Header().Add(contentType, "application/feed+json; charset=utf-8")
|
||||||
feedStr, err = feed.ToJSON()
|
feedStr, err = feed.ToJSON()
|
||||||
default:
|
default:
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
w.Header().Del(contentType)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch f {
|
|
||||||
case rssFeed:
|
|
||||||
w.Header().Add(contentType, "application/rss+xml; charset=utf-8")
|
|
||||||
case atomFeed:
|
|
||||||
w.Header().Add(contentType, "application/atom+xml; charset=utf-8")
|
|
||||||
case jsonFeed:
|
|
||||||
w.Header().Add(contentType, "application/feed+json; charset=utf-8")
|
|
||||||
}
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, _ = w.Write([]byte(feedStr))
|
_, _ = w.Write([]byte(feedStr))
|
||||||
}
|
}
|
||||||
|
|
16
http.go
16
http.go
|
@ -12,9 +12,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const contentType = "Content-Type"
|
const contentType = "Content-Type"
|
||||||
const contentTypeHTMLUTF8 = "text/html; charset=utf-8"
|
const charsetUtf8Suffix = "; charset=utf-8"
|
||||||
const contentTypeJSONUTF8 = "application/json; charset=utf-8"
|
const contentTypeHTML = "text/html"
|
||||||
|
const contentTypeHTMLUTF8 = contentTypeHTML + charsetUtf8Suffix
|
||||||
const contentTypeJSON = "application/json"
|
const contentTypeJSON = "application/json"
|
||||||
|
const contentTypeJSONUTF8 = contentTypeJSON + charsetUtf8Suffix
|
||||||
const contentTypeWWWForm = "application/x-www-form-urlencoded"
|
const contentTypeWWWForm = "application/x-www-form-urlencoded"
|
||||||
const contentTypeMultipartForm = "multipart/form-data"
|
const contentTypeMultipartForm = "multipart/form-data"
|
||||||
|
|
||||||
|
@ -171,6 +173,16 @@ func buildHandler() (http.Handler, error) {
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+jsonPath, serveHome(blog, blogPath, jsonFeed))
|
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+jsonPath, serveHome(blog, blogPath, jsonFeed))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+atomPath, serveHome(blog, blogPath, atomFeed))
|
r.With(cacheMiddleware, minifier.Middleware).Get(fullBlogPath+atomPath, serveHome(blog, blogPath, atomFeed))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+paginationPath, serveHome(blog, blogPath, noFeed))
|
r.With(cacheMiddleware, minifier.Middleware).Get(blogPath+paginationPath, serveHome(blog, blogPath, noFeed))
|
||||||
|
|
||||||
|
// Custom pages
|
||||||
|
for _, cp := range blogConfig.CustomPages {
|
||||||
|
serveFunc := serveCustomPage(blogConfig, cp)
|
||||||
|
if cp.Cache {
|
||||||
|
r.With(cacheMiddleware, minifier.Middleware).Get(cp.Path, serveFunc)
|
||||||
|
} else {
|
||||||
|
r.With(minifier.Middleware).Get(cp.Path, serveFunc)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sitemap
|
// Sitemap
|
||||||
|
|
|
@ -13,7 +13,7 @@ var minifier *minify.M
|
||||||
|
|
||||||
func initMinify() {
|
func initMinify() {
|
||||||
minifier = minify.New()
|
minifier = minify.New()
|
||||||
minifier.AddFunc("text/html", mHtml.Minify)
|
minifier.AddFunc(contentTypeHTML, mHtml.Minify)
|
||||||
minifier.AddFunc("text/css", mCss.Minify)
|
minifier.AddFunc("text/css", mCss.Minify)
|
||||||
minifier.AddFunc("application/javascript", mJs.Minify)
|
minifier.AddFunc("application/javascript", mJs.Minify)
|
||||||
minifier.AddFunc("application/rss+xml", mXml.Minify)
|
minifier.AddFunc("application/rss+xml", mXml.Minify)
|
||||||
|
|
16
render.go
16
render.go
|
@ -2,7 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -92,6 +94,20 @@ func initRendering() error {
|
||||||
}
|
}
|
||||||
return path
|
return path
|
||||||
},
|
},
|
||||||
|
"jsonFile": func(filename string) *interface{} {
|
||||||
|
parsed := []*interface{}{}
|
||||||
|
content, err := ioutil.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
contentString := "[" + string(content) + "]"
|
||||||
|
err = json.Unmarshal([]byte(contentString), &parsed)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return parsed[0]
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
templates = make(map[string]*template.Template)
|
templates = make(map[string]*template.Template)
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
{{ define "title" }}
|
||||||
|
<title>Blogroll - {{ .Blog.Title }}</title>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "main" }}
|
||||||
|
<main>
|
||||||
|
{{ $opmlJson := index ( jsonFile "data/opml.json" ) "outline" }}
|
||||||
|
{{ range $opmlJson }}
|
||||||
|
{{ md (printf "%s %s" "##" ._text) }}
|
||||||
|
<ul>
|
||||||
|
{{ range (index . "outline") }}
|
||||||
|
<li><a href="{{ ._htmlUrl }}" target="_blank">{{ ._title }}</a> (<a href="{{ ._xmlUrl }}" target="_blank">Feed</a>)</li>
|
||||||
|
{{ end }}
|
||||||
|
</ul>
|
||||||
|
{{ end }}
|
||||||
|
</main>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "blogroll" }}
|
||||||
|
{{ template "base" . }}
|
||||||
|
{{ end }}
|
Loading…
Reference in New Issue