Add BlogURL template function
This commit is contained in:
parent
0fdece68c0
commit
e742a2d6d5
@ -26,6 +26,10 @@ type Params struct {
|
|||||||
Logger *mlog.Logger
|
Logger *mlog.Logger
|
||||||
PowManager pow.Manager
|
PowManager pow.Manager
|
||||||
|
|
||||||
|
// PathPrefix, if given, will be prefixed to all url paths which are
|
||||||
|
// rendered by the API's templating system.
|
||||||
|
PathPrefix string
|
||||||
|
|
||||||
PostStore post.Store
|
PostStore post.Store
|
||||||
PostAssetStore post.AssetStore
|
PostAssetStore post.AssetStore
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ import (
|
|||||||
//go:embed tpl
|
//go:embed tpl
|
||||||
var tplFS embed.FS
|
var tplFS embed.FS
|
||||||
|
|
||||||
func mustParseTpl(name string) *template.Template {
|
func (a *api) mustParseTpl(name string) *template.Template {
|
||||||
|
|
||||||
mustRead := func(fileName string) string {
|
mustRead := func(fileName string) string {
|
||||||
path := filepath.Join("tpl", fileName)
|
path := filepath.Join("tpl", fileName)
|
||||||
@ -33,7 +33,15 @@ func mustParseTpl(name string) *template.Template {
|
|||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
tpl := template.Must(template.New("").Parse(mustRead(name)))
|
blogURL := func(path string) string {
|
||||||
|
return filepath.Join(a.params.PathPrefix, "/v2", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl := template.New("").Funcs(template.FuncMap{
|
||||||
|
"BlogURL": blogURL,
|
||||||
|
})
|
||||||
|
|
||||||
|
tpl = template.Must(tpl.Parse(mustRead(name)))
|
||||||
tpl = template.Must(tpl.New("base.html").Parse(mustRead("base.html")))
|
tpl = template.Must(tpl.New("base.html").Parse(mustRead("base.html")))
|
||||||
|
|
||||||
return tpl
|
return tpl
|
||||||
@ -41,7 +49,7 @@ func mustParseTpl(name string) *template.Template {
|
|||||||
|
|
||||||
func (a *api) renderIndexHandler() http.Handler {
|
func (a *api) renderIndexHandler() http.Handler {
|
||||||
|
|
||||||
tpl := mustParseTpl("index.html")
|
tpl := a.mustParseTpl("index.html")
|
||||||
const pageCount = 10
|
const pageCount = 10
|
||||||
|
|
||||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
@ -95,7 +103,7 @@ func (a *api) renderIndexHandler() http.Handler {
|
|||||||
|
|
||||||
func (a *api) renderPostHandler() http.Handler {
|
func (a *api) renderPostHandler() http.Handler {
|
||||||
|
|
||||||
tpl := mustParseTpl("post.html")
|
tpl := a.mustParseTpl("post.html")
|
||||||
|
|
||||||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="seven columns" style="margin-bottom: 3rem;">
|
<div class="seven columns" style="margin-bottom: 3rem;">
|
||||||
<h1 class="title">
|
<h1 class="title">
|
||||||
<a href="/">Mediocre Blog</a>
|
<a href="{{ BlogURL "/" }}">Mediocre Blog</a>
|
||||||
</h1>
|
</h1>
|
||||||
<div class="light social">
|
<div class="light social">
|
||||||
<span>By Brian Picciano</span>
|
<span>By Brian Picciano</span>
|
||||||
@ -31,13 +31,14 @@
|
|||||||
|
|
||||||
<div class="five columns light">
|
<div class="five columns light">
|
||||||
<span style="display:block; margin-bottom:0.5rem;">Get notified when new posts are published!</span>
|
<span style="display:block; margin-bottom:0.5rem;">Get notified when new posts are published!</span>
|
||||||
<a href="/follow.html">
|
<a href="{{ BlogURL "follow.html" }}">
|
||||||
<button class="button-primary">
|
<button class="button-primary">
|
||||||
<i class="far fa-envelope"></i>
|
<i class="far fa-envelope"></i>
|
||||||
Follow
|
Follow
|
||||||
</button>
|
</button>
|
||||||
</a>
|
</a>
|
||||||
<a href="/feed.xml">
|
|
||||||
|
<a href="{{ BlogURL "feed.xml" }}">
|
||||||
<button class="button">
|
<button class="button">
|
||||||
<i class="fas fa-rss"></i>
|
<i class="fas fa-rss"></i>
|
||||||
RSS
|
RSS
|
||||||
|
@ -53,6 +53,8 @@ func main() {
|
|||||||
chatGlobalRoomMaxMsgs := cfg.Int("chat-global-room-max-messages", 1000, "Maximum number of messages the global chat room can retain")
|
chatGlobalRoomMaxMsgs := cfg.Int("chat-global-room-max-messages", 1000, "Maximum number of messages the global chat room can retain")
|
||||||
chatUserIDCalcSecret := cfg.String("chat-user-id-calc-secret", "", "Secret to use when calculating user ids")
|
chatUserIDCalcSecret := cfg.String("chat-user-id-calc-secret", "", "Secret to use when calculating user ids")
|
||||||
|
|
||||||
|
pathPrefix := cfg.String("path-prefix", "", "Prefix which is optionally applied to all URL paths rendered by the blog")
|
||||||
|
|
||||||
// initialization
|
// initialization
|
||||||
err := cfg.Init(ctx)
|
err := cfg.Init(ctx)
|
||||||
|
|
||||||
@ -70,6 +72,10 @@ func main() {
|
|||||||
"chatGlobalRoomMaxMsgs", *chatGlobalRoomMaxMsgs,
|
"chatGlobalRoomMaxMsgs", *chatGlobalRoomMaxMsgs,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if *pathPrefix != "" {
|
||||||
|
ctx = mctx.Annotate(ctx, "pathPrefix", *pathPrefix)
|
||||||
|
}
|
||||||
|
|
||||||
clock := clock.Realtime()
|
clock := clock.Realtime()
|
||||||
|
|
||||||
powStore := pow.NewMemoryStore(clock)
|
powStore := pow.NewMemoryStore(clock)
|
||||||
@ -124,6 +130,7 @@ func main() {
|
|||||||
|
|
||||||
apiParams.Logger = logger.WithNamespace("api")
|
apiParams.Logger = logger.WithNamespace("api")
|
||||||
apiParams.PowManager = powMgr
|
apiParams.PowManager = powMgr
|
||||||
|
apiParams.PathPrefix = *pathPrefix
|
||||||
apiParams.PostStore = postStore
|
apiParams.PostStore = postStore
|
||||||
apiParams.PostAssetStore = postAssetStore
|
apiParams.PostAssetStore = postAssetStore
|
||||||
apiParams.MailingList = ml
|
apiParams.MailingList = ml
|
||||||
|
Loading…
Reference in New Issue
Block a user