Got actual HTML stuff for blog & portfolio done

This commit is contained in:
Caleb Gardner
2024-08-01 05:16:07 -05:00
parent 91c122e212
commit 892c386027
4 changed files with 92 additions and 11 deletions
+56 -5
View File
@@ -1,20 +1,49 @@
package main package main
import ( import (
"fmt"
"log"
"net/http" "net/http"
"time"
"github.com/CalebQ42/darkstorm-server/internal/backend" "github.com/CalebQ42/darkstorm-server/internal/backend"
"github.com/CalebQ42/darkstorm-server/internal/blog"
) )
const ( const (
blogTitle = "<h2 class='blog-title'>%v</h2>" blogTitle = "<h1 class='blog-title'>%v</h1>"
blogAuthor = "<h4 class='blog-author'><i>%v</i></h4>" blogAuthor = "<h4 class='blog-author'><i><b>By %v</b></i></h4>"
blogMain = "<p class='blog'>%v</p>" blogCreate = "<h5 class='blog-time'><i>Written on: %v</i></h5"
blogMain = "<div class='blog'>%v</div>"
authorInfo = `
<table><tr>
<td><img src="%v" alt="%v" style="width:100px"></td>
<td><h2 class="author-title">%v</h2>%v</td>
</tr></table>`
) )
func latestBlogsHandle(w http.ResponseWriter, r *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")
return
}
w.WriteHeader(http.StatusInternalServerError)
log.Println("error getting latest blogs:", err)
sendIndexWithContent(w, "Error getting page")
return
}
var out string
for _, b := range latest {
out += blogElement(b)
}
sendIndexWithContent(w, out)
}
func blogHandle(w http.ResponseWriter, r *http.Request, blog string) { func blogHandle(w http.ResponseWriter, 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 {
@@ -23,8 +52,30 @@ func blogHandle(w http.ResponseWriter, r *http.Request, blog string) {
return return
} }
w.WriteHeader(http.StatusInternalServerError) 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 return
} }
sendIndexWithContent(w, blogElement(bl))
}
func blogElement(b *blog.Blog) (out string) {
out = fmt.Sprintf(blogTitle, b.Title)
out += fmt.Sprintf(blogAuthor, b.Author)
cTime := time.Unix(b.CreateTime, 0).Format(time.DateOnly)
if b.UpdateTime > b.CreateTime {
out += fmt.Sprintf(blogCreate, cTime+"; Last updated on: "+time.Unix(b.UpdateTime, 0).Format(time.DateOnly))
} else {
out += fmt.Sprintf(blogCreate, cTime)
}
out += fmt.Sprintf(blogMain, b.Blog)
auth, err := blogApp.GetAuthor(b)
if err == nil {
out += authorSection(auth)
}
return
}
func authorSection(a *blog.Author) string {
return fmt.Sprintf(authorInfo, a.PicURL, a.Name+"'s profile picture", a.Name, a.About)
} }
+3 -3
View File
@@ -174,7 +174,7 @@ func (b *BlogApp) updateBlog(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
} }
func (b *BlogApp) LatestBlogs(page int64) ([]Blog, error) { func (b *BlogApp) LatestBlogs(page int64) ([]*Blog, error) {
res, err := b.blogCol.Find(context.Background(), bson.M{}, options.Find(). res, err := b.blogCol.Find(context.Background(), bson.M{}, options.Find().
SetSort(bson.M{"createTime": 1}). SetSort(bson.M{"createTime": 1}).
SetLimit(5). SetLimit(5).
@@ -185,13 +185,13 @@ func (b *BlogApp) LatestBlogs(page int64) ([]Blog, error) {
} }
return nil, err return nil, err
} }
var out []Blog var out []*Blog
err = res.All(context.Background(), &out) err = res.All(context.Background(), &out)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for i := range out { for i := range out {
b.ConvertBlog(&out[i]) b.ConvertBlog(out[i])
} }
return out, nil return out, nil
} }
+3 -2
View File
@@ -26,6 +26,7 @@ var (
) )
func main() { func main() {
addr := ":4223"
mongoURL := flag.String("mongo", "", "Enables MongoDB usage for Darkstorm backend.") mongoURL := flag.String("mongo", "", "Enables MongoDB usage for Darkstorm backend.")
webRoot = flag.String("web-root", "", "Sets root directory of web server.") webRoot = flag.String("web-root", "", "Sets root directory of web server.")
flag.Parse() flag.Parse()
@@ -43,7 +44,7 @@ func main() {
setupBackend(mux) setupBackend(mux)
setupWebsite(mux) setupWebsite(mux)
serv := &http.Server{ serv := &http.Server{
Addr: ":4223", Addr: addr,
Handler: mux, Handler: mux,
} }
err := serv.ListenAndServeTLS(filepath.Join(flag.Arg(0), "cert.pem"), filepath.Join(flag.Arg(0), "key.pem")) err := serv.ListenAndServeTLS(filepath.Join(flag.Arg(0), "cert.pem"), filepath.Join(flag.Arg(0), "key.pem"))
@@ -107,5 +108,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, r, path) blogHandle(w, path)
} }
+30 -1
View File
@@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"slices"
"strings"
) )
const ( const (
@@ -16,13 +18,21 @@ const (
) )
func portfolioRequest(w http.ResponseWriter, r *http.Request) { func portfolioRequest(w http.ResponseWriter, r *http.Request) {
proj, err := blogApp.Projects(r.URL.Query().Get("lang")) selectedLang := r.URL.Query().Get("lang")
proj, err := blogApp.Projects(selectedLang)
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") sendIndexWithContent(w, "Error getting portfolio")
return return
} }
aboutMe := "<h1 style='margin-bottom:-5px'>About Me</h1>"
if me, err := blogApp.AboutMe(); err != nil {
aboutMe += "Error getting info about me :("
} else {
aboutMe += authorSection(me)
}
aboutMe += "<h1 style='margin-bottom:15px'>My Projects</h1>"
langs := make(map[string]struct{}) langs := make(map[string]struct{})
out := "" out := ""
for _, p := range proj { for _, p := range proj {
@@ -34,5 +44,24 @@ func portfolioRequest(w http.ResponseWriter, r *http.Request) {
} }
out += fmt.Sprintf(portfolioDesc, p.Description) out += fmt.Sprintf(portfolioDesc, p.Description)
} }
langKeys := make([]string, 0, len(langs))
for k := range langs {
langKeys = append(langKeys, k)
}
slices.Sort(langKeys)
var tmp string
if selectedLang == "" {
tmp = fmt.Sprintf(portfolioSelectorOption, "", " selected=true", "All")
} else {
tmp = fmt.Sprintf(portfolioSelectorOption, "", "", "All")
}
for _, k := range langKeys {
if selectedLang == strings.ToLower(k) {
tmp += fmt.Sprintf(portfolioSelectorOption, strings.ToLower(k), " selected=true", k)
} else {
tmp += fmt.Sprintf(portfolioSelectorOption, strings.ToLower(k), "", k)
}
}
out = aboutMe + fmt.Sprintf(portfolioSelector, tmp) + out
sendIndexWithContent(w, out) sendIndexWithContent(w, out)
} }