diff --git a/cmd/dwelling-home/main.go b/cmd/dwelling-home/main.go index fce1697..d6bb6e5 100644 --- a/cmd/dwelling-home/main.go +++ b/cmd/dwelling-home/main.go @@ -17,7 +17,6 @@ import ( "git.arav.su/Arav/dwelling-home/web" "git.arav.su/Arav/httpr" gb "git.arav.su/Arav/justguestbook" - "github.com/a-h/templ" ) var ( @@ -56,7 +55,7 @@ func main() { r := httpr.New() r.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) { - dwhttp.Error(w, http.StatusNotFound, "", "", r.Referer()) + dwhttp.Error(w, http.StatusNotFound, "", "", r) } r.ServeStatic("/assets/*filepath", web.Assets()) @@ -65,7 +64,7 @@ func main() { r.Handler(http.MethodGet, "/rss.xml", hand.RSS) r.Handler(http.MethodGet, "/sitemap.xml", dwhttp.ServeAsset("sitemap.xml")) - r.Handler(http.MethodGet, "/", templ.Handler(web.Index()).ServeHTTP) + r.Handler(http.MethodGet, "/", hand.Index) r.Handler(http.MethodGet, "/privacy", hand.Privacy) r.Handler(http.MethodGet, "/stuff", hand.Stuff) r.Handler(http.MethodGet, "/stuff/article/*filepath", hand.Article) diff --git a/internal/http/api_guestbook_handlers.go b/internal/http/api_guestbook_handlers.go index c49c2c9..c9e478b 100644 --- a/internal/http/api_guestbook_handlers.go +++ b/internal/http/api_guestbook_handlers.go @@ -30,7 +30,7 @@ func (h *GuestbookApiHandlers) New(w http.ResponseWriter, r *http.Request) { if !inmemdb.Solve(captcha.ID(r.FormValue("captcha_id")), captcha.Answer(r.FormValue("captcha_answer"))) { Error(w, http.StatusForbidden, "Wrong answer given.", - "Here's your message:"+r.FormValue("message"), r.Referer()) + "Here's your message:"+r.FormValue("message"), r) return } @@ -44,14 +44,14 @@ func (h *GuestbookApiHandlers) New(w http.ResponseWriter, r *http.Request) { r.FormValue("website"), r.FormValue("hide_website") != "") if err != nil { Error(w, http.StatusInternalServerError, err.Error(), - "Here's your message:"+r.FormValue("message"), r.Referer()) + "Here's your message:"+r.FormValue("message"), r) return } } if err = h.db.NewEntry(entry); err != nil { Error(w, http.StatusInternalServerError, err.Error(), - "Here's your message:"+r.FormValue("message"), r.Referer()) + "Here's your message:"+r.FormValue("message"), r) return } diff --git a/internal/http/api_mindflow_handlers.go b/internal/http/api_mindflow_handlers.go index 23b9c91..fd10da0 100644 --- a/internal/http/api_mindflow_handlers.go +++ b/internal/http/api_mindflow_handlers.go @@ -51,7 +51,7 @@ func (h *MindflowApiHandlers) NewPost(w http.ResponseWriter, r *http.Request) { if err = h.db.NewPost(post); err != nil { msg := strings.Join([]string{"Title:", r.FormValue("title"), "| Body:", r.FormValue("body")}, " ") - Error(w, http.StatusInternalServerError, err.Error(), msg, r.Referer()) + Error(w, http.StatusInternalServerError, err.Error(), msg, r) return } diff --git a/internal/http/web_handlers.go b/internal/http/web_handlers.go index e190ac2..ba298e1 100644 --- a/internal/http/web_handlers.go +++ b/internal/http/web_handlers.go @@ -35,6 +35,10 @@ func NewHandlers(captchaExpire time.Duration, owner string, gbPageSize int64, guestbookPageSize: gbPageSize} } +func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) { + web.Index(r).Render(context.Background(), w) +} + func (h *Handlers) Privacy(w http.ResponseWriter, r *http.Request) { web.Privacy(r).Render(context.Background(), w) } @@ -47,14 +51,14 @@ func (h *Handlers) Mindflow(w http.ResponseWriter, r *http.Request) { posts, err := h.mindflowDB.Posts() if err != nil { log.Println("Cannot load posts.", err) - Error(w, http.StatusInternalServerError, "Cannot load posts.", "", r.Referer()) + Error(w, http.StatusInternalServerError, "Cannot load posts.", "", r) return } categories, err := h.mindflowDB.Categories() if err != nil { log.Println("Cannot load categories.", err) - Error(w, http.StatusInternalServerError, "Cannot load categories.", "", r.Referer()) + Error(w, http.StatusInternalServerError, "Cannot load categories.", "", r) return } @@ -64,13 +68,13 @@ func (h *Handlers) Mindflow(w http.ResponseWriter, r *http.Request) { func (h *Handlers) MindflowAdmin(w http.ResponseWriter, r *http.Request) { posts, err := h.mindflowDB.Posts() if err != nil { - Error(w, http.StatusInternalServerError, err.Error(), "failed to load posts", r.Referer()) + Error(w, http.StatusInternalServerError, err.Error(), "failed to load posts", r) return } categories, err := h.mindflowDB.Categories() if err != nil { - Error(w, http.StatusInternalServerError, err.Error(), "failed to load categories", r.Referer()) + Error(w, http.StatusInternalServerError, err.Error(), "failed to load categories", r) return } @@ -99,7 +103,7 @@ func (h *Handlers) Article(w http.ResponseWriter, r *http.Request) { name := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:] artcl := web.GetArticle(name) if artcl == nil { - Error(w, http.StatusNotFound, "", "An article \""+name+"\" doesn't exist.", r.Referer()) + Error(w, http.StatusNotFound, "", "An article \""+name+"\" doesn't exist.", r) return } @@ -147,7 +151,7 @@ func (h *Handlers) GuestbookAdmin(w http.ResponseWriter, r *http.Request) { entriesCount, _ := h.guestbookDB.Count() entries, err := h.guestbookDB.Entries(1, entriesCount) if err != nil { - Error(w, http.StatusInternalServerError, err.Error(), "cannot load gb", r.Referer()) + Error(w, http.StatusInternalServerError, err.Error(), "cannot load gb", r) return } for _, entry := range entries { @@ -178,9 +182,9 @@ func ServeAsset(path string) func(http.ResponseWriter, *http.Request) { } } -func Error(w http.ResponseWriter, code int, reason, message, referer string) { +func Error(w http.ResponseWriter, code int, reason, message string, r *http.Request) { w.WriteHeader(code) - web.ErrorXXX(code, reason, message, referer).Render(context.Background(), w) + web.ErrorXXX(code, reason, message, r).Render(context.Background(), w) } func cleanupNewlines(text string) (out string) { diff --git a/web/about.templ b/web/about.templ index 13e46e6..cba141a 100644 --- a/web/about.templ +++ b/web/about.templ @@ -6,7 +6,7 @@ import "git.arav.su/Arav/dwelling-home/pkg/servicestat" import "git.arav.su/Arav/dwelling-home/pkg/util" templ About(services *servicestat.ServiceList, r *http.Request) { - @base("About", "About me and my home servers.", "about, me, servcies", "/about", aboutHead()) { + @base("About", "About me and my home servers.", "about, me, servcies", "/about", r, aboutHead()) {

Me

Who am I? My name is Alexander Andreev. I'm a russian guy of age 31 who likes tinkering with computers.

diff --git a/web/article.templ b/web/article.templ index ee65eec..ba0a2ee 100644 --- a/web/article.templ +++ b/web/article.templ @@ -6,7 +6,7 @@ import "net/http" import "git.arav.su/Arav/dwelling-home/pkg/util" templ Article(title, description, body, urlName string, date time.Time, r *http.Request) { - @base(title + " - Stuff", description, "", "/stuff/article/"+urlName, articleHead()) { + @base(title + " - Stuff", description, "", "/stuff/article/"+urlName, r, articleHead()) {

{ title }

diff --git a/web/base.templ b/web/base.templ index de69861..79611e1 100644 --- a/web/base.templ +++ b/web/base.templ @@ -1,10 +1,12 @@ package web import "strings" +import "net/http" import "git.arav.su/Arav/dwelling-home/internal/version" +import "git.arav.su/Arav/dwelling-home/pkg/util" -templ base(title, description, keywords, canonical string, head templ.Component) { +templ base(title, description, keywords, canonical string, r *http.Request, head templ.Component) { @@ -23,7 +25,7 @@ templ base(title, description, keywords, canonical string, head templ.Component) if canonical != "" { - + } if head != nil { @head diff --git a/web/errorXXX.templ b/web/errorXXX.templ index d946bc6..e56c8fb 100644 --- a/web/errorXXX.templ +++ b/web/errorXXX.templ @@ -3,9 +3,9 @@ package web import "fmt" import "net/http" -templ ErrorXXX(errCode int, reason, message, referer string) { +templ ErrorXXX(errCode int, reason, message string, r *http.Request) { {{ errText := http.StatusText(errCode) }} - @base(errText, errText, "", "", errorXXXHead()) { + @base(errText, errText, "", "", r, errorXXXHead()) {

{ fmt.Sprint(errCode) }

{ errText } @@ -16,9 +16,9 @@ templ ErrorXXX(errCode int, reason, message, referer string) {

{ message }

}
- if referer != "" { + if r.Referer() != "" {
-

Go back

+

Go back

} } diff --git a/web/guestbook.templ b/web/guestbook.templ index 48f40c7..faaf8e3 100644 --- a/web/guestbook.templ +++ b/web/guestbook.templ @@ -11,7 +11,7 @@ import "git.arav.su/Arav/justguestbook" import "git.arav.su/Arav/dwelling-home/pkg/util" templ Guestbook(captchaID, owner string, entries []*justguestbook.Entry, pageCount, pageCur int64, r *http.Request) { - @base("Guestbook", "This is my guestbook. Welcome.", "guestbook, personal", "/guestbook", guestbookHead()) { + @base("Guestbook", "This is my guestbook. Welcome.", "guestbook, personal", "/guestbook", r, guestbookHead()) {
diff --git a/web/guestbook_admin.templ b/web/guestbook_admin.templ index 91d181b..cbb6c79 100644 --- a/web/guestbook_admin.templ +++ b/web/guestbook_admin.templ @@ -5,5 +5,5 @@ import "net/http" import "git.arav.su/Arav/justguestbook" templ GuestbookAdmin(owner string, entries []*justguestbook.Entry, r *http.Request) { - @base("Guestbook Administration", "", "", "/guestbook/admin", nil) + @base("Guestbook Administration", "", "", "/guestbook/admin", r, nil) } diff --git a/web/index.templ b/web/index.templ index b1e2731..cb09a85 100644 --- a/web/index.templ +++ b/web/index.templ @@ -1,7 +1,9 @@ package web -templ Index() { - @base("", "A homepage of a russian guy Alexander aka Arav. Not just homepage, but something more...", "homepage, personal, blog, services, self-hosting", "/", indexHead()) { +import "net/http" + +templ Index(r *http.Request) { + @base("", "A homepage of a russian guy Alexander aka Arav. Not just homepage, but something more...", "homepage, personal, blog, services, self-hosting", "/", r, indexHead()) {
arav.su.onion.ygg.i2pah diff --git a/web/mindflow.templ b/web/mindflow.templ index 7574b2c..cb98eff 100644 --- a/web/mindflow.templ +++ b/web/mindflow.templ @@ -8,7 +8,7 @@ import "git.arav.su/Arav/dwelling-home/pkg/mindflow" import "git.arav.su/Arav/dwelling-home/pkg/util" templ Mindflow(posts []mindflow.Post, categories []mindflow.Category, r *http.Request) { - @base("Mindflow", "Updates on my infrastructure, my very important opinions and thoughts.", "updates, thoughts, opinions, blog, diary", "/mindflow", mindflowHead()) { + @base("Mindflow", "Updates on my infrastructure, my very important opinions and thoughts.", "updates, thoughts, opinions, blog, diary", "/mindflow", r, mindflowHead()) {

Here I post updates on websites and infrastructure, my very important opinions and thoughts no one asked for. If you'd like to subscribe to this bullshittery then RSS feed at your service. :)