Simplified sending HTMX
This commit is contained in:
@@ -24,11 +24,7 @@ func latestBlogsHandle(w http.ResponseWriter, r *http.Request) {
|
|||||||
for _, b := range latest {
|
for _, b := range latest {
|
||||||
out += b.HTMX(blogApp, r.Context())
|
out += b.HTMX(blogApp, r.Context())
|
||||||
}
|
}
|
||||||
if r.Header.Get("Hx-Request") == "true" {
|
sendContent(w, r, out, "", "")
|
||||||
w.Write([]byte("<title>Darkstorm.tech</title>" + out))
|
|
||||||
} else {
|
|
||||||
sendContent(w, r, out, "", "")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func blogHandle(w http.ResponseWriter, r *http.Request, blog string) {
|
func blogHandle(w http.ResponseWriter, r *http.Request, blog string) {
|
||||||
@@ -44,9 +40,5 @@ func blogHandle(w http.ResponseWriter, r *http.Request, blog string) {
|
|||||||
sendContent(w, r, "Error getting page", "", "")
|
sendContent(w, r, "Error getting page", "", "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if r.Header.Get("Hx-Request") == "true" {
|
sendContent(w, r, bl.HTMX(blogApp, r.Context()), bl.Title, bl.Favicon)
|
||||||
w.Write([]byte("<title>" + bl.Title + "</title>" + bl.HTMX(blogApp, r.Context())))
|
|
||||||
} else {
|
|
||||||
sendContent(w, r, bl.HTMX(blogApp, r.Context()), bl.Title, bl.Favicon)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func filesRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
pageContent = "<p>404 Not Found</p>"
|
pageContent = "<p>404 Not Found</p>"
|
||||||
// w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
} else {
|
} else {
|
||||||
pageContent = "<p>Server error!</p>"
|
pageContent = "<p>Server error!</p>"
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
@@ -52,9 +52,5 @@ func filesRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if r.Header.Get("Hx-Request") == "true" {
|
sendContent(w, r, pageContent, "Files", "")
|
||||||
w.Write([]byte(pageContent))
|
|
||||||
} else {
|
|
||||||
sendContent(w, r, pageContent, "Files", "")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-5
@@ -14,9 +14,5 @@ func portfolioRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
sendContent(w, r, "Error getting portfolio", "", "")
|
sendContent(w, r, "Error getting portfolio", "", "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if r.Header.Get("Hx-Request") == "true" {
|
sendContent(w, r, proj.FullHTMX(r.Context(), blogApp, selectedTech), "Portfolio", "")
|
||||||
w.Write([]byte("<title>Portfolio</title>" + proj.FullHTMX(r.Context(), blogApp, selectedTech)))
|
|
||||||
} else {
|
|
||||||
sendContent(w, r, proj.FullHTMX(r.Context(), blogApp, selectedTech), "Portfolio", "")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func sendContent(w http.ResponseWriter, r *http.Request, content string, title string, favicon string) {
|
func sendContent(w http.ResponseWriter, r *http.Request, content string, title string, favicon string) {
|
||||||
|
if title == "" {
|
||||||
|
title = "Darkstorm.tech"
|
||||||
|
}
|
||||||
|
if r.Header.Get("Hx-Request") == "true" {
|
||||||
|
w.Write([]byte("<title>" + title + "</title>" + content))
|
||||||
|
return
|
||||||
|
}
|
||||||
if r.URL.Query().Get("contentOnly") == "true" {
|
if r.URL.Query().Get("contentOnly") == "true" {
|
||||||
json.NewEncoder(w).Encode(map[string]string{"content": content, "title": title, "favicon": favicon})
|
json.NewEncoder(w).Encode(map[string]string{"content": content, "title": title, "favicon": favicon})
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user