Added contentOnly query to send html content as json

This commit is contained in:
Caleb Gardner
2024-08-06 16:46:25 -05:00
parent 0d40dd95c1
commit f99c9bd2fb
5 changed files with 18 additions and 17 deletions
+8 -8
View File
@@ -23,40 +23,40 @@ const (
</tr></table>`
)
func latestBlogsHandle(w http.ResponseWriter, _ *http.Request) {
func latestBlogsHandle(w http.ResponseWriter, r *http.Request) {
latest, err := blogApp.LatestBlogs(0)
if err != nil {
if err == backend.ErrNotFound {
w.WriteHeader(404)
sendIndexWithContent(w, "Page not found", "", "")
sendContent(w, r, "Page not found", "", "")
return
}
w.WriteHeader(http.StatusInternalServerError)
log.Println("error getting latest blogs:", err)
sendIndexWithContent(w, "Error getting page", "", "")
sendContent(w, r, "Error getting page", "", "")
return
}
var out string
for _, b := range latest {
out += blogElement(b)
}
sendIndexWithContent(w, out, "", "")
sendContent(w, r, out, "", "")
}
func blogHandle(w http.ResponseWriter, blog string) {
func blogHandle(w http.ResponseWriter, r *http.Request, blog string) {
bl, err := blogApp.Blog(blog)
if err != nil {
if err == backend.ErrNotFound {
w.WriteHeader(404)
sendIndexWithContent(w, "Page not found", "", "")
sendContent(w, r, "Page not found", "", "")
return
}
w.WriteHeader(http.StatusInternalServerError)
log.Printf("error getting blog %v: %v\n", blog, err)
sendIndexWithContent(w, "Error getting page", "", "")
sendContent(w, r, "Error getting page", "", "")
return
}
sendIndexWithContent(w, blogElement(bl), bl.Title, bl.Favicon)
sendContent(w, r, blogElement(bl), bl.Title, bl.Favicon)
}
func blogElement(b *blog.Blog) (out string) {
+1 -5
View File
@@ -45,9 +45,5 @@ func filesRequest(w http.ResponseWriter, r *http.Request) {
return
}
}
if r.URL.Query().Get("contentOnly") == "true" {
w.Write([]byte(pageContent))
} else {
sendIndexWithContent(w, pageContent, "Files", "")
}
sendContent(w, r, pageContent, "Files", "")
}
+1 -1
View File
@@ -107,5 +107,5 @@ func mainHandle(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(*webRoot, spl[0], "index.html"))
return
}
blogHandle(w, path)
blogHandle(w, r, path)
}
+2 -2
View File
@@ -23,7 +23,7 @@ func portfolioRequest(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Println("error getting portfolio projects:", err)
w.WriteHeader(http.StatusInternalServerError)
sendIndexWithContent(w, "Error getting portfolio", "", "")
sendContent(w, r, "Error getting portfolio", "", "")
return
}
aboutMe := "<h1 class='about-me-header'>About Me</h1>"
@@ -63,5 +63,5 @@ func portfolioRequest(w http.ResponseWriter, r *http.Request) {
}
}
out = aboutMe + fmt.Sprintf(portfolioSelector, tmp) + out
sendIndexWithContent(w, out, "Portfolio", "")
sendContent(w, r, out, "Portfolio", "")
}
+6 -1
View File
@@ -2,6 +2,7 @@ package main
import (
"bytes"
"encoding/json"
"io"
"log"
"net/http"
@@ -15,7 +16,11 @@ const (
titleReplace = "<!--Title-->"
)
func sendIndexWithContent(w http.ResponseWriter, content string, title string, favicon string) {
func sendContent(w http.ResponseWriter, r *http.Request, content string, title string, favicon string) {
if r.URL.Query().Get("contentOnly") == "true" {
json.NewEncoder(w).Encode(map[string]string{"content": content, "title": title, "favicon": favicon})
return
}
indexFile, err := os.Open(filepath.Join(*webRoot, "index.html"))
if err != nil {
log.Println("error when opening main index.html:", err)