From 62fae652be4589790dae2b30380986bd122a567f Mon Sep 17 00:00:00 2001
From: Pedro Costa <550684+pnmcosta@users.noreply.github.com>
Date: Fri, 5 Apr 2024 11:57:44 +0100
Subject: [PATCH] concurrenctly generate files
---
Makefile | 2 +-
internal/posts/posts.go | 6 +-
internal/templates/index/view.templ | 2 +-
internal/templates/post/link.templ | 2 +-
internal/templates/post/view.templ | 2 +-
internal/templates/tag/view.templ | 2 +-
main.go | 118 ++++++++++++++++------------
7 files changed, 75 insertions(+), 59 deletions(-)
diff --git a/Makefile b/Makefile
index 09962d6..ffbc1e0 100644
--- a/Makefile
+++ b/Makefile
@@ -23,7 +23,7 @@ assets: clean
@cp -a assets/. ./public/
generate: ## Go and Templ generate
- @npx tailwindcss -i style.css -o ./public/style.css
+ @npx tailwindcss -i style.css -o ./public/style.css --minify
@templ generate
run: ## run and watch
diff --git a/internal/posts/posts.go b/internal/posts/posts.go
index 294601d..8c15896 100644
--- a/internal/posts/posts.go
+++ b/internal/posts/posts.go
@@ -24,10 +24,10 @@ type Post struct {
}
type walker struct {
- posts []Post
+ posts []*Post
}
-func ParsePosts() []Post {
+func ParsePosts() []*Post {
w := walker{}
filepath.WalkDir("./posts", w.walk)
// stort by latest
@@ -115,7 +115,7 @@ func (w *walker) walk(s string, d fs.DirEntry, err error) error {
}
log.Printf("%s: post parsed\n", s)
- w.posts = append(w.posts, post)
+ w.posts = append(w.posts, &post)
}
return nil
}
diff --git a/internal/templates/index/view.templ b/internal/templates/index/view.templ
index aaf8e02..c080039 100644
--- a/internal/templates/index/view.templ
+++ b/internal/templates/index/view.templ
@@ -4,7 +4,7 @@ import "github.com/pnmcosta/csta.dev/internal/posts"
import "github.com/pnmcosta/csta.dev/internal/templates/layout"
import "github.com/pnmcosta/csta.dev/internal/templates/post"
-templ View(posts []posts.Post, tags []string) {
+templ View(posts []*posts.Post, tags []string) {
@layout.Base() {
Olá, I'm Pedro Maia Costa a product
diff --git a/internal/templates/post/link.templ b/internal/templates/post/link.templ
index 24e259a..021e0fd 100644
--- a/internal/templates/post/link.templ
+++ b/internal/templates/post/link.templ
@@ -4,6 +4,6 @@ import "github.com/pnmcosta/csta.dev/internal/posts"
import "path"
import "github.com/gosimple/slug"
-templ Link(post posts.Post) {
+templ Link(post *posts.Post) {
{ post.Title } ({ post.Date.Format("2006/01/02") })
}
diff --git a/internal/templates/post/view.templ b/internal/templates/post/view.templ
index c0b6cc2..45389b4 100644
--- a/internal/templates/post/view.templ
+++ b/internal/templates/post/view.templ
@@ -3,7 +3,7 @@ package post
import "github.com/pnmcosta/csta.dev/internal/posts"
import "github.com/pnmcosta/csta.dev/internal/templates/layout"
-templ View(post posts.Post, content templ.Component) {
+templ View(post *posts.Post, content templ.Component) {
@layout.Base() {
@content
diff --git a/internal/templates/tag/view.templ b/internal/templates/tag/view.templ
index 7a578d2..cf58af6 100644
--- a/internal/templates/tag/view.templ
+++ b/internal/templates/tag/view.templ
@@ -4,7 +4,7 @@ import "github.com/pnmcosta/csta.dev/internal/posts"
import "github.com/pnmcosta/csta.dev/internal/templates/layout"
import "github.com/pnmcosta/csta.dev/internal/templates/post"
-templ View(tag string, posts []posts.Post) {
+templ View(tag string, posts []*posts.Post) {
@layout.Base() {
if len(posts) > 0 {
{ tag } Posts
diff --git a/main.go b/main.go
index 7bafe48..126e6d8 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path"
+ "sync"
"github.com/gosimple/slug"
"github.com/pnmcosta/csta.dev/internal/posts"
@@ -26,74 +27,89 @@ func main() {
// Output path.
rootPath := "public"
- if err := os.Mkdir(rootPath, 0755); err != nil && !os.IsExist(err) {
+ if err := os.Mkdir(rootPath, 0755); err != nil && os.IsNotExist(err) {
log.Fatalf("failed to create output directory: %v", err)
}
- tags := map[string][]Post{}
+ tags := map[string][]*Post{}
+ var wg sync.WaitGroup
+ wg.Add(len(posts))
// Create a page for each post.
for _, post := range posts {
- // Create the output directory.
- dir := path.Join(rootPath, post.Date.Format("2006/01/02"), slug.Make(post.Title))
- if err := os.MkdirAll(dir, 0755); err != nil && !os.IsExist(err) {
- log.Fatalf("failed to create dir %q: %v", dir, err)
- continue
- }
-
- // Create the output file.
- name := path.Join(dir, "index.html")
- f, err := os.Create(name)
- if err != nil {
- log.Fatalf("failed to create output file: %v", err)
- continue
- }
-
- // Create an unsafe component containing raw HTML.
- content := postTempl.Unsafe(string(post.Content))
-
- // Use templ to render the template containing the raw HTML.
- err = postTempl.View(post, content).Render(context.Background(), f)
- if err != nil {
- log.Fatalf("failed to write output file: %v", err)
- continue
- }
-
+ go func() {
+ defer wg.Done()
+
+ // Create the output directory.
+ dir := path.Join(rootPath, post.Date.Format("2006/01/02"), slug.Make(post.Title))
+ if err := os.MkdirAll(dir, 0755); err != nil && os.IsNotExist(err) {
+ log.Fatalf("failed to create dir %q: %v", dir, err)
+ return
+ }
+
+ // Create the output file.
+ name := path.Join(dir, "index.html")
+ f, err := os.Create(name)
+ if err != nil {
+ log.Fatalf("failed to create output file: %v", err)
+ return
+ }
+
+ // Create an unsafe component containing raw HTML.
+ content := postTempl.Unsafe(string(post.Content))
+
+ // Use templ to render the template containing the raw HTML.
+ err = postTempl.View(post, content).Render(context.Background(), f)
+ if err != nil {
+ log.Fatalf("failed to write output file: %v", err)
+ return
+ }
+ }()
+
+ // note failure to render will still generate tag ref
for _, tag := range post.Tags {
tags[tag] = append(tags[tag], post)
}
}
+ wg.Wait()
+ wg.Add(len(tags))
// Create a page for each tag.
tagKeys := make([]string, 0, len(tags))
for tag, posts := range tags {
- slug := slug.Make(tag)
-
- // Create the output directory.
- dir := path.Join(rootPath, "tag", slug)
- if err := os.MkdirAll(dir, 0755); err != nil && !os.IsExist(err) {
- log.Fatalf("failed to create dir %q: %v", dir, err)
- continue
- }
-
- // Create the output file.
- name := path.Join(dir, "index.html")
- f, err := os.Create(name)
- if err != nil {
- log.Fatalf("failed to create output file: %v", err)
- continue
- }
-
- // Use templ to render the template containing the raw HTML.
- err = tagTempl.View(tag, posts).Render(context.Background(), f)
- if err != nil {
- log.Fatalf("failed to write output file: %v", err)
- continue
- }
-
tagKeys = append(tagKeys, tag)
+
+ go func() {
+ defer wg.Done()
+
+ slug := slug.Make(tag)
+
+ // Create the output directory.
+ dir := path.Join(rootPath, "tag", slug)
+ if err := os.MkdirAll(dir, 0755); err != nil && os.IsNotExist(err) {
+ log.Fatalf("failed to create dir %q: %v", dir, err)
+ return
+ }
+
+ // Create the output file.
+ name := path.Join(dir, "index.html")
+ f, err := os.Create(name)
+ if err != nil {
+ log.Fatalf("failed to create output file: %v", err)
+ return
+ }
+
+ // Use templ to render the template containing the raw HTML.
+ err = tagTempl.View(tag, posts).Render(context.Background(), f)
+ if err != nil {
+ log.Fatalf("failed to write output file: %v", err)
+ return
+ }
+ }()
}
+ wg.Wait()
+
// Create an index page.
name := path.Join(rootPath, "index.html")
f, err := os.Create(name)