Skip to content

Commit

Permalink
add a fast coded comment system
Browse files Browse the repository at this point in the history
  • Loading branch information
Gsvd committed Apr 2, 2024
1 parent 81d6f9c commit 50992be
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
tmp
node_modules
.DS_Store
.DS_Store
.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: "Create a seedbox on Raspberry Pi using the latest version of qbittorrent
description: "qBittorrent has consistently stood out for its comprehensive features. However, as of the time of writing, the only version of qbittorrent-nox available for Raspbian is somewhat outdated (4.1.5)."
slug: "create-a-seedbox-on-raspberry-pi-using-the-latest-version-of-qbittorrent"
date: "November 6, 2023"
id: 2
id: 1
---

## Introduction
Expand Down
14 changes: 10 additions & 4 deletions cmd/gsvd.dev/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package main

import (
"github.com/gofiber/fiber/v2/middleware/logger"
"log"
"net/http"

embeded "github.com/Gsvd/gsvd.dev"
"github.com/Gsvd/gsvd.dev/handlers"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/template/html/v2"
"github.com/joho/godotenv"
"log"
"net/http"
)

func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}

engine := html.NewFileSystem(http.FS(embeded.TemplateFiles), ".html")
app := fiber.New(fiber.Config{
Views: engine,
Expand Down Expand Up @@ -49,6 +54,7 @@ func main() {
app.Get("/", handlers.HomeHandler)
app.Get("/blog", handlers.BlogHandler)
app.Get("/blog/:title", handlers.BlogPostHandler)
app.Post("/blog/:title/:id", handlers.BlogPostCommentHandler)
app.Get("/resume", handlers.ResumeHandler)
app.Get("/contact", handlers.ContactHandler)

Expand Down
2 changes: 1 addition & 1 deletion dist/css/tailwind.min.css

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ module github.com/Gsvd/gsvd.dev
go 1.22.1

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/andybalholm/brotli v1.0.6 // indirect
github.com/gernest/front v0.0.0-20210301115436-8a0b0a782d0a // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/gofiber/fiber/v2 v2.50.0 // indirect
github.com/gofiber/template v1.8.2 // indirect
github.com/gofiber/template/html/v2 v2.0.5 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/gernest/front v0.0.0-20210301115436-8a0b0a782d0a h1:z7BePknRd4Nz3CeWDhcmCkuCliM2YY/RnjWpdPUuQQo=
github.com/gernest/front v0.0.0-20210301115436-8a0b0a782d0a/go.mod h1:FwEMwQ5+xky8tbzDLj72k2RAqXnFByLNwxg+9UZDtqU=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/gofiber/fiber/v2 v2.50.0 h1:ia0JaB+uw3GpNSCR5nvC5dsaxXjRU5OEu36aytx+zGw=
github.com/gofiber/fiber/v2 v2.50.0/go.mod h1:21eytvay9Is7S6z+OgPi7c7n4++tnClWmhpimVHMimw=
github.com/gofiber/template v1.8.2 h1:PIv9s/7Uq6m+Fm2MDNd20pAFFKt5wWs7ZBd8iV9pWwk=
Expand All @@ -16,6 +20,8 @@ github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
Expand Down
67 changes: 64 additions & 3 deletions handlers/blog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ package handlers

import (
"bytes"
"database/sql"
"fmt"
"html/template"
"net/http"

embeded "github.com/Gsvd/gsvd.dev"
"github.com/Gsvd/gsvd.dev/internal"
"github.com/gernest/front"
_ "github.com/go-sql-driver/mysql"
"github.com/gofiber/fiber/v2"
"github.com/mitchellh/mapstructure"
"github.com/russross/blackfriday/v2"
"html/template"
"log"
"net/http"
"os"
"time"
)

type Comment struct {
Id int
Author string
Content string
Created time.Time
CreatedFormatted string
}

func BlogHandler(c *fiber.Ctx) error {
articlesMetadata, err := internal.LoadArticlesMetadata()
if err != nil {
Expand Down Expand Up @@ -46,6 +58,30 @@ func BlogPostHandler(c *fiber.Ctx) error {
panic(err)
}

db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", os.Getenv("DB_USER"), os.Getenv("DB_PASS"), os.Getenv("DB_HOST"), os.Getenv("DB_NAME")))
if err != nil {
panic(err)
}
defer db.Close()

comments := make([]Comment, 0)

results, err := db.Query("SELECT id, author, content, created_at FROM comments WHERE article_id = ? AND approved = 1;", metadata.Id)
if err != nil {
panic(err.Error())
}
defer results.Close()

for results.Next() {
var c Comment
err := results.Scan(&c.Id, &c.Author, &c.Content, &c.Created)
if err != nil {
panic(err.Error())
}
c.CreatedFormatted = c.Created.Format("2006-01-02 15:04:05")
comments = append(comments, c)
}

htmlContent := blackfriday.Run([]byte(body))

article := internal.Article{
Expand All @@ -57,5 +93,30 @@ func BlogPostHandler(c *fiber.Ctx) error {
"Title": metadata.Title + " - Gsvd",
"Article": article,
"Canonical": "blog/" + metadata.Slug,
"Comments": comments,
}, "templates/layouts/post")
}

func BlogPostCommentHandler(c *fiber.Ctx) error {
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true", os.Getenv("DB_USER"), os.Getenv("DB_PASS"), os.Getenv("DB_HOST"), os.Getenv("DB_NAME")))
if err != nil {
panic(err)
}
defer db.Close()

articleId := c.Params("id")
articleSlug := c.Params("title")
author := c.FormValue("author")
content := c.FormValue("content")

if author == "" || content == "" || articleId == "" || articleSlug == "" {
return c.Redirect(fmt.Sprintf("/blog/%s", articleSlug))
}

_, err = db.Exec("INSERT INTO comments (article_id, author, content) VALUES (?, ?, ?);", articleId, author, content)
if err != nil {
log.Println(err.Error())
}

return c.Redirect(fmt.Sprintf("/blog/%s", articleSlug))
}
30 changes: 30 additions & 0 deletions templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,34 @@ <h1 class="text-2xl underline underline-offset-2 mb-2">{{.Article.Metadata.Title
<h2 class="text-lg mb-8 italic">{{.Article.Metadata.Date}}</h2>
<article class="prose dark:prose-dark max-w-none">{{.Article.Content}}</article>

<h2 class="text-2xl mt-8 mb-4">Comments</h2>
<div class="flex flex-col">
<div class="flex md:flex-col flex-col-reverse gap-y-2 prose dark:prose-dark max-w-none">
{{if .Comments}}
{{range .Comments}}
<div>
<blockquote class="m-0">
<p>{{.Content}}</p>
<div class="flex items-center justify-between text-sm">
<cite class="font-medium">{{.Author}}</cite>
<cite>{{.CreatedFormatted}}</cite>
</div>
</blockquote>
</div>
{{end}}
{{else}}
<p>There is no comment yet.</p>
{{end}}
<div class="flex flex-col md:mt-4">
<form action="/blog/{{.Article.Metadata.Slug}}/{{.Article.Metadata.Id}}" method="post">
<textarea placeholder="Such a great article!" class="p-4 resize-y w-full bg-[#f2e5bc] dark:bg-[#32302f] dark:text-[#fbf1c7]/[0.85] outline-none" name="content" rows="5"></textarea>
<div class="flex md:flex-row flex-col justify-end gap-2">
<input type="text" name="author" class="bg-[#f2e5bc] dark:bg-[#32302f] dark:text-[#fbf1c7]/[0.85] px-2 outline-none md:w-48 w-full" placeholder="Gordon Clark">
<input type="submit" value="Send" class="bg-[#f2e5bc] dark:bg-[#32302f] dark:text-[#fbf1c7]/[0.85] hover:cursor-pointer md:w-48 w-full">
</div>
</form>
</div>
</div>
</div>

{{template "templates/partials/footer" .}}

0 comments on commit 50992be

Please sign in to comment.