Improve method to get relative and full addresses (first unit tests!)

pull/7/head
Jan-Lukas Else 1 year ago
parent 4f1c07957d
commit a2f5ddbdcf
  1. 2
      activityPub.go
  2. 4
      activityStreams.go
  3. 13
      blogs.go
  4. 5
      blogstats.go
  5. 6
      comments.go
  6. 4
      commentsAdmin.go
  7. 2
      customPages.go
  8. 2
      feeds.go
  9. 2
      go.mod
  10. 3
      go.sum
  11. 2
      healthcheck.go
  12. 47
      http.go
  13. 2
      micropub.go
  14. 2
      micropubMedia.go
  15. 2
      nodeinfo.go
  16. 4
      notifications.go
  17. 4
      opensearch.go
  18. 45
      paths.go
  19. 108
      paths_test.go
  20. 19
      posts.go
  21. 4
      postsFuncs.go
  22. 2
      render.go
  23. 2
      robotstxt.go
  24. 2
      search.go
  25. 2
      shortDomain.go
  26. 34
      sitemap.go
  27. 2
      taxonomies.go
  28. 7
      utils.go
  29. 4
      webmentionAdmin.go

@ -409,7 +409,7 @@ func (a *goBlog) apNewID(blog *configBlog) (hash string, url string) {
}
func (a *goBlog) apIri(b *configBlog) string {
return a.cfg.Server.PublicAddress + b.Path
return a.getFullAddress(b.Path)
}
func apRequestIsSuccess(code int) bool {

@ -127,7 +127,7 @@ func (a *goBlog) toASNote(p *post) *asNote {
as.Tag = append(as.Tag, &asTag{
Type: "Hashtag",
Name: tag,
Href: a.cfg.Server.PublicAddress + a.cfg.Blogs[p.Blog].getRelativePath(fmt.Sprintf("/%s/%s", tagTax, urlize(tag))),
Href: a.getFullAddress(a.getRelativePath(p.Blog, fmt.Sprintf("/%s/%s", tagTax, urlize(tag)))),
})
}
}
@ -165,7 +165,7 @@ func (a *goBlog) serveActivityStreams(blog string, w http.ResponseWriter, r *htt
Name: b.Title,
Summary: b.Description,
PreferredUsername: blog,
Inbox: a.cfg.Server.PublicAddress + "/activitypub/inbox/" + blog,
Inbox: a.getFullAddress("/activitypub/inbox/" + blog),
PublicKey: &asPublicKey{
Owner: a.apIri(b),
ID: a.apIri(b) + "#main-key",

@ -1,13 +0,0 @@
package main
import "strings"
func (blog *configBlog) getRelativePath(path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if blog.Path != "/" {
return blog.Path + path
}
return path
}

@ -20,10 +20,11 @@ func (a *goBlog) initBlogStats() {
func (a *goBlog) serveBlogStats(w http.ResponseWriter, r *http.Request) {
blog := r.Context().Value(blogContextKey).(string)
canonical := a.blogPath(blog) + a.cfg.Blogs[blog].BlogStats.Path
bc := a.cfg.Blogs[blog]
canonical := bc.getRelativePath(bc.BlogStats.Path)
a.render(w, r, templateBlogStats, &renderData{
BlogString: blog,
Canonical: canonical,
Canonical: a.getFullAddress(canonical),
Data: map[string]interface{}{
"TableUrl": canonical + ".table.html",
},

@ -42,7 +42,7 @@ func (a *goBlog) serveComment(w http.ResponseWriter, r *http.Request) {
blog := r.Context().Value(blogContextKey).(string)
a.render(w, r, templateComment, &renderData{
BlogString: blog,
Canonical: a.cfg.Server.PublicAddress + a.cfg.Blogs[blog].getRelativePath(fmt.Sprintf("/comment/%d", id)),
Canonical: a.getFullAddress(a.cfg.Blogs[blog].getRelativePath(fmt.Sprintf("/comment/%d", id))),
Data: comment,
})
}
@ -75,9 +75,9 @@ func (a *goBlog) createComment(w http.ResponseWriter, r *http.Request) {
// Serve error
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
} else {
commentAddress := fmt.Sprintf("%s/%d", a.blogPath(r.Context().Value(blogContextKey).(string))+"/comment", commentID)
commentAddress := fmt.Sprintf("%s/%d", a.getRelativePath(r.Context().Value(blogContextKey).(string), "/comment"), commentID)
// Send webmention
_ = a.createWebmention(a.cfg.Server.PublicAddress+commentAddress, a.cfg.Server.PublicAddress+target)
_ = a.createWebmention(a.getFullAddress(commentAddress), a.getFullAddress(target))
// Redirect to comment
http.Redirect(w, r, commentAddress, http.StatusFound)
}

@ -77,8 +77,8 @@ func (a *goBlog) commentsAdmin(w http.ResponseWriter, r *http.Request) {
"Comments": comments,
"HasPrev": hasPrev,
"HasNext": hasNext,
"Prev": slashIfEmpty(prevPath),
"Next": slashIfEmpty(nextPath),
"Prev": prevPath,
"Next": nextPath,
},
})
}

@ -15,7 +15,7 @@ func (a *goBlog) serveCustomPage(w http.ResponseWriter, r *http.Request) {
}
a.render(w, r, page.Template, &renderData{
BlogString: r.Context().Value(blogContextKey).(string),
Canonical: a.cfg.Server.PublicAddress + page.Path,
Canonical: a.getFullAddress(page.Path),
Data: page.Data,
})
}

@ -33,7 +33,7 @@ func (a *goBlog) generateFeed(blog string, f feedType, w http.ResponseWriter, r
feed := &feeds.Feed{
Title: title,
Description: description,
Link: &feeds.Link{Href: a.cfg.Server.PublicAddress + strings.TrimSuffix(r.URL.Path, "."+string(f))},
Link: &feeds.Link{Href: a.getFullAddress(strings.TrimSuffix(r.URL.Path, "."+string(f)))},
Created: now,
Author: &feeds.Author{
Name: a.cfg.User.Name,

@ -61,7 +61,7 @@ require (
go.uber.org/atomic v1.8.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/net v0.0.0-20210525063256-abc453219eb5
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 // indirect
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf // indirect

@ -416,8 +416,9 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5 h1:wjuX4b5yYQnEQHzd+CBcrcC6OVR2J1CN6mUy0oSxIPo=
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b h1:k+E048sYJHyVnsr1GDrRZWQ32D2C7lWs9JRc0bel53A=
golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20170912212905-13449ad91cb2/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

@ -7,7 +7,7 @@ import (
)
func (a *goBlog) healthcheck() bool {
req, err := http.NewRequest(http.MethodGet, a.cfg.Server.PublicAddress+"/ping", nil)
req, err := http.NewRequest(http.MethodGet, a.getFullAddress("/ping"), nil)
if err != nil {
fmt.Println(err.Error())
return false

@ -247,11 +247,9 @@ func (a *goBlog) buildStaticHandlersRouters() error {
sbm := middleware.WithValue(blogContextKey, blog)
a.setBlogMiddlewares[blog] = sbm
blogPath := a.blogPath(blog)
for _, section := range blogConfig.Sections {
if section.Name != "" {
secPath := blogPath + "/" + section.Name
secPath := blogConfig.getRelativePath(section.Name)
a.sectionMiddlewares[secPath] = middleware.WithValue(indexConfigKey, &indexConfig{
path: secPath,
section: section,
@ -261,14 +259,14 @@ func (a *goBlog) buildStaticHandlersRouters() error {
for _, taxonomy := range blogConfig.Taxonomies {
if taxonomy.Name != "" {
taxPath := blogPath + "/" + taxonomy.Name
taxPath := blogConfig.getRelativePath(taxonomy.Name)
a.taxonomyMiddlewares[taxPath] = middleware.WithValue(taxonomyContextKey, taxonomy)
}
}
if blogConfig.Photos != nil && blogConfig.Photos.Enabled {
a.photosMiddlewares[blog] = middleware.WithValue(indexConfigKey, &indexConfig{
path: blogPath + blogConfig.Photos.Path,
path: blogConfig.getRelativePath(blogConfig.Photos.Path),
parameter: blogConfig.Photos.Parameter,
title: blogConfig.Photos.Title,
description: blogConfig.Photos.Description,
@ -277,7 +275,7 @@ func (a *goBlog) buildStaticHandlersRouters() error {
}
if blogConfig.Search != nil && blogConfig.Search.Enabled {
a.searchMiddlewares[blog] = middleware.WithValue(pathContextKey, blogPath+blogConfig.Search.Path)
a.searchMiddlewares[blog] = middleware.WithValue(pathContextKey, blogConfig.getRelativePath(blogConfig.Search.Path))
}
for _, cp := range blogConfig.CustomPages {
@ -285,7 +283,7 @@ func (a *goBlog) buildStaticHandlersRouters() error {
}
if commentsConfig := blogConfig.Comments; commentsConfig != nil && commentsConfig.Enabled {
a.commentsMiddlewares[blog] = middleware.WithValue(pathContextKey, blogPath+"/comment")
a.commentsMiddlewares[blog] = middleware.WithValue(pathContextKey, blogConfig.getRelativePath("/comment"))
}
}
@ -401,8 +399,6 @@ func (a *goBlog) buildDynamicRouter() (*chi.Mux, error) {
r.With(a.privateModeHandler...).With(a.cache.cacheMiddleware).Get("/s/{id:[0-9a-fA-F]+}", a.redirectToLongPath)
for blog, blogConfig := range a.cfg.Blogs {
blogPath := a.blogPath(blog)
sbm := a.setBlogMiddlewares[blog]
// Sections
@ -411,7 +407,7 @@ func (a *goBlog) buildDynamicRouter() (*chi.Mux, error) {
r.Use(a.cache.cacheMiddleware, sbm)
for _, section := range blogConfig.Sections {
if section.Name != "" {
secPath := blogPath + "/" + section.Name
secPath := blogConfig.getRelativePath(section.Name)
r.Group(func(r chi.Router) {
r.Use(a.sectionMiddlewares[secPath])
r.Get(secPath, a.serveIndex)
@ -425,7 +421,7 @@ func (a *goBlog) buildDynamicRouter() (*chi.Mux, error) {
// Taxonomies
for _, taxonomy := range blogConfig.Taxonomies {
if taxonomy.Name != "" {
taxPath := blogPath + "/" + taxonomy.Name
taxPath := blogConfig.getRelativePath(taxonomy.Name)
taxValues, err := a.db.allTaxonomyValues(blog, taxonomy.Name)
if err != nil {
return nil, err
@ -459,7 +455,7 @@ func (a *goBlog) buildDynamicRouter() (*chi.Mux, error) {
r.Group(func(r chi.Router) {
r.Use(a.privateModeHandler...)
r.Use(a.cache.cacheMiddleware, sbm, a.photosMiddlewares[blog])
photoPath := blogPath + blogConfig.Photos.Path
photoPath := blogConfig.getRelativePath(blogConfig.Photos.Path)
r.Get(photoPath, a.serveIndex)
r.Get(photoPath+feedPath, a.serveIndex)
r.Get(photoPath+paginationPath, a.serveIndex)
@ -468,13 +464,13 @@ func (a *goBlog) buildDynamicRouter() (*chi.Mux, error) {
// Search
if blogConfig.Search != nil && blogConfig.Search.Enabled {
searchPath := blogPath + blogConfig.Search.Path
searchPath := blogConfig.getRelativePath(blogConfig.Search.Path)
r.With(sbm, a.searchMiddlewares[blog]).Mount(searchPath, a.searchRouter)
}
// Stats
if blogConfig.BlogStats != nil && blogConfig.BlogStats.Enabled {
statsPath := blogPath + blogConfig.BlogStats.Path
statsPath := blogConfig.getRelativePath(blogConfig.BlogStats.Path)
r.Group(func(r chi.Router) {
r.Use(a.privateModeHandler...)
r.Use(a.cache.cacheMiddleware, sbm)
@ -492,7 +488,7 @@ func (a *goBlog) buildDynamicRouter() (*chi.Mux, error) {
monthRegex := `/{month:x|\d\d}`
dayRegex := `/{day:\d\d}`
yearPath := blogPath + yearRegex
yearPath := blogConfig.getRelativePath(yearRegex)
r.Get(yearPath, a.serveDate)
r.Get(yearPath+feedPath, a.serveDate)
r.Get(yearPath+paginationPath, a.serveDate)
@ -514,8 +510,8 @@ func (a *goBlog) buildDynamicRouter() (*chi.Mux, error) {
r.Use(a.privateModeHandler...)
r.Use(sbm)
r.With(a.checkActivityStreamsRequest, a.cache.cacheMiddleware).Get(blogConfig.Path, a.serveHome)
r.With(a.cache.cacheMiddleware).Get(blogConfig.Path+feedPath, a.serveHome)
r.With(a.cache.cacheMiddleware).Get(blogPath+paginationPath, a.serveHome)
r.With(a.cache.cacheMiddleware).Get(blogConfig.getRelativePath(feedPath), a.serveHome)
r.With(a.cache.cacheMiddleware).Get(blogConfig.getRelativePath(paginationPath), a.serveHome)
})
}
@ -535,21 +531,20 @@ func (a *goBlog) buildDynamicRouter() (*chi.Mux, error) {
if randomPath == "" {
randomPath = "/random"
}
r.With(a.privateModeHandler...).With(sbm).Get(blogPath+randomPath, a.redirectToRandomPost)
r.With(a.privateModeHandler...).With(sbm).Get(blogConfig.getRelativePath(randomPath), a.redirectToRandomPost)
}
// Editor
r.With(sbm).Mount(blogPath+"/editor", a.editorRouter)
r.With(sbm).Mount(blogConfig.getRelativePath("/editor"), a.editorRouter)
// Comments
if commentsConfig := blogConfig.Comments; commentsConfig != nil && commentsConfig.Enabled {
commentsPath := blogPath + "/comment"
r.With(sbm, a.commentsMiddlewares[blog]).Mount(commentsPath, a.commentsRouter)
r.With(sbm, a.commentsMiddlewares[blog]).Mount(blogConfig.getRelativePath("/comment"), a.commentsRouter)
}
// Blogroll
if brConfig := blogConfig.Blogroll; brConfig != nil && brConfig.Enabled {
brPath := blogPath + brConfig.Path
brPath := blogConfig.getRelativePath(brConfig.Path)
r.Group(func(r chi.Router) {
r.Use(a.privateModeHandler...)
r.Use(a.cache.cacheMiddleware, sbm)
@ -577,14 +572,6 @@ func (a *goBlog) buildDynamicRouter() (*chi.Mux, error) {
return r, nil
}
func (a *goBlog) blogPath(blog string) string {
blogPath := a.cfg.Blogs[blog].Path
if blogPath == "/" {
return ""
}
return blogPath
}
const blogContextKey requestContextKey = "blog"
const pathContextKey requestContextKey = "httpPath"

@ -28,7 +28,7 @@ func (a *goBlog) serveMicropubQuery(w http.ResponseWriter, r *http.Request) {
w.Header().Set(contentType, contentTypeJSONUTF8)
w.WriteHeader(http.StatusOK)
b, _ := json.Marshal(&micropubConfig{
MediaEndpoint: a.cfg.Server.PublicAddress + micropubPath + micropubMediaSubPath,
MediaEndpoint: a.getFullAddress(micropubPath + micropubMediaSubPath),
})
_, _ = writeMinified(w, contentTypeJSON, b)
case "source":

@ -113,7 +113,7 @@ func (a *goBlog) uploadFile(filename string, f io.Reader) (string, error) {
if ms != nil && ms.MediaURL != "" {
return ms.MediaURL + loc, nil
}
return a.cfg.Server.PublicAddress + loc, nil
return a.getFullAddress(loc), nil
}
func (config *configMicropubMedia) uploadToBunny(filename string, f io.Reader) (location string, err error) {

@ -9,7 +9,7 @@ func (a *goBlog) serveNodeInfoDiscover(w http.ResponseWriter, r *http.Request) {
b, _ := json.Marshal(map[string]interface{}{
"links": []map[string]interface{}{
{
"href": a.cfg.Server.PublicAddress + "/nodeinfo",
"href": a.getFullAddress("/nodeinfo"),
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
},
},

@ -158,8 +158,8 @@ func (a *goBlog) notificationsAdmin(w http.ResponseWriter, r *http.Request) {
"Notifications": notifications,
"HasPrev": hasPrev,
"HasNext": hasNext,
"Prev": slashIfEmpty(prevPath),
"Next": slashIfEmpty(nextPath),
"Prev": prevPath,
"Next": nextPath,
},
})
}

@ -9,7 +9,7 @@ func (a *goBlog) serveOpenSearch(w http.ResponseWriter, r *http.Request) {
blog := r.Context().Value(blogContextKey).(string)
b := a.cfg.Blogs[blog]
title := b.Title
sURL := a.cfg.Server.PublicAddress + b.getRelativePath(b.Search.Path)
sURL := a.getFullAddress(b.getRelativePath(b.Search.Path))
xml := fmt.Sprintf("<?xml version=\"1.0\"?><OpenSearchDescription xmlns=\"http://a9.com/-/spec/opensearch/1.1/\" xmlns:moz=\"http://www.mozilla.org/2006/browser/search/\">"+
"<ShortName>%s</ShortName><Description>%s</Description>"+
"<Url type=\"text/html\" method=\"post\" template=\"%s\"><Param name=\"q\" value=\"{searchTerms}\" /></Url>"+
@ -17,7 +17,7 @@ func (a *goBlog) serveOpenSearch(w http.ResponseWriter, r *http.Request) {
"</OpenSearchDescription>",
title, title, sURL, sURL)
w.Header().Set(contentType, "application/opensearchdescription+xml")
writeMinified(w, contentTypeXML, []byte(xml))
_, _ = writeMinified(w, contentTypeXML, []byte(xml))
}
func openSearchUrl(b *configBlog) string {

@ -0,0 +1,45 @@
package main
import "strings"
func (a *goBlog) getRelativePath(blog, path string) string {
// Get blog
bc := a.cfg.Blogs[blog]
if bc == nil {
return ""
}
// Get relative path
return bc.getRelativePath(path)
}
func (blog *configBlog) getRelativePath(path string) string {
// Check if path is absolute
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
// Check if blog uses subpath
if blog.Path != "" && blog.Path != "/" {
// Check if path is root
if path == "/" {
path = blog.Path
} else {
path = blog.Path + path
}
}
return path
}
func (a *goBlog) getFullAddress(path string) string {
// Call method with just the relevant config
return a.cfg.Server.getFullAddress(path)
}
func (cfg *configServer) getFullAddress(path string) string {
// Remove trailing slash
pa := strings.TrimSuffix(cfg.PublicAddress, "/")
// Check if path is root => blank path
if path == "/" {
path = ""
}
return pa + path
}

@ -0,0 +1,108 @@
package main
import (
"reflect"
"testing"
)
func Test_getFullAddress(t *testing.T) {
cfg1 := &configServer{
PublicAddress: "https://example.com",
}
cfg2 := &configServer{
PublicAddress: "https://example.com/",
}
app := &goBlog{
cfg: &config{
Server: cfg1,
},
}
if got := cfg1.getFullAddress("/test"); !reflect.DeepEqual(got, "https://example.com/test") {
t.Errorf("Wrong full path, got: %v", got)
}
if got := cfg2.getFullAddress("/test"); !reflect.DeepEqual(got, "https://example.com/test") {
t.Errorf("Wrong full path, got: %v", got)
}
if got := app.getFullAddress("/test"); !reflect.DeepEqual(got, "https://example.com/test") {
t.Errorf("Wrong full path, got: %v", got)
}
if got := cfg1.getFullAddress("/"); !reflect.DeepEqual(got, "https://example.com") {
t.Errorf("Wrong full path, got: %v", got)
}
if got := cfg1.getFullAddress(""); !reflect.DeepEqual(got, "https://example.com") {
t.Errorf("Wrong full path, got: %v", got)
}
}
func Test_getRelativeBlogPath(t *testing.T) {
blog1 := &configBlog{
Path: "",
}
blog2 := &configBlog{
Path: "",
}
blog3 := &configBlog{
Path: "/de",
}
if got := blog1.getRelativePath(""); !reflect.DeepEqual(got, "/") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
if got := blog2.getRelativePath(""); !reflect.DeepEqual(got, "/") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
if got := blog3.getRelativePath(""); !reflect.DeepEqual(got, "/de") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
if got := blog1.getRelativePath("test"); !reflect.DeepEqual(got, "/test") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
if got := blog2.getRelativePath("test"); !reflect.DeepEqual(got, "/test") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
if got := blog3.getRelativePath("test"); !reflect.DeepEqual(got, "/de/test") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
if got := blog1.getRelativePath("/test"); !reflect.DeepEqual(got, "/test") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
if got := blog2.getRelativePath("/test"); !reflect.DeepEqual(got, "/test") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
if got := blog3.getRelativePath("/test"); !reflect.DeepEqual(got, "/de/test") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
app := &goBlog{
cfg: &config{
Blogs: map[string]*configBlog{
"de": blog3,
},
},
}
if got := app.getRelativePath("de", "/test"); !reflect.DeepEqual(got, "/de/test") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
if got := app.getRelativePath("", "/test"); !reflect.DeepEqual(got, "") {
t.Errorf("Wrong relative blog path, got: %v", got)
}
}

@ -53,7 +53,7 @@ func (a *goBlog) servePost(w http.ResponseWriter, r *http.Request) {
return
}
if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
if r.URL.Path == a.blogPath(p.Blog) {
if r.URL.Path == a.getRelativePath(p.Blog, "") {
a.serveActivityStreams(p.Blog, w, r)
return
}
@ -116,7 +116,7 @@ func (a *goBlog) serveHome(w http.ResponseWriter, r *http.Request) {
return
}
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
path: a.blogPath(blog),
path: a.getRelativePath(blog, ""),
})))
}
@ -136,7 +136,6 @@ func (a *goBlog) serveDate(w http.ResponseWriter, r *http.Request) {
return
}
var title, dPath strings.Builder
dPath.WriteString(a.blogPath(r.Context().Value(blogContextKey).(string)) + "/")
if year != 0 {
ys := fmt.Sprintf("%0004d", year)
title.WriteString(ys)
@ -157,7 +156,7 @@ func (a *goBlog) serveDate(w http.ResponseWriter, r *http.Request) {
dPath.WriteString(fmt.Sprintf("/%02d", day))
}
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
path: dPath.String(),
path: a.getRelativePath(r.Context().Value(blogContextKey).(string), dPath.String()),
year: year,
month: month,
day: day,
@ -257,7 +256,7 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
if prevPage < 2 {
prevPath = path
} else {
prevPath = fmt.Sprintf("%s/page/%d", path, prevPage)
prevPath = fmt.Sprintf("%s/page/%d", strings.TrimSuffix(path, "/"), prevPage)
}
hasNext, _ = p.HasNext()
if hasNext {
@ -265,23 +264,23 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
} else {
nextPage, _ = p.Page()
}
nextPath = fmt.Sprintf("%s/page/%d", path, nextPage)
nextPath = fmt.Sprintf("%s/page/%d", strings.TrimSuffix(path, "/"), nextPage)
summaryTemplate := ic.summaryTemplate
if summaryTemplate == "" {
summaryTemplate = templateSummary
}
a.render(w, r, templateIndex, &renderData{
BlogString: blog,
Canonical: a.cfg.Server.PublicAddress + path,
Canonical: a.getFullAddress(path),
Data: map[string]interface{}{
"Title": title,
"Description": description,
"Posts": posts,
"HasPrev": hasPrev,
"HasNext": hasNext,
"First": slashIfEmpty(path),
"Prev": slashIfEmpty(prevPath),
"Next": slashIfEmpty(nextPath),
"First": path,
"Prev": prevPath,
"Next": nextPath,
"SummaryTemplate": summaryTemplate,
},
})

@ -9,7 +9,7 @@ import (
)
func (a *goBlog) fullPostURL(p *post) string {
return a.cfg.Server.PublicAddress + p.Path
return a.getFullAddress(p.Path)
}
func (a *goBlog) shortPostURL(p *post) string {
@ -20,7 +20,7 @@ func (a *goBlog) shortPostURL(p *post) string {
if a.cfg.Server.ShortPublicAddress != "" {
return a.cfg.Server.ShortPublicAddress + s
}
return a.cfg.Server.PublicAddress + s
return a.getFullAddress(s)
}
func (p *post) firstParameter(parameter string) (result string) {

@ -141,7 +141,7 @@ func (a *goBlog) initRendering() error {
"urlize": urlize,
"sort": sortedStrings,
"absolute": func(path string) string {
return a.cfg.Server.PublicAddress + path
return a.getFullAddress(path)
},
"blogrelative": func(blog *configBlog, path string) string {
return blog.getRelativePath(path)

@ -6,7 +6,7 @@ import (
)
func (a *goBlog) serveRobotsTXT(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(fmt.Sprintf("User-agent: *\nSitemap: %v", a.cfg.Server.PublicAddress+sitemapPath)))
_, _ = w.Write([]byte(fmt.Sprintf("User-agent: *\nSitemap: %v", a.getFullAddress(sitemapPath))))
}
func servePrivateRobotsTXT(w http.ResponseWriter, r *http.Request) {

@ -25,7 +25,7 @@ func (a *goBlog) serveSearch(w http.ResponseWriter, r *http.Request) {
}
a.render(w, r, templateSearch, &renderData{
BlogString: blog,
Canonical: a.cfg.Server.PublicAddress + servePath,
Canonical: a.getFullAddress(servePath),
})
}

@ -7,7 +7,7 @@ import (
func (a *goBlog) redirectShortDomain(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if a.cfg.Server.shortPublicHostname != "" && r.Host == a.cfg.Server.shortPublicHostname {
http.Redirect(rw, r, a.cfg.Server.PublicAddress+r.RequestURI, http.StatusMovedPermanently)
http.Redirect(rw, r, a.getFullAddress(r.RequestURI), http.StatusMovedPermanently)
return
}
next.ServeHTTP(rw, r)

@ -17,18 +17,14 @@ func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
// Blogs
for b, bc := range a.cfg.Blogs {
// Blog
blogPath := bc.Path
if blogPath == "/" {
blogPath = ""
}
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + blogPath,
Loc: a.getFullAddress(bc.Path),
})
// Sections
for _, section := range bc.Sections {
if section.Name != "" {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + bc.getRelativePath("/"+section.Name),
Loc: a.getFullAddress(bc.getRelativePath(section.Name)),
})
}
}
@ -38,13 +34,13 @@ func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
// Taxonomy
taxPath := bc.getRelativePath("/" + taxonomy.Name)
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + taxPath,
Loc: a.getFullAddress(taxPath),
})
// Values
if taxValues, err := a.db.allTaxonomyValues(b, taxonomy.Name); err == nil {
for _, tv := range taxValues {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + taxPath + "/" + urlize(tv),
Loc: a.getFullAddress(taxPath + "/" + urlize(tv)),
})
}
}
@ -58,7 +54,7 @@ func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
yearPath := bc.getRelativePath("/" + fmt.Sprintf("%0004d", d.year))
if !already[yearPath] {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + yearPath,
Loc: a.getFullAddress(yearPath),
})
already[yearPath] = true
}
@ -66,7 +62,7 @@ func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
monthPath := yearPath + "/" + fmt.Sprintf("%02d", d.month)
if !already[monthPath] {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + monthPath,
Loc: a.getFullAddress(monthPath),
})
already[monthPath] = true
}
@ -74,15 +70,15 @@ func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
dayPath := monthPath + "/" + fmt.Sprintf("%02d", d.day)
if !already[dayPath] {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + dayPath,
Loc: a.getFullAddress(dayPath),
})
already[dayPath] = true
}
// Generic month
genericMonthPath := blogPath + "/x/" + fmt.Sprintf("%02d", d.month)
genericMonthPath := bc.getRelativePath("/x/" + fmt.Sprintf("%02d", d.month))
if !already[genericMonthPath] {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + genericMonthPath,
Loc: a.getFullAddress(genericMonthPath),
})
already[genericMonthPath] = true
}
@ -90,7 +86,7 @@ func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
genericMonthDayPath := genericMonthPath + "/" + fmt.Sprintf("%02d", d.day)
if !already[genericMonthDayPath] {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + genericMonthDayPath,
Loc: a.getFullAddress(genericMonthDayPath),
})
already[genericMonthDayPath] = true
}
@ -99,31 +95,31 @@ func (a *goBlog) serveSitemap(w http.ResponseWriter, r *http.Request) {
// Photos
if bc.Photos != nil && bc.Photos.Enabled {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + bc.getRelativePath(bc.Photos.Path),
Loc: a.getFullAddress(bc.getRelativePath(bc.Photos.Path)),
})
}
// Search
if bc.Search != nil && bc.Search.Enabled {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + bc.getRelativePath(bc.Search.Path),
Loc: a.getFullAddress(bc.getRelativePath(bc.Search.Path)),
})
}
// Stats
if bc.BlogStats != nil && bc.BlogStats.Enabled {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + bc.getRelativePath(bc.BlogStats.Path),
Loc: a.getFullAddress(bc.getRelativePath(bc.BlogStats.Path)),
})
}
// Blogroll
if bc.Blogroll != nil && bc.Blogroll.Enabled {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + bc.getRelativePath(bc.Blogroll.Path),
Loc: a.getFullAddress(bc.getRelativePath(bc.Blogroll.Path)),
})
}
// Custom pages
for _, cp := range bc.CustomPages {
sm.Add(&sitemap.URL{
Loc: a.cfg.Server.PublicAddress + cp.Path,
Loc: a.getFullAddress(cp.Path),
})
}
}

@ -14,7 +14,7 @@ func (a *goBlog) serveTaxonomy(w http.ResponseWriter, r *http.Request) {
}
a.render(w, r, templateTaxonomy, &renderData{
BlogString: blog,
Canonical: a.cfg.Server.PublicAddress + r.URL.Path,
Canonical: a.getFullAddress(r.URL.Path),
Data: map[string]interface{}{
"Taxonomy": tax,
"ValueGroups": groupStrings(allValues),

@ -106,13 +106,6 @@ func unescapedPath(p string) string {
return p
}
func slashIfEmpty(s string) string {
if s == "" {
return "/"
}
return s
}
type stringGroup struct {
Identifier string
Strings []string

@ -97,8 +97,8 @@ func (a *goBlog) webmentionAdmin(w http.ResponseWriter, r *http.Request) {
"Mentions": mentions,
"HasPrev": hasPrev,
"HasNext": hasNext,
"Prev": slashIfEmpty(prevPath) + query,
"Next": slashIfEmpty(nextPath) + query,
"Prev": prevPath + query,
"Next": nextPath + query,
},
})
}

Loading…
Cancel
Save