mirror of
https://github.com/jlelse/GoBlog
synced 2024-06-14 13:27:08 +00:00
Taxonomies, sections and blog with title and description
This commit is contained in:
parent
484da515aa
commit
1d31cbe20a
23
config.go
23
config.go
|
@ -36,12 +36,26 @@ type configBlog struct {
|
||||||
Lang string `mapstructure:"lang"`
|
Lang string `mapstructure:"lang"`
|
||||||
// Title of the blog, e.g. "My blog"
|
// Title of the blog, e.g. "My blog"
|
||||||
Title string `mapstructure:"title"`
|
Title string `mapstructure:"title"`
|
||||||
|
// Description of the blog
|
||||||
|
Description string `mapstructure:"description"`
|
||||||
// Number of posts per page
|
// Number of posts per page
|
||||||
Pagination int `mapstructure:"pagination"`
|
Pagination int `mapstructure:"pagination"`
|
||||||
// Sections
|
// Sections
|
||||||
Sections []string `mapstructure:"sections"`
|
Sections []*section `mapstructure:"sections"`
|
||||||
// Taxonomies
|
// Taxonomies
|
||||||
Taxonomies []string `mapstructure:"taxonomies"`
|
Taxonomies []*taxonomy `mapstructure:"taxonomies"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type section struct {
|
||||||
|
Name string `mapstructure:"name"`
|
||||||
|
Title string `mapstructure:"title"`
|
||||||
|
Description string `mapstructure:"description"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type taxonomy struct {
|
||||||
|
Name string `mapstructure:"name"`
|
||||||
|
Title string `mapstructure:"title"`
|
||||||
|
Description string `mapstructure:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type configUser struct {
|
type configUser struct {
|
||||||
|
@ -71,9 +85,10 @@ func initConfig() error {
|
||||||
viper.SetDefault("cache.expiration", 600)
|
viper.SetDefault("cache.expiration", 600)
|
||||||
viper.SetDefault("blog.lang", "en")
|
viper.SetDefault("blog.lang", "en")
|
||||||
viper.SetDefault("blog.title", "My blog")
|
viper.SetDefault("blog.title", "My blog")
|
||||||
|
viper.SetDefault("blog.description", "This is my blog")
|
||||||
viper.SetDefault("blog.pagination", 10)
|
viper.SetDefault("blog.pagination", 10)
|
||||||
viper.SetDefault("blog.sections", []string{"posts"})
|
viper.SetDefault("blog.sections", []*section{{Name: "posts", Title: "Posts", Description: "**Posts** on this blog"}})
|
||||||
viper.SetDefault("blog.taxonomies", []string{"tags"})
|
viper.SetDefault("blog.taxonomies", []*taxonomy{{Name: "tags", Title: "Tags", Description: "**Tags** on this blog"}})
|
||||||
viper.SetDefault("user.nick", "admin")
|
viper.SetDefault("user.nick", "admin")
|
||||||
viper.SetDefault("user.name", "Admin")
|
viper.SetDefault("user.name", "Admin")
|
||||||
viper.SetDefault("user.password", "secret")
|
viper.SetDefault("user.password", "secret")
|
||||||
|
|
|
@ -13,10 +13,15 @@ cache:
|
||||||
blog:
|
blog:
|
||||||
lang: en
|
lang: en
|
||||||
title: My blog
|
title: My blog
|
||||||
|
description: This is my blog
|
||||||
sections:
|
sections:
|
||||||
- posts
|
- name: posts
|
||||||
|
title: Posts
|
||||||
|
description: "**Posts** on this blog"
|
||||||
taxonomies:
|
taxonomies:
|
||||||
- tags
|
- name: tags
|
||||||
|
title: Tags
|
||||||
|
description: "**Tags** on this blog"
|
||||||
user:
|
user:
|
||||||
nick: admin
|
nick: admin
|
||||||
name: Admin
|
name: Admin
|
||||||
|
|
15
http.go
15
http.go
|
@ -88,21 +88,22 @@ func buildHandler() (http.Handler, error) {
|
||||||
paginationPath := "/page/{page}"
|
paginationPath := "/page/{page}"
|
||||||
|
|
||||||
for _, section := range appConfig.Blog.Sections {
|
for _, section := range appConfig.Blog.Sections {
|
||||||
if section != "" {
|
if section.Name != "" {
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get("/"+section, serveSection("/"+section, section))
|
path := "/"+section.Name
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get("/"+section+paginationPath, serveSection("/"+section, section))
|
r.With(cacheMiddleware, minifier.Middleware).Get(path, serveSection(path, section))
|
||||||
|
r.With(cacheMiddleware, minifier.Middleware).Get(path+paginationPath, serveSection(path, section))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, taxonomy := range appConfig.Blog.Taxonomies {
|
for _, taxonomy := range appConfig.Blog.Taxonomies {
|
||||||
if taxonomy != "" {
|
if taxonomy.Name != "" {
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get("/"+taxonomy, serveTaxonomy(taxonomy))
|
r.With(cacheMiddleware, minifier.Middleware).Get("/"+taxonomy.Name, serveTaxonomy(taxonomy))
|
||||||
values, err := allTaxonomyValues(taxonomy)
|
values, err := allTaxonomyValues(taxonomy.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, tv := range values {
|
for _, tv := range values {
|
||||||
path := "/" + taxonomy + "/" + tv
|
path := "/" + taxonomy.Name + "/" + tv
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(path, serveTaxonomyValue(path, taxonomy, tv))
|
r.With(cacheMiddleware, minifier.Middleware).Get(path, serveTaxonomyValue(path, taxonomy, tv))
|
||||||
r.With(cacheMiddleware, minifier.Middleware).Get(path+paginationPath, serveTaxonomyValue(path, taxonomy, tv))
|
r.With(cacheMiddleware, minifier.Middleware).Get(path+paginationPath, serveTaxonomyValue(path, taxonomy, tv))
|
||||||
}
|
}
|
||||||
|
|
74
posts.go
74
posts.go
|
@ -36,12 +36,14 @@ func servePost(w http.ResponseWriter, r *http.Request) {
|
||||||
render(w, templatePost, post)
|
render(w, templatePost, post)
|
||||||
}
|
}
|
||||||
|
|
||||||
type indexTemplateDate struct {
|
type indexTemplateData struct {
|
||||||
Posts []*Post
|
Title string
|
||||||
HasPrev bool
|
Description string
|
||||||
HasNext bool
|
Posts []*Post
|
||||||
Prev string
|
HasPrev bool
|
||||||
Next string
|
HasNext bool
|
||||||
|
Prev string
|
||||||
|
Next string
|
||||||
}
|
}
|
||||||
|
|
||||||
type postPaginationAdapter struct {
|
type postPaginationAdapter struct {
|
||||||
|
@ -72,45 +74,45 @@ func (p *postPaginationAdapter) Slice(offset, length int, data interface{}) erro
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveHome(path string) func(w http.ResponseWriter, r *http.Request) {
|
func serveHome(path string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return serveIndex(path, "", "", "")
|
return serveIndex(path, nil, nil, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveSection(path, section string) func(w http.ResponseWriter, r *http.Request) {
|
func serveSection(path string, section *section) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return serveIndex(path, section, "", "")
|
return serveIndex(path, section, nil, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveTaxonomy(taxonomy string) func(w http.ResponseWriter, r *http.Request) {
|
func serveTaxonomy(tax *taxonomy) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
allValues, err := allTaxonomyValues(taxonomy)
|
allValues, err := allTaxonomyValues(tax.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
render(w, templateTaxonomy, struct {
|
render(w, templateTaxonomy, struct {
|
||||||
Taxonomy string
|
Taxonomy *taxonomy
|
||||||
TaxonomyValues []string
|
TaxonomyValues []string
|
||||||
}{
|
}{
|
||||||
Taxonomy: taxonomy,
|
Taxonomy: tax,
|
||||||
TaxonomyValues: allValues,
|
TaxonomyValues: allValues,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveTaxonomyValue(path, taxonomy, value string) func(w http.ResponseWriter, r *http.Request) {
|
func serveTaxonomyValue(path string, tax *taxonomy, value string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return serveIndex(path, "", taxonomy, value)
|
return serveIndex(path, nil, tax, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveIndex(path, section, taxonomy, taxonomyValue string) func(w http.ResponseWriter, r *http.Request) {
|
func serveIndex(path string, sec *section, tax *taxonomy, taxonomyValue string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
pageNoString := chi.URLParam(r, "page")
|
pageNoString := chi.URLParam(r, "page")
|
||||||
pageNo, _ := strconv.Atoi(pageNoString)
|
pageNo, _ := strconv.Atoi(pageNoString)
|
||||||
sections := appConfig.Blog.Sections
|
sections := appConfig.Blog.Sections
|
||||||
if len(section) > 0 {
|
if sec != nil {
|
||||||
sections = []string{section}
|
sections = []*section{sec}
|
||||||
}
|
}
|
||||||
p := paginator.New(&postPaginationAdapter{context: r.Context(), config: &postsRequestConfig{
|
p := paginator.New(&postPaginationAdapter{context: r.Context(), config: &postsRequestConfig{
|
||||||
sections: sections,
|
sections: sections,
|
||||||
taxonomy: taxonomy,
|
taxonomy: tax,
|
||||||
taxonomyValue: taxonomyValue,
|
taxonomyValue: taxonomyValue,
|
||||||
}}, appConfig.Blog.Pagination)
|
}}, appConfig.Blog.Pagination)
|
||||||
p.SetPage(pageNo)
|
p.SetPage(pageNo)
|
||||||
|
@ -128,12 +130,24 @@ func serveIndex(path, section, taxonomy, taxonomyValue string) func(w http.Respo
|
||||||
if err == paginator.ErrNoNextPage {
|
if err == paginator.ErrNoNextPage {
|
||||||
nextPage = p.Page()
|
nextPage = p.Page()
|
||||||
}
|
}
|
||||||
render(w, templateIndex, &indexTemplateDate{
|
var title, description string
|
||||||
Posts: posts,
|
if tax != nil {
|
||||||
HasPrev: p.HasPrev(),
|
title = fmt.Sprintf("%s: %s", tax.Title, taxonomyValue)
|
||||||
HasNext: p.HasNext(),
|
} else if sec != nil {
|
||||||
Prev: fmt.Sprintf("%s/page/%d", path, prevPage),
|
title = sec.Title
|
||||||
Next: fmt.Sprintf("%s/page/%d", path, nextPage),
|
description = sec.Description
|
||||||
|
} else if tax == nil && sec == nil {
|
||||||
|
title = appConfig.Blog.Title
|
||||||
|
description = appConfig.Blog.Description
|
||||||
|
}
|
||||||
|
render(w, templateIndex, &indexTemplateData{
|
||||||
|
Title: title,
|
||||||
|
Description: description,
|
||||||
|
Posts: posts,
|
||||||
|
HasPrev: p.HasPrev(),
|
||||||
|
HasNext: p.HasNext(),
|
||||||
|
Prev: fmt.Sprintf("%s/page/%d", path, prevPage),
|
||||||
|
Next: fmt.Sprintf("%s/page/%d", path, nextPage),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -152,8 +166,8 @@ type postsRequestConfig struct {
|
||||||
path string
|
path string
|
||||||
limit int
|
limit int
|
||||||
offset int
|
offset int
|
||||||
sections []string
|
sections []*section
|
||||||
taxonomy string
|
taxonomy *taxonomy
|
||||||
taxonomyValue string
|
taxonomyValue string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,8 +176,8 @@ func getPosts(context context.Context, config *postsRequestConfig) (posts []*Pos
|
||||||
var rows *sql.Rows
|
var rows *sql.Rows
|
||||||
defaultSelection := "select p.path, coalesce(content, ''), coalesce(published, ''), coalesce(updated, ''), coalesce(parameter, ''), coalesce(value, '') "
|
defaultSelection := "select p.path, coalesce(content, ''), coalesce(published, ''), coalesce(updated, ''), coalesce(parameter, ''), coalesce(value, '') "
|
||||||
postsTable := "posts"
|
postsTable := "posts"
|
||||||
if len(config.taxonomy) > 0 && len(config.taxonomyValue) > 0 {
|
if config.taxonomy != nil && len(config.taxonomyValue) > 0 {
|
||||||
postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.taxonomy + "' and pp.value = '" + config.taxonomyValue + "')"
|
postsTable = "(select distinct p.* from " + postsTable + " p left outer join post_parameters pp on p.path = pp.path where pp.parameter = '" + config.taxonomy.Name + "' and pp.value = '" + config.taxonomyValue + "')"
|
||||||
}
|
}
|
||||||
if len(config.sections) > 0 {
|
if len(config.sections) > 0 {
|
||||||
postsTable = "(select * from " + postsTable + " where"
|
postsTable = "(select * from " + postsTable + " where"
|
||||||
|
@ -171,7 +185,7 @@ func getPosts(context context.Context, config *postsRequestConfig) (posts []*Pos
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
postsTable += " or"
|
postsTable += " or"
|
||||||
}
|
}
|
||||||
postsTable += " path like '/" + section + "/%'"
|
postsTable += " path like '/" + section.Name + "/%'"
|
||||||
}
|
}
|
||||||
postsTable += ")"
|
postsTable += ")"
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,9 @@
|
||||||
|
|
||||||
{{ define "main" }}
|
{{ define "main" }}
|
||||||
<main>
|
<main>
|
||||||
|
{{ with .Title }}<h1>{{ . }}</h1>{{ end }}
|
||||||
|
{{ with .Description }}{{ md . }}{{ end }}
|
||||||
|
{{ if (or .Title .Description) }}<hr>{{ end }}
|
||||||
{{ range $i, $post := .Posts }}
|
{{ range $i, $post := .Posts }}
|
||||||
{{ include "summary" . }}
|
{{ include "summary" . }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
{{ define "title" }}
|
{{ define "title" }}
|
||||||
<title>{{ blog.Title }}</title>
|
<title>{{ .Taxonomy.Title }} - {{ blog.Title }}</title>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
{{ define "main" }}
|
{{ define "main" }}
|
||||||
<main>
|
<main>
|
||||||
<ul>
|
{{ with .Taxonomy.Title }}<h1>{{ . }}</h1>{{ end }}
|
||||||
{{ $taxonomy := .Taxonomy }}
|
{{ with .Taxonomy.Description }}{{ md . }}{{ end }}
|
||||||
{{ range $i, $value := .TaxonomyValues }}<li><a href="/{{ $taxonomy }}/{{ . }}">{{ . }}</a></li>{{ end }}
|
<ul>
|
||||||
</ul>
|
{{ $taxonomy := .Taxonomy.Name }}
|
||||||
|
{{ range $i, $value := .TaxonomyValues }}
|
||||||
|
<li><a href="/{{ $taxonomy }}/{{ . }}">{{ . }}</a></li>{{ end }}
|
||||||
|
</ul>
|
||||||
</main>
|
</main>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user