repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
f3f69ff
parent
a2181c3
author
Eric Bower
date
2022-09-02 17:11:22 +0000 UTC
fix: use host domain for full urls
7 files changed,  +159, -167
M filehandlers/imgs/handler.go
+2, -2
 1@@ -229,11 +229,11 @@ func (h *UploadImgHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
 2 		return "", err
 3 	}
 4 
 5+	curl := shared.NewCreateURL(h.Cfg)
 6 	url := h.Cfg.FullPostURL(
 7+		curl,
 8 		h.User.Name,
 9 		metadata.Slug,
10-		h.Cfg.IsSubdomains(),
11-		true,
12 	)
13 	return url, nil
14 }
M filehandlers/post_handler.go
+4, -2
 1@@ -257,7 +257,8 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
 2 	} else {
 3 		if metadata.Text == post.Text {
 4 			logger.Infof("(%s) found, but text is identical, skipping", filename)
 5-			return h.Cfg.FullPostURL(h.User.Name, metadata.Slug, h.Cfg.IsSubdomains(), true), nil
 6+			curl := shared.NewCreateURL(h.Cfg)
 7+			return h.Cfg.FullPostURL(curl, h.User.Name, metadata.Slug), nil
 8 		}
 9 
10 		logger.Infof("(%s) found, updating record", filename)
11@@ -290,5 +291,6 @@ func (h *ScpUploadHandler) Write(s ssh.Session, entry *utils.FileEntry) (string,
12 		}
13 	}
14 
15-	return h.Cfg.FullPostURL(h.User.Name, metadata.Slug, h.Cfg.IsSubdomains(), true), nil
16+	curl := shared.NewCreateURL(h.Cfg)
17+	return h.Cfg.FullPostURL(curl, h.User.Name, metadata.Slug), nil
18 }
M imgs/api.go
+20, -37
  1@@ -129,12 +129,6 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
  2 		return
  3 	}
  4 
  5-	hostDomain := strings.Split(r.Host, ":")[0]
  6-	appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
  7-
  8-	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
  9-	withUserName := (!onSubdomain && hostDomain == appDomain) || !cfg.IsCustomdomains()
 10-
 11 	ts, err := shared.RenderTemplate(cfg, []string{
 12 		cfg.StaticPath("html/blog.page.tmpl"),
 13 	})
 14@@ -151,15 +145,16 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
 15 	}
 16 	readmeTxt := &ReadmeTxt{}
 17 
 18+	curl := shared.CreateURLFromRequest(cfg, r)
 19 	postCollection := make([]*PostItemData, 0, len(posts))
 20 	for _, post := range posts {
 21 		url := fmt.Sprintf(
 22 			"%s/300x",
 23-			cfg.ImgURL(post.Username, post.Slug, onSubdomain, withUserName),
 24+			cfg.ImgURL(curl, post.Username, post.Slug),
 25 		)
 26 		postCollection = append(postCollection, &PostItemData{
 27 			ImgURL:       template.URL(url),
 28-			URL:          template.URL(cfg.ImgPostURL(post.Username, post.Slug, onSubdomain, withUserName)),
 29+			URL:          template.URL(cfg.ImgPostURL(curl, post.Username, post.Slug)),
 30 			Caption:      post.Title,
 31 			PublishAt:    post.PublishAt.Format("02 Jan, 2006"),
 32 			PublishAtISO: post.PublishAt.Format(time.RFC3339),
 33@@ -169,8 +164,8 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
 34 	data := BlogPageData{
 35 		Site:      *cfg.GetSiteData(),
 36 		PageTitle: headerTxt.Title,
 37-		URL:       template.URL(cfg.FullBlogURL(username, onSubdomain, withUserName)),
 38-		RSSURL:    template.URL(cfg.RssBlogURL(username, onSubdomain, withUserName, tag)),
 39+		URL:       template.URL(cfg.FullBlogURL(curl, username)),
 40+		RSSURL:    template.URL(cfg.RssBlogURL(curl, username, tag)),
 41 		Readme:    readmeTxt,
 42 		Header:    headerTxt,
 43 		Username:  username,
 44@@ -333,11 +328,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 45 	}
 46 
 47 	blogName := GetBlogName(username)
 48-	hostDomain := strings.Split(r.Host, ":")[0]
 49-	appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
 50-
 51-	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
 52-	withUserName := (!onSubdomain && hostDomain == appDomain) || !cfg.IsCustomdomains()
 53+	curl := shared.CreateURLFromRequest(cfg, r)
 54 
 55 	var data PostPageData
 56 	post, err := dbpool.FindPostWithSlug(slug, user.ID, cfg.Space)
 57@@ -355,7 +346,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 58 		tagLinks := make([]Link, 0, len(post.Tags))
 59 		for _, tag := range post.Tags {
 60 			tagLinks = append(tagLinks, Link{
 61-				URL:  template.URL(cfg.TagURL(username, tag, onSubdomain, withUserName)),
 62+				URL:  template.URL(cfg.TagURL(curl, username, tag)),
 63 				Text: tag,
 64 			})
 65 		}
 66@@ -363,8 +354,8 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 67 		data = PostPageData{
 68 			Site:         *cfg.GetSiteData(),
 69 			PageTitle:    GetPostTitle(post),
 70-			URL:          template.URL(cfg.FullPostURL(post.Username, post.Slug, onSubdomain, withUserName)),
 71-			BlogURL:      template.URL(cfg.FullBlogURL(username, onSubdomain, withUserName)),
 72+			URL:          template.URL(cfg.FullPostURL(curl, post.Username, post.Slug)),
 73+			BlogURL:      template.URL(cfg.FullBlogURL(curl, username)),
 74 			Caption:      post.Description,
 75 			Title:        post.Title,
 76 			Slug:         post.Slug,
 77@@ -373,13 +364,13 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 78 			Username:     username,
 79 			BlogName:     blogName,
 80 			Contents:     template.HTML(text),
 81-			ImgURL:       template.URL(cfg.ImgURL(username, post.Slug, onSubdomain, withUserName)),
 82+			ImgURL:       template.URL(cfg.ImgURL(curl, username, post.Slug)),
 83 			Tags:         tagLinks,
 84 		}
 85 	} else {
 86 		data = PostPageData{
 87 			Site:         *cfg.GetSiteData(),
 88-			BlogURL:      template.URL(cfg.FullBlogURL(username, onSubdomain, withUserName)),
 89+			BlogURL:      template.URL(cfg.FullBlogURL(curl, username)),
 90 			PageTitle:    "Post not found",
 91 			Caption:      "Post not found",
 92 			Title:        "Post not found",
 93@@ -481,15 +472,11 @@ func rssBlogHandler(w http.ResponseWriter, r *http.Request) {
 94 		Title: GetBlogName(username),
 95 	}
 96 
 97-	hostDomain := strings.Split(r.Host, ":")[0]
 98-	appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
 99-
100-	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
101-	withUserName := (!onSubdomain && hostDomain == appDomain) || !cfg.IsCustomdomains()
102+	curl := shared.CreateURLFromRequest(cfg, r)
103 
104 	feed := &feeds.Feed{
105 		Title:       headerTxt.Title,
106-		Link:        &feeds.Link{Href: cfg.FullBlogURL(username, onSubdomain, withUserName)},
107+		Link:        &feeds.Link{Href: cfg.FullBlogURL(curl, username)},
108 		Description: headerTxt.Bio,
109 		Author:      &feeds.Author{Name: username},
110 		Created:     time.Now(),
111@@ -502,14 +489,14 @@ func rssBlogHandler(w http.ResponseWriter, r *http.Request) {
112 		}
113 		var tpl bytes.Buffer
114 		data := &PostPageData{
115-			ImgURL: template.URL(cfg.ImgURL(username, post.Slug, onSubdomain, withUserName)),
116+			ImgURL: template.URL(cfg.ImgURL(curl, username, post.Slug)),
117 		}
118 		if err := ts.Execute(&tpl, data); err != nil {
119 			continue
120 		}
121 
122-		realUrl := cfg.FullPostURL(post.Username, post.Slug, onSubdomain, withUserName)
123-		if !onSubdomain && !withUserName {
124+		realUrl := cfg.FullPostURL(curl, post.Username, post.Slug)
125+		if !curl.Subdomain && !curl.UsernameInRoute {
126 			realUrl = fmt.Sprintf("%s://%s%s", cfg.Protocol, r.Host, realUrl)
127 		}
128 
129@@ -571,24 +558,20 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
130 		Created:     time.Now(),
131 	}
132 
133-	hostDomain := strings.Split(r.Host, ":")[0]
134-	appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
135-
136-	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
137-	withUserName := (!onSubdomain && hostDomain == appDomain) || !cfg.IsCustomdomains()
138+	curl := shared.CreateURLFromRequest(cfg, r)
139 
140 	var feedItems []*feeds.Item
141 	for _, post := range pager.Data {
142 		var tpl bytes.Buffer
143 		data := &PostPageData{
144-			ImgURL: template.URL(cfg.ImgURL(post.Username, post.Slug, onSubdomain, withUserName)),
145+			ImgURL: template.URL(cfg.ImgURL(curl, post.Username, post.Slug)),
146 		}
147 		if err := ts.Execute(&tpl, data); err != nil {
148 			continue
149 		}
150 
151-		realUrl := cfg.FullPostURL(post.Username, post.Slug, onSubdomain, withUserName)
152-		if !onSubdomain && !withUserName {
153+		realUrl := cfg.FullPostURL(curl, post.Username, post.Slug)
154+		if !curl.Subdomain && !curl.UsernameInRoute {
155 			realUrl = fmt.Sprintf("%s://%s%s", cfg.Protocol, r.Host, realUrl)
156 		}
157 
M lists/api.go
+5, -10
 1@@ -8,7 +8,6 @@ import (
 2 	"net/url"
 3 	"sort"
 4 	"strconv"
 5-	"strings"
 6 	"time"
 7 
 8 	"git.sr.ht/~erock/pico/db"
 9@@ -138,11 +137,7 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
10 		return
11 	}
12 
13-	hostDomain := strings.Split(r.Host, ":")[0]
14-	appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
15-
16-	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
17-	withUserName := (!onSubdomain && hostDomain == appDomain) || !cfg.IsCustomdomains()
18+	curl := shared.CreateURLFromRequest(cfg, r)
19 
20 	ts, err := shared.RenderTemplate(cfg, []string{
21 		cfg.StaticPath("html/blog.page.tmpl"),
22@@ -190,8 +185,8 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
23 	postCollection := make([]PostItemData, 0, len(posts))
24 	for _, post := range posts {
25 		p := PostItemData{
26-			URL:            template.URL(cfg.FullPostURL(post.Username, post.Slug, onSubdomain, withUserName)),
27-			BlogURL:        template.URL(cfg.FullBlogURL(post.Username, onSubdomain, withUserName)),
28+			URL:            template.URL(cfg.FullPostURL(curl, post.Username, post.Slug)),
29+			BlogURL:        template.URL(cfg.FullBlogURL(curl, post.Username)),
30 			Title:          shared.FilenameToTitle(post.Filename, post.Title),
31 			PublishAt:      post.PublishAt.Format("02 Jan, 2006"),
32 			PublishAtISO:   post.PublishAt.Format(time.RFC3339),
33@@ -204,8 +199,8 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
34 	data := BlogPageData{
35 		Site:      *cfg.GetSiteData(),
36 		PageTitle: headerTxt.Title,
37-		URL:       template.URL(cfg.FullBlogURL(username, onSubdomain, withUserName)),
38-		RSSURL:    template.URL(cfg.RssBlogURL(username, onSubdomain, withUserName, tag)),
39+		URL:       template.URL(cfg.FullBlogURL(curl, username)),
40+		RSSURL:    template.URL(cfg.RssBlogURL(curl, username, tag)),
41 		Readme:    readmeTxt,
42 		Header:    headerTxt,
43 		Username:  username,
M pastes/api.go
+5, -11
 1@@ -6,7 +6,6 @@ import (
 2 	"net/http"
 3 	"net/url"
 4 	"os"
 5-	"strings"
 6 	"time"
 7 
 8 	"git.sr.ht/~erock/pico/db"
 9@@ -95,12 +94,6 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
10 		return
11 	}
12 
13-	hostDomain := strings.Split(r.Host, ":")[0]
14-	appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
15-
16-	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
17-	withUserName := (!onSubdomain && hostDomain == appDomain) || !cfg.IsCustomdomains()
18-
19 	ts, err := shared.RenderTemplate(cfg, []string{
20 		cfg.StaticPath("html/blog.page.tmpl"),
21 	})
22@@ -116,11 +109,12 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
23 		Bio:   "",
24 	}
25 
26+	curl := shared.CreateURLFromRequest(cfg, r)
27 	postCollection := make([]PostItemData, 0, len(posts))
28 	for _, post := range posts {
29 		p := PostItemData{
30-			URL:            template.URL(cfg.FullPostURL(post.Username, post.Slug, onSubdomain, withUserName)),
31-			BlogURL:        template.URL(cfg.FullBlogURL(post.Username, onSubdomain, withUserName)),
32+			URL:            template.URL(cfg.FullPostURL(curl, post.Username, post.Slug)),
33+			BlogURL:        template.URL(cfg.FullBlogURL(curl, post.Username)),
34 			Title:          post.Filename,
35 			PublishAt:      post.PublishAt.Format("02 Jan, 2006"),
36 			PublishAtISO:   post.PublishAt.Format(time.RFC3339),
37@@ -133,8 +127,8 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
38 	data := BlogPageData{
39 		Site:      *cfg.GetSiteData(),
40 		PageTitle: headerTxt.Title,
41-		URL:       template.URL(cfg.FullBlogURL(username, onSubdomain, withUserName)),
42-		RSSURL:    template.URL(cfg.RssBlogURL(username, onSubdomain, withUserName, "")),
43+		URL:       template.URL(cfg.FullBlogURL(curl, username)),
44+		RSSURL:    template.URL(cfg.RssBlogURL(curl, username, "")),
45 		Header:    headerTxt,
46 		Username:  username,
47 		Posts:     postCollection,
M prose/api.go
+21, -37
  1@@ -8,7 +8,6 @@ import (
  2 	"net/url"
  3 	"os"
  4 	"strconv"
  5-	"strings"
  6 	"time"
  7 
  8 	"git.sr.ht/~erock/pico/db"
  9@@ -182,11 +181,7 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
 10 		cfg.StaticPath("html/blog.page.tmpl"),
 11 	})
 12 
 13-	hostDomain := strings.Split(r.Host, ":")[0]
 14-	appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
 15-
 16-	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
 17-	withUserName := (!onSubdomain && hostDomain == appDomain) || !cfg.IsCustomdomains()
 18+	curl := shared.CreateURLFromRequest(cfg, r)
 19 
 20 	if err != nil {
 21 		logger.Error(err)
 22@@ -223,10 +218,9 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
 23 			finURL := nav.URL
 24 			if !u.IsAbs() {
 25 				finURL = cfg.FullPostURL(
 26+					curl,
 27 					readme.Username,
 28 					nav.URL,
 29-					onSubdomain,
 30-					withUserName,
 31 				)
 32 			}
 33 			headerTxt.Nav = append(headerTxt.Nav, shared.Link{
 34@@ -250,8 +244,8 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
 35 	postCollection := make([]PostItemData, 0, len(posts))
 36 	for _, post := range posts {
 37 		p := PostItemData{
 38-			URL:            template.URL(cfg.FullPostURL(post.Username, post.Slug, onSubdomain, withUserName)),
 39-			BlogURL:        template.URL(cfg.FullBlogURL(post.Username, onSubdomain, withUserName)),
 40+			URL:            template.URL(cfg.FullPostURL(curl, post.Username, post.Slug)),
 41+			BlogURL:        template.URL(cfg.FullBlogURL(curl, post.Username)),
 42 			Title:          shared.FilenameToTitle(post.Filename, post.Title),
 43 			PublishAt:      post.PublishAt.Format("02 Jan, 2006"),
 44 			PublishAtISO:   post.PublishAt.Format(time.RFC3339),
 45@@ -264,8 +258,8 @@ func blogHandler(w http.ResponseWriter, r *http.Request) {
 46 	data := BlogPageData{
 47 		Site:      *cfg.GetSiteData(),
 48 		PageTitle: headerTxt.Title,
 49-		URL:       template.URL(cfg.FullBlogURL(username, onSubdomain, withUserName)),
 50-		RSSURL:    template.URL(cfg.RssBlogURL(username, onSubdomain, withUserName, tag)),
 51+		URL:       template.URL(cfg.FullBlogURL(curl, username)),
 52+		RSSURL:    template.URL(cfg.RssBlogURL(curl, username, tag)),
 53 		Readme:    readmeTxt,
 54 		Header:    headerTxt,
 55 		Username:  username,
 56@@ -343,11 +337,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 57 	}
 58 
 59 	blogName := GetBlogName(username)
 60-	hostDomain := strings.Split(r.Host, ":")[0]
 61-	appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
 62-
 63-	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
 64-	withUserName := (!onSubdomain && hostDomain == appDomain) || !cfg.IsCustomdomains()
 65+	curl := shared.CreateURLFromRequest(cfg, r)
 66 
 67 	ogImage := ""
 68 	ogImageCard := ""
 69@@ -411,8 +401,8 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 70 		data = PostPageData{
 71 			Site:         *cfg.GetSiteData(),
 72 			PageTitle:    GetPostTitle(post),
 73-			URL:          template.URL(cfg.FullPostURL(post.Username, post.Slug, onSubdomain, withUserName)),
 74-			BlogURL:      template.URL(cfg.FullBlogURL(username, onSubdomain, withUserName)),
 75+			URL:          template.URL(cfg.FullPostURL(curl, post.Username, post.Slug)),
 76+			BlogURL:      template.URL(cfg.FullBlogURL(curl, username)),
 77 			Description:  post.Description,
 78 			Title:        shared.FilenameToTitle(post.Filename, post.Title),
 79 			Slug:         post.Slug,
 80@@ -431,7 +421,7 @@ func postHandler(w http.ResponseWriter, r *http.Request) {
 81 	} else {
 82 		data = PostPageData{
 83 			Site:         *cfg.GetSiteData(),
 84-			BlogURL:      template.URL(cfg.FullBlogURL(username, onSubdomain, withUserName)),
 85+			BlogURL:      template.URL(cfg.FullBlogURL(curl, username)),
 86 			PageTitle:    "Post not found",
 87 			Description:  "Post not found",
 88 			Title:        "Post not found",
 89@@ -551,10 +541,12 @@ func readHandler(w http.ResponseWriter, r *http.Request) {
 90 		Tags:      tags,
 91 		HasFilter: tag != "",
 92 	}
 93+
 94+	curl := shared.NewCreateURL(cfg)
 95 	for _, post := range pager.Data {
 96 		item := PostItemData{
 97-			URL:            template.URL(cfg.FullPostURL(post.Username, post.Slug, true, true)),
 98-			BlogURL:        template.URL(cfg.FullBlogURL(post.Username, true, true)),
 99+			URL:            template.URL(cfg.FullPostURL(curl, post.Username, post.Slug)),
100+			BlogURL:        template.URL(cfg.FullBlogURL(curl, post.Username)),
101 			Title:          shared.FilenameToTitle(post.Filename, post.Title),
102 			Description:    post.Description,
103 			Username:       post.Username,
104@@ -631,15 +623,11 @@ func rssBlogHandler(w http.ResponseWriter, r *http.Request) {
105 		}
106 	}
107 
108-	hostDomain := strings.Split(r.Host, ":")[0]
109-	appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
110-
111-	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
112-	withUserName := (!onSubdomain && hostDomain == appDomain) || !cfg.IsCustomdomains()
113+	curl := shared.CreateURLFromRequest(cfg, r)
114 
115 	feed := &feeds.Feed{
116 		Title:       headerTxt.Title,
117-		Link:        &feeds.Link{Href: cfg.FullBlogURL(username, onSubdomain, withUserName)},
118+		Link:        &feeds.Link{Href: cfg.FullBlogURL(curl, username)},
119 		Description: headerTxt.Bio,
120 		Author:      &feeds.Author{Name: username},
121 		Created:     time.Now(),
122@@ -674,8 +662,8 @@ func rssBlogHandler(w http.ResponseWriter, r *http.Request) {
123 			continue
124 		}
125 
126-		realUrl := cfg.FullPostURL(post.Username, post.Slug, onSubdomain, withUserName)
127-		if !onSubdomain && !withUserName {
128+		realUrl := cfg.FullPostURL(curl, post.Username, post.Slug)
129+		if !curl.Subdomain && !curl.UsernameInRoute {
130 			realUrl = fmt.Sprintf("%s://%s%s", cfg.Protocol, r.Host, realUrl)
131 		}
132 
133@@ -737,11 +725,7 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
134 		Created:     time.Now(),
135 	}
136 
137-	hostDomain := strings.Split(r.Host, ":")[0]
138-	appDomain := strings.Split(cfg.ConfigCms.Domain, ":")[0]
139-
140-	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
141-	withUserName := (!onSubdomain && hostDomain == appDomain) || !cfg.IsCustomdomains()
142+	curl := shared.CreateURLFromRequest(cfg, r)
143 
144 	var feedItems []*feeds.Item
145 	for _, post := range pager.Data {
146@@ -759,8 +743,8 @@ func rssHandler(w http.ResponseWriter, r *http.Request) {
147 			continue
148 		}
149 
150-		realUrl := cfg.FullPostURL(post.Username, post.Slug, onSubdomain, withUserName)
151-		if !onSubdomain && !withUserName {
152+		realUrl := cfg.FullPostURL(curl, post.Username, post.Slug)
153+		if !curl.Subdomain && !curl.UsernameInRoute {
154 			realUrl = fmt.Sprintf("%s://%s%s", cfg.Protocol, r.Host, realUrl)
155 		}
156 
M shared/config.go
+102, -68
  1@@ -4,6 +4,7 @@ import (
  2 	"fmt"
  3 	"html/template"
  4 	"log"
  5+	"net/http"
  6 	"net/url"
  7 	"path"
  8 	"strings"
  9@@ -30,55 +31,44 @@ type ConfigSite struct {
 10 	CustomdomainsEnabled bool
 11 }
 12 
 13-func (c *ConfigSite) GetSiteData() *SitePageData {
 14-	return &SitePageData{
 15-		Domain:  template.URL(c.Domain),
 16-		HomeURL: template.URL(c.HomeURL()),
 17-		Email:   c.Email,
 18-	}
 19+type CreateURL struct {
 20+	Subdomain       bool
 21+	UsernameInRoute bool
 22+	HostDomain      string
 23+	AppDomain       string
 24+	Username        string
 25+	Cfg             *ConfigSite
 26 }
 27 
 28-func (c *ConfigSite) BlogURL(username string) string {
 29-	if c.IsSubdomains() {
 30-		return fmt.Sprintf("%s://%s.%s", c.Protocol, username, c.Domain)
 31+func NewCreateURL(cfg *ConfigSite) *CreateURL {
 32+	return &CreateURL{
 33+		Cfg:       cfg,
 34+		Subdomain: cfg.IsSubdomains(),
 35 	}
 36-
 37-	return fmt.Sprintf("/%s", username)
 38 }
 39 
 40-func (c *ConfigSite) FullBlogURL(username string, onSubdomain bool, withUserName bool) string {
 41-	if c.IsSubdomains() && onSubdomain {
 42-		return fmt.Sprintf("%s://%s.%s", c.Protocol, username, c.Domain)
 43-	}
 44-
 45-	if withUserName {
 46-		return fmt.Sprintf("/%s", username)
 47-	}
 48+func CreateURLFromRequest(cfg *ConfigSite, r *http.Request) *CreateURL {
 49+	hostDomain := strings.Split(r.Host, ":")[0]
 50+	appDomain := strings.Split(cfg.Domain, ":")[0]
 51 
 52-	return "/"
 53-}
 54+	onSubdomain := cfg.IsSubdomains() && strings.Contains(hostDomain, appDomain)
 55+	withUserName := !cfg.IsCustomdomains() || (!onSubdomain && hostDomain == appDomain)
 56 
 57-func (c *ConfigSite) PostURL(username, slug string) string {
 58-	fname := url.PathEscape(slug)
 59-	if c.IsSubdomains() {
 60-		return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, fname)
 61+	return &CreateURL{
 62+		Cfg:             cfg,
 63+		AppDomain:       appDomain,
 64+		HostDomain:      hostDomain,
 65+		Subdomain:       onSubdomain,
 66+		UsernameInRoute: withUserName,
 67 	}
 68-
 69-	return fmt.Sprintf("/%s/%s", username, fname)
 70-
 71 }
 72 
 73-func (c *ConfigSite) FullPostURL(username, slug string, onSubdomain bool, withUserName bool) string {
 74-	fname := url.PathEscape(strings.TrimLeft(slug, "/"))
 75-	if c.IsSubdomains() && onSubdomain {
 76-		return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, fname)
 77-	}
 78-
 79-	if withUserName {
 80-		return fmt.Sprintf("/%s/%s", username, fname)
 81+func (c *ConfigSite) GetSiteData() *SitePageData {
 82+	return &SitePageData{
 83+		Domain:  template.URL(c.Domain),
 84+		HomeURL: template.URL(c.HomeURL()),
 85+		Email:   c.Email,
 86 	}
 87-
 88-	return fmt.Sprintf("/%s", fname)
 89 }
 90 
 91 func (c *ConfigSite) IsSubdomains() bool {
 92@@ -89,23 +79,6 @@ func (c *ConfigSite) IsCustomdomains() bool {
 93 	return c.CustomdomainsEnabled
 94 }
 95 
 96-func (c *ConfigSite) RssBlogURL(username string, onSubdomain bool, withUserName bool, tag string) string {
 97-	url := ""
 98-	if c.IsSubdomains() && onSubdomain {
 99-		url = fmt.Sprintf("%s://%s.%s/rss", c.Protocol, username, c.Domain)
100-	} else if withUserName {
101-		url = fmt.Sprintf("/%s/rss", username)
102-	} else {
103-		url = "/rss"
104-	}
105-
106-	if tag != "" {
107-		return fmt.Sprintf("%s?tag=%s", url, tag)
108-	}
109-
110-	return url
111-}
112-
113 func (c *ConfigSite) HomeURL() string {
114 	if c.IsSubdomains() || c.IsCustomdomains() {
115 		return fmt.Sprintf("//%s", c.Domain)
116@@ -122,6 +95,18 @@ func (c *ConfigSite) ReadURL() string {
117 	return "/read"
118 }
119 
120+func (c *ConfigSite) StaticPath(fname string) string {
121+	return path.Join(c.Space, fname)
122+}
123+
124+func (c *ConfigSite) BlogURL(username string) string {
125+	if c.IsSubdomains() {
126+		return fmt.Sprintf("%s://%s.%s", c.Protocol, username, c.Domain)
127+	}
128+
129+	return fmt.Sprintf("/%s", username)
130+}
131+
132 func (c *ConfigSite) CssURL(username string) string {
133 	if c.IsSubdomains() || c.IsCustomdomains() {
134 		return fmt.Sprintf("%s://%s.%s/_styles.css", c.Protocol, username, c.Domain)
135@@ -130,8 +115,14 @@ func (c *ConfigSite) CssURL(username string) string {
136 	return fmt.Sprintf("/%s/styles.css", username)
137 }
138 
139-func (c *ConfigSite) StaticPath(fname string) string {
140-	return path.Join(c.Space, fname)
141+func (c *ConfigSite) PostURL(username, slug string) string {
142+	fname := url.PathEscape(slug)
143+	if c.IsSubdomains() {
144+		return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, fname)
145+	}
146+
147+	return fmt.Sprintf("/%s/%s", username, fname)
148+
149 }
150 
151 func (c *ConfigSite) RawPostURL(username, slug string) string {
152@@ -148,48 +139,91 @@ func (c *ConfigSite) ImgFullURL(username, slug string) string {
153 	return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, fname)
154 }
155 
156-func (c *ConfigSite) ImgURL(username string, slug string, onSubdomain bool, withUserName bool) string {
157+func (c *ConfigSite) FullBlogURL(curl *CreateURL, username string) string {
158+	if c.IsSubdomains() && curl.Subdomain {
159+		return fmt.Sprintf("%s://%s.%s", c.Protocol, username, c.Domain)
160+	}
161+
162+	if curl.UsernameInRoute {
163+		return fmt.Sprintf("/%s", username)
164+	}
165+
166+	return fmt.Sprintf("%s://%s", c.Protocol, curl.HostDomain)
167+}
168+
169+func (c *ConfigSite) FullPostURL(curl *CreateURL, username, slug string) string {
170+	fname := url.PathEscape(strings.TrimLeft(slug, "/"))
171+
172+	if curl.Subdomain && c.IsSubdomains() {
173+		return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, fname)
174+	}
175+
176+	if curl.UsernameInRoute {
177+		return fmt.Sprintf("%s://%s/%s/%s", c.Protocol, c.Domain, username, fname)
178+	}
179+
180+	return fmt.Sprintf("%s://%s/%s", c.Protocol, curl.HostDomain, fname)
181+}
182+
183+func (c *ConfigSite) RssBlogURL(curl *CreateURL, username, tag string) string {
184+	url := ""
185+	if c.IsSubdomains() && curl.Subdomain {
186+		url = fmt.Sprintf("%s://%s.%s/rss", c.Protocol, username, c.Domain)
187+	} else if curl.UsernameInRoute {
188+		url = fmt.Sprintf("/%s/rss", username)
189+	} else {
190+		url = "/rss"
191+	}
192+
193+	if tag != "" {
194+		return fmt.Sprintf("%s?tag=%s", url, tag)
195+	}
196+
197+	return url
198+}
199+
200+func (c *ConfigSite) ImgURL(curl *CreateURL, username string, slug string) string {
201 	fname := url.PathEscape(strings.TrimLeft(slug, "/"))
202-	if c.IsSubdomains() && onSubdomain {
203+	if c.IsSubdomains() && curl.Subdomain {
204 		return fmt.Sprintf("%s://%s.%s/%s", c.Protocol, username, c.Domain, fname)
205 	}
206 
207-	if withUserName {
208+	if curl.UsernameInRoute {
209 		return fmt.Sprintf("/%s/%s", username, fname)
210 	}
211 
212 	return fmt.Sprintf("/%s", fname)
213 }
214 
215-func (c *ConfigSite) ImgPostURL(username string, slug string, onSubdomain bool, withUserName bool) string {
216+func (c *ConfigSite) ImgPostURL(curl *CreateURL, username string, slug string) string {
217 	fname := url.PathEscape(strings.TrimLeft(slug, "/"))
218-	if c.IsSubdomains() && onSubdomain {
219+	if c.IsSubdomains() && curl.Subdomain {
220 		return fmt.Sprintf("%s://%s.%s/p/%s", c.Protocol, username, c.Domain, fname)
221 	}
222 
223-	if withUserName {
224+	if curl.UsernameInRoute {
225 		return fmt.Sprintf("/%s/p/%s", username, fname)
226 	}
227 
228 	return fmt.Sprintf("/p/%s", fname)
229 }
230 
231-func (c *ConfigSite) ImgOrigURL(username string, slug string, onSubdomain bool, withUserName bool) string {
232+func (c *ConfigSite) ImgOrigURL(curl *CreateURL, username string, slug string) string {
233 	fname := url.PathEscape(strings.TrimLeft(slug, "/"))
234-	if c.IsSubdomains() && onSubdomain {
235+	if c.IsSubdomains() && curl.Subdomain {
236 		return fmt.Sprintf("%s://%s.%s/o/%s", c.Protocol, username, c.Domain, fname)
237 	}
238 
239-	if withUserName {
240+	if curl.UsernameInRoute {
241 		return fmt.Sprintf("/%s/o/%s", username, fname)
242 	}
243 
244 	return fmt.Sprintf("/o/%s", fname)
245 }
246 
247-func (c *ConfigSite) TagURL(username, tag string, onSubdomain, withUserName bool) string {
248+func (c *ConfigSite) TagURL(curl *CreateURL, username, tag string) string {
249 	tg := url.PathEscape(tag)
250-	return fmt.Sprintf("%s?tag=%s", c.FullBlogURL(username, onSubdomain, withUserName), tg)
251+	return fmt.Sprintf("%s?tag=%s", c.FullBlogURL(curl, username), tg)
252 }
253 
254 func CreateLogger() *zap.SugaredLogger {