mirror of https://github.com/jlelse/GoBlog
Add simple index (or /blog) site
This commit is contained in:
parent
fa10bd03d3
commit
626000ab96
23
http.go
23
http.go
|
@ -10,7 +10,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const contentTypeHTML = "text/html"
|
const contentTypeHTML = "text/html; charset=utf-8"
|
||||||
|
|
||||||
var d *dynamicHandler
|
var d *dynamicHandler
|
||||||
|
|
||||||
|
@ -53,12 +53,9 @@ func buildHandler() (http.Handler, error) {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
|
|
||||||
if appConfig.Server.Logging {
|
if appConfig.Server.Logging {
|
||||||
r.Use(middleware.RealIP)
|
r.Use(middleware.RealIP, middleware.Logger)
|
||||||
r.Use(middleware.Logger)
|
|
||||||
}
|
}
|
||||||
r.Use(middleware.Recoverer)
|
r.Use(middleware.Recoverer, middleware.StripSlashes, middleware.GetHead)
|
||||||
r.Use(middleware.StripSlashes)
|
|
||||||
r.Use(middleware.GetHead)
|
|
||||||
|
|
||||||
r.Route("/api", func(apiRouter chi.Router) {
|
r.Route("/api", func(apiRouter chi.Router) {
|
||||||
apiRouter.Use(middleware.BasicAuth("API", map[string]string{
|
apiRouter.Use(middleware.BasicAuth("API", map[string]string{
|
||||||
|
@ -88,11 +85,25 @@ func buildHandler() (http.Handler, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
routePatterns := routesToStringSlice(r.Routes())
|
||||||
|
if !routePatterns.has("/") {
|
||||||
|
r.With(cacheMiddleware, minifier.Middleware).Get("/", serveIndex)
|
||||||
|
} else if !routePatterns.has("/blog") {
|
||||||
|
r.With(cacheMiddleware, minifier.Middleware).Get("/blog", serveIndex)
|
||||||
|
}
|
||||||
|
|
||||||
r.With(minifier.Middleware).NotFound(serve404)
|
r.With(minifier.Middleware).NotFound(serve404)
|
||||||
|
|
||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func routesToStringSlice(routes []chi.Route) (ss stringSlice) {
|
||||||
|
for _, r := range routes {
|
||||||
|
ss = append(ss, r.Pattern)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
type dynamicHandler struct {
|
type dynamicHandler struct {
|
||||||
realHandler http.Handler
|
realHandler http.Handler
|
||||||
changeMutex *sync.Mutex
|
changeMutex *sync.Mutex
|
||||||
|
|
|
@ -12,7 +12,7 @@ var minifier *minify.M
|
||||||
|
|
||||||
func initMinify() {
|
func initMinify() {
|
||||||
minifier = minify.New()
|
minifier = minify.New()
|
||||||
minifier.AddFunc(contentTypeHTML, html.Minify)
|
minifier.AddFunc("text/html", html.Minify)
|
||||||
minifier.AddFunc("text/css", css.Minify)
|
minifier.AddFunc("text/css", css.Minify)
|
||||||
minifier.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
|
minifier.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
|
||||||
}
|
}
|
||||||
|
|
9
posts.go
9
posts.go
|
@ -31,6 +31,15 @@ func servePost(w http.ResponseWriter, r *http.Request) {
|
||||||
render(w, templatePost, post)
|
render(w, templatePost, post)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func serveIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
|
posts, err := getAllPosts(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
render(w, templateIndex, posts)
|
||||||
|
}
|
||||||
|
|
||||||
func getPost(context context.Context, path string) (*Post, error) {
|
func getPost(context context.Context, path string) (*Post, error) {
|
||||||
posts, err := getPosts(context, path)
|
posts, err := getPosts(context, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -11,6 +11,8 @@ import (
|
||||||
const templatePost = "post"
|
const templatePost = "post"
|
||||||
const templateError = "error"
|
const templateError = "error"
|
||||||
const templateRedirect = "redirect"
|
const templateRedirect = "redirect"
|
||||||
|
const templateIndex = "index"
|
||||||
|
const templateSummary = "summary"
|
||||||
|
|
||||||
var templates map[string]*template.Template
|
var templates map[string]*template.Template
|
||||||
var templateFunctions template.FuncMap
|
var templateFunctions template.FuncMap
|
||||||
|
@ -31,10 +33,15 @@ func initRendering() {
|
||||||
"p": func(post Post, parameter string) string {
|
"p": func(post Post, parameter string) string {
|
||||||
return post.Parameters[parameter]
|
return post.Parameters[parameter]
|
||||||
},
|
},
|
||||||
|
"include": func(templateName string, data interface{}) (template.HTML, error) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
err := templates[templateName].ExecuteTemplate(buf, templateName, data)
|
||||||
|
return template.HTML(buf.String()), err
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
templates = make(map[string]*template.Template)
|
templates = make(map[string]*template.Template)
|
||||||
for _, name := range []string{templatePost, templateError, templateRedirect} {
|
for _, name := range []string{templatePost, templateError, templateRedirect, templateIndex, templateSummary} {
|
||||||
templates[name] = loadTemplate(name)
|
templates[name] = loadTemplate(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
{{ define "title" }}
|
||||||
|
<title>{{ blog.Title }}</title>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "main" }}
|
||||||
|
<main>
|
||||||
|
{{ range $i, $post := . }}
|
||||||
|
{{ include "summary" . }}
|
||||||
|
{{ end }}
|
||||||
|
</main>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "index" }}
|
||||||
|
{{ template "base" . }}
|
||||||
|
{{ end }}
|
|
@ -13,6 +13,6 @@
|
||||||
</main>
|
</main>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ define "post"}}
|
{{ define "post" }}
|
||||||
{{ template "base" . }}
|
{{ template "base" . }}
|
||||||
{{ end }}
|
{{ end }}
|
|
@ -0,0 +1,8 @@
|
||||||
|
{{ define "summary" }}
|
||||||
|
<article>
|
||||||
|
{{ with p . "title" }}<h2>{{ . }}</h2>{{ end }}
|
||||||
|
<p>{{ md .Content }}</p>
|
||||||
|
<a href="{{ .Path }}">View</a>
|
||||||
|
</article>
|
||||||
|
<hr>
|
||||||
|
{{ end }}
|
Loading…
Reference in New Issue