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