mirror of https://github.com/jlelse/GoBlog
Simplify
This commit is contained in:
parent
302e290ea2
commit
5e65fd6bd7
4
cache.go
4
cache.go
|
@ -137,7 +137,7 @@ func getCache(key string, next http.Handler, r *http.Request) (item *cacheItem)
|
||||||
}
|
}
|
||||||
if item == nil || item.expired() {
|
if item == nil || item.expired() {
|
||||||
// No cache available
|
// No cache available
|
||||||
servertiming.FromContext(r.Context()).NewMetric("cacheMiss")
|
servertiming.FromContext(r.Context()).NewMetric("cm")
|
||||||
// Remove problematic headers
|
// Remove problematic headers
|
||||||
r.Header.Del("If-Modified-Since")
|
r.Header.Del("If-Modified-Since")
|
||||||
r.Header.Del("If-Unmodified-Since")
|
r.Header.Del("If-Unmodified-Since")
|
||||||
|
@ -185,7 +185,7 @@ func getCache(key string, next http.Handler, r *http.Request) (item *cacheItem)
|
||||||
cacheLru.Add(key, item)
|
cacheLru.Add(key, item)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
servertiming.FromContext(r.Context()).NewMetric("cache")
|
servertiming.FromContext(r.Context()).NewMetric("c")
|
||||||
}
|
}
|
||||||
return item
|
return item
|
||||||
}
|
}
|
||||||
|
|
32
render.go
32
render.go
|
@ -11,7 +11,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -47,9 +46,6 @@ var templateFunctions template.FuncMap
|
||||||
|
|
||||||
func initRendering() error {
|
func initRendering() error {
|
||||||
templateFunctions = template.FuncMap{
|
templateFunctions = template.FuncMap{
|
||||||
"blog": func(blog string) *configBlog {
|
|
||||||
return appConfig.Blogs[blog]
|
|
||||||
},
|
|
||||||
"menu": func(blog *configBlog, id string) *menu {
|
"menu": func(blog *configBlog, id string) *menu {
|
||||||
return blog.Menus[id]
|
return blog.Menus[id]
|
||||||
},
|
},
|
||||||
|
@ -133,36 +129,12 @@ func initRendering() error {
|
||||||
nrd.Data = data[1]
|
nrd.Data = data[1]
|
||||||
rd = &nrd
|
rd = &nrd
|
||||||
}
|
}
|
||||||
buf := new(bytes.Buffer)
|
var buf bytes.Buffer
|
||||||
err := templates[templateName].ExecuteTemplate(buf, templateName, rd)
|
err := templates[templateName].ExecuteTemplate(&buf, templateName, rd)
|
||||||
return template.HTML(buf.String()), err
|
return template.HTML(buf.String()), err
|
||||||
}
|
}
|
||||||
return "", errors.New("wrong arguments")
|
return "", errors.New("wrong arguments")
|
||||||
},
|
},
|
||||||
"default": func(dflt interface{}, given ...interface{}) interface{} {
|
|
||||||
if len(given) == 0 {
|
|
||||||
return dflt
|
|
||||||
}
|
|
||||||
g := reflect.ValueOf(given[0])
|
|
||||||
if !g.IsValid() {
|
|
||||||
return dflt
|
|
||||||
}
|
|
||||||
set := false
|
|
||||||
switch g.Kind() {
|
|
||||||
case reflect.Bool:
|
|
||||||
set = true
|
|
||||||
case reflect.String, reflect.Array, reflect.Slice, reflect.Map:
|
|
||||||
set = g.Len() != 0
|
|
||||||
case reflect.Int:
|
|
||||||
set = g.Int() != 0
|
|
||||||
default:
|
|
||||||
set = !g.IsNil()
|
|
||||||
}
|
|
||||||
if set {
|
|
||||||
return given[0]
|
|
||||||
}
|
|
||||||
return dflt
|
|
||||||
},
|
|
||||||
"urlize": urlize,
|
"urlize": urlize,
|
||||||
"sort": sortedStrings,
|
"sort": sortedStrings,
|
||||||
"absolute": func(path string) string {
|
"absolute": func(path string) string {
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
<div class="p-author h-card hide">
|
<div class="p-author h-card hide">
|
||||||
{{ with .Picture }}<data class="u-photo" value="{{ . }}"></data>{{ end }}
|
{{ with .Picture }}<data class="u-photo" value="{{ . }}"></data>{{ end }}
|
||||||
{{ if .Name }}
|
{{ if .Name }}
|
||||||
<a href="{{ .Link | default "/" }}" class="p-name u-url" rel="me">{{ .Name }}</a>
|
<a href="{{ with .Link }}{{ . }}{{ else }}/{{ end }}" class="p-name u-url" rel="me">{{ .Name }}</a>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</div>
|
</div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
|
@ -49,7 +49,7 @@ tags:
|
||||||
<input type="hidden" name="editoraction" value="loadupdate">
|
<input type="hidden" name="editoraction" value="loadupdate">
|
||||||
<select name="url" class="fw">
|
<select name="url" class="fw">
|
||||||
{{ range $i, $draft := .Data.Drafts }}
|
{{ range $i, $draft := .Data.Drafts }}
|
||||||
<option value="{{ absolute $draft.Path }}">{{ (title $draft) | default $draft.Path }}</option>
|
<option value="{{ absolute $draft.Path }}">{{ with (title $draft) }}{{ . }}{{ else }}{{ $draft.Path }}{{ end }}</option>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</select>
|
</select>
|
||||||
<input class="fw" type="submit" value="{{ string .Blog.Lang "update" }}">
|
<input class="fw" type="submit" value="{{ string .Blog.Lang "update" }}">
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
{{ if .Data.Published }}<div>{{ string .Blog.Lang "publishedon" }} <time class="dt-published" datetime="{{ dateformat .Data.Published "2006-01-02T15:04:05Z07:00"}}">{{ isodate .Data.Published }}</time>{{ if $section }} in <a href="{{ blogrelative .Blog $section.Name }}">{{ $section.Title }}</a>{{ end }}</div>{{ end }}
|
{{ if .Data.Published }}<div>{{ string .Blog.Lang "publishedon" }} <time class="dt-published" datetime="{{ dateformat .Data.Published "2006-01-02T15:04:05Z07:00"}}">{{ isodate .Data.Published }}</time>{{ if $section }} in <a href="{{ blogrelative .Blog $section.Name }}">{{ $section.Title }}</a>{{ end }}</div>{{ end }}
|
||||||
{{ if .Data.Updated }}<div>{{ string .Blog.Lang "updatedon" }} <time class="dt-updated" datetime="{{ dateformat .Data.Updated "2006-01-02T15:04:05Z07:00"}}">{{ isodate .Data.Updated }}</time></div>{{ end }}
|
{{ if .Data.Updated }}<div>{{ string .Blog.Lang "updatedon" }} <time class="dt-updated" datetime="{{ dateformat .Data.Updated "2006-01-02T15:04:05Z07:00"}}">{{ isodate .Data.Updated }}</time></div>{{ end }}
|
||||||
{{ if p .Data "replylink" }}
|
{{ if p .Data "replylink" }}
|
||||||
<div>{{ string .Blog.Lang "replyto" }}: <a class="u-in-reply-to" href="{{ p .Data "replylink" }}" target="_blank" rel="noopener">{{ p .Data "replytitle" | default (p .Data "replylink") }}</a></div>
|
<div>{{ string .Blog.Lang "replyto" }}: <a class="u-in-reply-to" href="{{ p .Data "replylink" }}" target="_blank" rel="noopener">{{ with (p .Data "replytitle") }}{{ . }}{{ else }}{{ p .Data "replylink" }}{{ end }}</a></div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ if p .Data "likelink" }}
|
{{ if p .Data "likelink" }}
|
||||||
<div>{{ string .Blog.Lang "likeof" }}: <a class="u-like-of" href="{{ p .Data "likelink" }}" target="_blank" rel="noopener">{{ p .Data "liketitle" | default (p .Data "likelink") }}</a></div>
|
<div>{{ string .Blog.Lang "likeof" }}: <a class="u-like-of" href="{{ p .Data "likelink" }}" target="_blank" rel="noopener">{{ with (p .Data "liketitle") }}{{ . }}{{ else }}{{ p .Data "likelink" }}{{ end }}</a></div>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{ end }}
|
{{ end }}
|
Loading…
Reference in New Issue