From 87ac3bf270872ee9fd9ecacf5b17a20728be9aa2 Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Fri, 2 Aug 2024 05:40:30 -0500 Subject: [PATCH] Added title and favicon support --- blog.go | 14 +++++++------- files.go | 2 +- portfolio.go | 8 ++++---- web.go | 18 +++++++++++++++--- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/blog.go b/blog.go index abb6d94..19b5dab 100644 --- a/blog.go +++ b/blog.go @@ -18,7 +18,7 @@ const ( authorInfo = ` - +
%v%v

%v

%v
` ) @@ -28,19 +28,19 @@ func latestBlogsHandle(w http.ResponseWriter, _ *http.Request) { if err != nil { if err == backend.ErrNotFound { w.WriteHeader(404) - sendIndexWithContent(w, "Page not found") + sendIndexWithContent(w, "Page not found", "", "") return } w.WriteHeader(http.StatusInternalServerError) log.Println("error getting latest blogs:", err) - sendIndexWithContent(w, "Error getting page") + sendIndexWithContent(w, "Error getting page", "", "") return } var out string for _, b := range latest { out += blogElement(b) } - sendIndexWithContent(w, out) + sendIndexWithContent(w, out, "", "") } func blogHandle(w http.ResponseWriter, blog string) { @@ -48,15 +48,15 @@ func blogHandle(w http.ResponseWriter, blog string) { if err != nil { if err == backend.ErrNotFound { w.WriteHeader(404) - sendIndexWithContent(w, "Page not found") + sendIndexWithContent(w, "Page not found", "", "") return } w.WriteHeader(http.StatusInternalServerError) log.Printf("error getting blog %v: %v\n", blog, err) - sendIndexWithContent(w, "Error getting page") + sendIndexWithContent(w, "Error getting page", "", "") return } - sendIndexWithContent(w, blogElement(bl)) + sendIndexWithContent(w, blogElement(bl), bl.Title, bl.Favicon) } func blogElement(b *blog.Blog) (out string) { diff --git a/files.go b/files.go index 271e0bb..e844280 100644 --- a/files.go +++ b/files.go @@ -48,6 +48,6 @@ func filesRequest(w http.ResponseWriter, r *http.Request) { if r.URL.Query().Get("contentOnly") == "true" { w.Write([]byte(pageContent)) } else { - sendIndexWithContent(w, pageContent) + sendIndexWithContent(w, pageContent, "Files", "") } } diff --git a/portfolio.go b/portfolio.go index ce0ceeb..cfaae0c 100644 --- a/portfolio.go +++ b/portfolio.go @@ -23,16 +23,16 @@ 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") + sendIndexWithContent(w, "Error getting portfolio", "", "") return } - aboutMe := "

About Me

" + aboutMe := "

About Me

" if me, err := blogApp.AboutMe(); err != nil { aboutMe += "Error getting info about me :(" } else { aboutMe += authorSection(me) } - aboutMe += "

My Projects

" + aboutMe += "

My Projects

" langs := make(map[string]struct{}) out := "" for _, p := range proj { @@ -63,5 +63,5 @@ func portfolioRequest(w http.ResponseWriter, r *http.Request) { } } out = aboutMe + fmt.Sprintf(portfolioSelector, tmp) + out - sendIndexWithContent(w, out) + sendIndexWithContent(w, out, "Portfolio", "") } diff --git a/web.go b/web.go index 755b65c..ac8da29 100644 --- a/web.go +++ b/web.go @@ -9,9 +9,13 @@ import ( "path/filepath" ) -const replacementComment = "" +const ( + contentReplace = "" + faviconReplace = "" + titleReplace = "" +) -func sendIndexWithContent(w http.ResponseWriter, content string) { +func sendIndexWithContent(w http.ResponseWriter, content string, title string, favicon string) { indexFile, err := os.Open(filepath.Join(*webRoot, "index.html")) if err != nil { log.Println("error when opening main index.html:", err) @@ -24,7 +28,15 @@ func sendIndexWithContent(w http.ResponseWriter, content string) { w.WriteHeader(http.StatusInternalServerError) return } - dat = bytes.ReplaceAll(dat, []byte(replacementComment), []byte(content)) + dat = bytes.ReplaceAll(dat, []byte(contentReplace), []byte(content)) + if title == "" { + title = "Darkstorm.tech" + } + dat = bytes.ReplaceAll(dat, []byte(titleReplace), []byte(title)) + if favicon == "" { + favicon = "https://darkstorm.tech/favicon.png" + } + dat = bytes.ReplaceAll(dat, []byte(faviconReplace), []byte(favicon)) w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write(dat) }