Got actual HTML stuff for blog & portfolio done
This commit is contained in:
@@ -1,20 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/CalebQ42/darkstorm-server/internal/backend"
|
||||
"github.com/CalebQ42/darkstorm-server/internal/blog"
|
||||
)
|
||||
|
||||
const (
|
||||
blogTitle = "<h2 class='blog-title'>%v</h2>"
|
||||
blogAuthor = "<h4 class='blog-author'><i>%v</i></h4>"
|
||||
blogMain = "<p class='blog'>%v</p>"
|
||||
blogTitle = "<h1 class='blog-title'>%v</h1>"
|
||||
blogAuthor = "<h4 class='blog-author'><i><b>By %v</b></i></h4>"
|
||||
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)
|
||||
if err != nil {
|
||||
if err == backend.ErrNotFound {
|
||||
@@ -23,8 +52,30 @@ func blogHandle(w http.ResponseWriter, r *http.Request, blog string) {
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
log.Printf("error getting blog %v: %v\n", blog, err)
|
||||
sendIndexWithContent(w, "Error getting page")
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ func (b *BlogApp) updateBlog(w http.ResponseWriter, r *http.Request) {
|
||||
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().
|
||||
SetSort(bson.M{"createTime": 1}).
|
||||
SetLimit(5).
|
||||
@@ -185,13 +185,13 @@ func (b *BlogApp) LatestBlogs(page int64) ([]Blog, error) {
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
var out []Blog
|
||||
var out []*Blog
|
||||
err = res.All(context.Background(), &out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range out {
|
||||
b.ConvertBlog(&out[i])
|
||||
b.ConvertBlog(out[i])
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ var (
|
||||
)
|
||||
|
||||
func main() {
|
||||
addr := ":4223"
|
||||
mongoURL := flag.String("mongo", "", "Enables MongoDB usage for Darkstorm backend.")
|
||||
webRoot = flag.String("web-root", "", "Sets root directory of web server.")
|
||||
flag.Parse()
|
||||
@@ -43,7 +44,7 @@ func main() {
|
||||
setupBackend(mux)
|
||||
setupWebsite(mux)
|
||||
serv := &http.Server{
|
||||
Addr: ":4223",
|
||||
Addr: addr,
|
||||
Handler: mux,
|
||||
}
|
||||
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"))
|
||||
return
|
||||
}
|
||||
blogHandle(w, r, path)
|
||||
blogHandle(w, path)
|
||||
}
|
||||
|
||||
+30
-1
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -16,13 +18,21 @@ const (
|
||||
)
|
||||
|
||||
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 {
|
||||
log.Println("error getting portfolio projects:", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
sendIndexWithContent(w, "Error getting portfolio")
|
||||
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{})
|
||||
out := ""
|
||||
for _, p := range proj {
|
||||
@@ -34,5 +44,24 @@ func portfolioRequest(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user