Added contentOnly query to send html content as json
This commit is contained in:
@@ -23,40 +23,40 @@ const (
|
|||||||
</tr></table>`
|
</tr></table>`
|
||||||
)
|
)
|
||||||
|
|
||||||
func latestBlogsHandle(w http.ResponseWriter, _ *http.Request) {
|
func latestBlogsHandle(w http.ResponseWriter, r *http.Request) {
|
||||||
latest, err := blogApp.LatestBlogs(0)
|
latest, err := blogApp.LatestBlogs(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == backend.ErrNotFound {
|
if err == backend.ErrNotFound {
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
sendIndexWithContent(w, "Page not found", "", "")
|
sendContent(w, r, "Page not found", "", "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
log.Println("error getting latest blogs:", err)
|
log.Println("error getting latest blogs:", err)
|
||||||
sendIndexWithContent(w, "Error getting page", "", "")
|
sendContent(w, r, "Error getting page", "", "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var out string
|
var out string
|
||||||
for _, b := range latest {
|
for _, b := range latest {
|
||||||
out += blogElement(b)
|
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)
|
bl, err := blogApp.Blog(blog)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == backend.ErrNotFound {
|
if err == backend.ErrNotFound {
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
sendIndexWithContent(w, "Page not found", "", "")
|
sendContent(w, r, "Page not found", "", "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
log.Printf("error getting blog %v: %v\n", blog, err)
|
log.Printf("error getting blog %v: %v\n", blog, err)
|
||||||
sendIndexWithContent(w, "Error getting page", "", "")
|
sendContent(w, r, "Error getting page", "", "")
|
||||||
return
|
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) {
|
func blogElement(b *blog.Blog) (out string) {
|
||||||
|
|||||||
@@ -45,9 +45,5 @@ func filesRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if r.URL.Query().Get("contentOnly") == "true" {
|
sendContent(w, r, pageContent, "Files", "")
|
||||||
w.Write([]byte(pageContent))
|
|
||||||
} else {
|
|
||||||
sendIndexWithContent(w, pageContent, "Files", "")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -107,5 +107,5 @@ func mainHandle(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.ServeFile(w, r, filepath.Join(*webRoot, spl[0], "index.html"))
|
http.ServeFile(w, r, filepath.Join(*webRoot, spl[0], "index.html"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
blogHandle(w, path)
|
blogHandle(w, r, path)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -23,7 +23,7 @@ func portfolioRequest(w http.ResponseWriter, r *http.Request) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error getting portfolio projects:", err)
|
log.Println("error getting portfolio projects:", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
sendIndexWithContent(w, "Error getting portfolio", "", "")
|
sendContent(w, r, "Error getting portfolio", "", "")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
aboutMe := "<h1 class='about-me-header'>About Me</h1>"
|
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
|
out = aboutMe + fmt.Sprintf(portfolioSelector, tmp) + out
|
||||||
sendIndexWithContent(w, out, "Portfolio", "")
|
sendContent(w, r, out, "Portfolio", "")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -15,7 +16,11 @@ const (
|
|||||||
titleReplace = "<!--Title-->"
|
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"))
|
indexFile, err := os.Open(filepath.Join(*webRoot, "index.html"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("error when opening main index.html:", err)
|
log.Println("error when opening main index.html:", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user