GoBlog/posts.go

236 lines
5.7 KiB
Go
Raw Normal View History

2020-07-28 19:17:07 +00:00
package main
import (
"errors"
2020-08-05 17:54:04 +00:00
"fmt"
2020-10-18 09:54:29 +00:00
"html/template"
2020-07-28 19:17:07 +00:00
"net/http"
"reflect"
2020-08-05 17:54:04 +00:00
"strconv"
2020-09-24 15:13:03 +00:00
"strings"
2020-10-06 17:07:48 +00:00
"github.com/go-chi/chi"
"github.com/vcraescu/go-paginator"
2020-07-28 19:17:07 +00:00
)
2020-07-30 19:18:13 +00:00
var errPostNotFound = errors.New("post not found")
2020-07-28 19:17:07 +00:00
2020-10-15 15:32:46 +00:00
type post struct {
2020-08-31 19:12:43 +00:00
Path string `json:"path"`
Content string `json:"content"`
Published string `json:"published"`
Updated string `json:"updated"`
Parameters map[string][]string `json:"parameters"`
2020-10-06 17:07:48 +00:00
Blog string `json:"blog"`
Section string `json:"section"`
// Not persisted
2020-10-18 09:54:29 +00:00
Slug string `json:"slug"`
rendered template.HTML
2020-07-28 19:17:07 +00:00
}
2020-07-29 14:41:36 +00:00
func servePost(w http.ResponseWriter, r *http.Request) {
as := strings.HasSuffix(r.URL.Path, ".as")
if as {
r.URL.Path = strings.TrimSuffix(r.URL.Path, ".as")
2020-09-24 15:13:03 +00:00
}
p, err := getPost(r.URL.Path)
2020-07-30 19:18:13 +00:00
if err == errPostNotFound {
serve404(w, r)
2020-07-29 14:41:36 +00:00
return
2020-07-28 19:17:07 +00:00
} else if err != nil {
2020-07-29 14:41:36 +00:00
http.Error(w, err.Error(), http.StatusInternalServerError)
return
2020-07-28 19:17:07 +00:00
}
if as {
p.serveActivityStreams(w)
return
}
2020-11-01 17:37:21 +00:00
canonical := p.firstParameter("original")
if canonical == "" {
canonical = appConfig.Server.PublicAddress + p.Path
}
2020-10-12 16:47:23 +00:00
render(w, templatePost, &renderData{
2020-10-15 15:32:46 +00:00
blogString: p.Blog,
2020-11-01 17:37:21 +00:00
Canonical: canonical,
2020-10-15 15:32:46 +00:00
Data: p,
2020-10-12 16:47:23 +00:00
})
2020-07-28 19:17:07 +00:00
}
type indexTemplateData struct {
2020-10-06 17:07:48 +00:00
Blog string
Title string
Description string
2020-10-15 15:32:46 +00:00
Posts []*post
HasPrev bool
HasNext bool
2020-09-02 15:56:18 +00:00
First string
Prev string
Next string
2020-08-05 17:54:04 +00:00
}
type postPaginationAdapter struct {
2020-10-19 18:25:30 +00:00
config *postsRequestConfig
nums int
}
func (p *postPaginationAdapter) Nums() int {
if p.nums == 0 {
2020-10-19 18:25:30 +00:00
p.nums, _ = countPosts(p.config)
}
return p.nums
}
func (p *postPaginationAdapter) Slice(offset, length int, data interface{}) error {
if reflect.TypeOf(data).Kind() != reflect.Ptr {
panic("data has to be a pointer")
}
2020-08-31 19:12:43 +00:00
modifiedConfig := *p.config
modifiedConfig.offset = offset
modifiedConfig.limit = length
2020-10-19 18:25:30 +00:00
posts, err := getPosts(&modifiedConfig)
reflect.ValueOf(data).Elem().Set(reflect.ValueOf(&posts).Elem())
return err
}
2020-10-15 17:50:34 +00:00
func serveHome(blog string, path string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
as := strings.HasSuffix(r.URL.Path, ".as")
if as {
appConfig.Blogs[blog].serveActivityStreams(blog, w)
return
}
serveIndex(&indexConfig{
blog: blog,
path: path,
})(w, r)
}
2020-08-25 18:55:32 +00:00
}
2020-10-15 17:50:34 +00:00
func serveSection(blog string, path string, section *section) func(w http.ResponseWriter, r *http.Request) {
2020-09-21 16:03:05 +00:00
return serveIndex(&indexConfig{
2020-10-06 17:07:48 +00:00
blog: blog,
2020-09-21 16:03:05 +00:00
path: path,
section: section,
})
2020-08-31 19:12:43 +00:00
}
2020-10-06 17:07:48 +00:00
func serveTaxonomy(blog string, tax *taxonomy) func(w http.ResponseWriter, r *http.Request) {
2020-08-31 19:12:43 +00:00
return func(w http.ResponseWriter, r *http.Request) {
2020-10-06 17:07:48 +00:00
allValues, err := allTaxonomyValues(blog, tax.Name)
2020-08-31 19:12:43 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2020-10-12 16:47:23 +00:00
render(w, templateTaxonomy, &renderData{
blogString: blog,
Canonical: appConfig.Server.PublicAddress + r.URL.Path,
2020-10-12 16:47:23 +00:00
Data: struct {
Taxonomy *taxonomy
TaxonomyValues []string
}{
Taxonomy: tax,
TaxonomyValues: allValues,
},
2020-08-31 19:12:43 +00:00
})
}
2020-08-25 18:55:32 +00:00
}
2020-10-15 17:50:34 +00:00
func serveTaxonomyValue(blog string, path string, tax *taxonomy, value string) func(w http.ResponseWriter, r *http.Request) {
2020-09-21 16:03:05 +00:00
return serveIndex(&indexConfig{
2020-10-06 17:07:48 +00:00
blog: blog,
2020-09-21 16:03:05 +00:00
path: path,
tax: tax,
taxValue: value,
})
2020-08-31 19:12:43 +00:00
}
2020-10-06 17:07:48 +00:00
func servePhotos(blog string) func(w http.ResponseWriter, r *http.Request) {
2020-09-21 16:03:05 +00:00
return serveIndex(&indexConfig{
blog: blog,
path: appConfig.Blogs[blog].Photos.Path,
parameter: appConfig.Blogs[blog].Photos.Parameter,
template: templatePhotos,
2020-09-21 16:03:05 +00:00
})
}
type indexConfig struct {
blog string
path string
section *section
tax *taxonomy
taxValue string
parameter string
template string
2020-09-21 16:03:05 +00:00
}
func serveIndex(ic *indexConfig) func(w http.ResponseWriter, r *http.Request) {
2020-08-05 17:54:04 +00:00
return func(w http.ResponseWriter, r *http.Request) {
pageNoString := chi.URLParam(r, "page")
pageNo, _ := strconv.Atoi(pageNoString)
2020-10-06 17:07:48 +00:00
var sections []string
2020-09-21 16:03:05 +00:00
if ic.section != nil {
2020-10-06 17:07:48 +00:00
sections = []string{ic.section.Name}
} else {
for sectionKey := range appConfig.Blogs[ic.blog].Sections {
sections = append(sections, sectionKey)
}
2020-08-25 18:55:32 +00:00
}
2020-10-19 18:25:30 +00:00
p := paginator.New(&postPaginationAdapter{config: &postsRequestConfig{
blog: ic.blog,
sections: sections,
taxonomy: ic.tax,
taxonomyValue: ic.taxValue,
parameter: ic.parameter,
2020-10-06 17:07:48 +00:00
}}, appConfig.Blogs[ic.blog].Pagination)
p.SetPage(pageNo)
2020-10-15 15:32:46 +00:00
var posts []*post
err := p.Results(&posts)
2020-08-05 17:54:04 +00:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Meta
var title, description string
2020-09-21 16:03:05 +00:00
if ic.tax != nil {
title = fmt.Sprintf("%s: %s", ic.tax.Title, ic.taxValue)
} else if ic.section != nil {
title = ic.section.Title
description = ic.section.Description
}
// Check if feed
2020-10-15 17:50:34 +00:00
if ft := feedType(chi.URLParam(r, "feed")); ft != noFeed {
generateFeed(ic.blog, ft, w, r, posts, title, description)
2020-09-02 15:48:37 +00:00
return
}
// Navigation
prevPage, err := p.PrevPage()
if err == paginator.ErrNoPrevPage {
prevPage = p.Page()
}
nextPage, err := p.NextPage()
if err == paginator.ErrNoNextPage {
nextPage = p.Page()
}
2020-09-21 16:03:05 +00:00
template := ic.template
if len(template) == 0 {
template = templateIndex
}
2020-10-12 16:47:23 +00:00
render(w, template, &renderData{
blogString: ic.blog,
Canonical: appConfig.Server.PublicAddress + r.URL.Path,
2020-10-12 16:47:23 +00:00
Data: &indexTemplateData{
Title: title,
Description: description,
Posts: posts,
HasPrev: p.HasPrev(),
HasNext: p.HasNext(),
First: ic.path,
Prev: fmt.Sprintf("%s/page/%d", ic.path, prevPage),
Next: fmt.Sprintf("%s/page/%d", ic.path, nextPage),
},
2020-08-05 17:54:04 +00:00
})
2020-08-05 17:14:10 +00:00
}
}