Add image/asset inclusion (#2)
Co-authored-by: jolheiser <john.olheiser@gmail.com> Reviewed-on: #2main v0.0.2
parent
5920853468
commit
34bfed8ee7
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
title = "Cool"
|
||||
date = 2022-07-07
|
||||
tags = ["gopher", "cool"]
|
||||
---
|
||||
|
||||
# Cool
|
||||
|
||||
![gophercool](./gophercool.png)
|
|
@ -1,7 +1,9 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"go.jolheiser.com/blog/post"
|
||||
"go.jolheiser.com/blog/static"
|
||||
|
@ -18,7 +20,10 @@ func New(blog *post.Blog) *chi.Mux {
|
|||
m.Use(middleware.Recoverer)
|
||||
|
||||
m.Get("/", indexHandler(blog))
|
||||
m.Get("/{post}", fileHandler(blog))
|
||||
m.Route("/{post}", func(r chi.Router) {
|
||||
r.With(slashesMiddleware).Get("/", fileHandler(blog))
|
||||
r.Get("/{asset}", assetHandler(blog))
|
||||
})
|
||||
m.Route("/_", func(r chi.Router) {
|
||||
r.Get("/sakura.css", static.SakuraCSS)
|
||||
})
|
||||
|
@ -48,3 +53,41 @@ func fileHandler(blog *post.Blog) http.HandlerFunc {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func assetHandler(blog *post.Blog) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
postName := chi.URLParam(r, "post")
|
||||
p, ok := blog.Post(postName)
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
assetName := chi.URLParam(r, "asset")
|
||||
apn := filepath.Join(filepath.Dir(p.Path), assetName)
|
||||
http.ServeFile(w, r, apn)
|
||||
}
|
||||
}
|
||||
|
||||
func slashesMiddleware(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
var path string
|
||||
rctx := chi.RouteContext(r.Context())
|
||||
if rctx != nil && rctx.RoutePath != "" {
|
||||
path = rctx.RoutePath
|
||||
} else {
|
||||
path = r.URL.Path
|
||||
}
|
||||
if len(path) > 1 && path[len(path)-1] != '/' {
|
||||
path += "/"
|
||||
if r.URL.RawQuery != "" {
|
||||
path = fmt.Sprintf("%s?%s", path, r.URL.RawQuery)
|
||||
}
|
||||
redirectURL := fmt.Sprintf("//%s%s", r.Host, path)
|
||||
http.Redirect(w, r, redirectURL, 301)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
<h1 class="title">Blog Posts</h1>
|
||||
{{range .SortedPosts}}
|
||||
<h5>
|
||||
<a href="{{.Slug}}"><strong>{{.Title}}</strong></a><br/>
|
||||
<a href="{{.Slug}}/"><strong>{{.Title}}</strong></a><br/>
|
||||
{{if .Author}}<small>@{{.Author}}</small>{{end}}<span class="right">{{.Date.Format "Jan 2, 2006"}}</span><br/>
|
||||
{{range .Tags}}<code class="tag">{{.}}</code> {{end}}
|
||||
</h5>
|
||||
|
|
|
@ -4,10 +4,10 @@
|
|||
<meta charset="UTF-8">
|
||||
<title>{{.Title}}</title>
|
||||
<link rel="icon" href="data:,">
|
||||
<link rel="stylesheet" href="_/sakura.css"/>
|
||||
<link rel="stylesheet" href="../_/sakura.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<a style="position: absolute; left: 1em;" href="../../">Index</a>
|
||||
<a style="position: absolute; left: 1em;" href="../">Index</a>
|
||||
{{.Content}}
|
||||
</body>
|
||||
<script>
|
||||
|
|
Loading…
Reference in New Issue