Skip to content

Commit

Permalink
posts page, improve build logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pnmcosta committed Apr 6, 2024
1 parent 8beea11 commit a641975
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 26 deletions.
1 change: 0 additions & 1 deletion internal/posts/posts.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ func (w *walker) walk(s string, d fs.DirEntry, err error) error {
post.Date = time.Now().UTC()
}

log.Printf("%s: post parsed\n", s)
w.posts = append(w.posts, &post)
}
return nil
Expand Down
21 changes: 21 additions & 0 deletions internal/templates/index/posts.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package index

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 Posts(posts []*posts.Post) {
@layout.Base() {
<h1 class="text-xl">Recent posts</h1>
<ul class="list-disc">
for _, p := range posts {
<li>
@post.LinkWithTags(p)
</li>
}
</ul>
<p>
<a href="/">Back</a>
</p>
}
}
4 changes: 2 additions & 2 deletions internal/templates/index/view.templ
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ templ View(posts []*posts.Post, tags []string) {
,
}
@tag.Link(t)
}&nbsp;and <a href="/posts">more</a>.
}&nbsp;and <a href="/posts.html">more</a>.
</p>
}
<p>
If you're interested in exploring the source code of this site, it's available on <a href="https://github.com/pnmcosta/csta.dev" target="_blank">GitHub</a>.
If you're interested in the source code of this site, it's available on <a href="https://github.com/pnmcosta/csta.dev" target="_blank">GitHub</a>.
Feel free to explore and learn from it.
</p>
<p>
Expand Down
2 changes: 1 addition & 1 deletion internal/templates/layout/base.templ
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ templ Base() {
/>
</head>
<body class="bg-black font-mono">
<div class="container md:w-2/3 lg:w-3/5 xl:w-1/2 space-y-3 p-4 sm:p-6 lg:p-8 mb-8 sm:mb-4 text-lg text-stone-300">
<div class="container md:w-2/3 lg:w-3/5 xl:w-1/2 space-y-3 p-6 lg:p-8 mb-8 sm:mb-4 text-lg text-stone-300">
<a href="/" id="title" class="text-6xl lg:text-8xl my-6 font-bold text-nowrap">
<span class="visible">csta.dev</span>
<span>&lt;csta&#x2F;&gt;</span>
Expand Down
11 changes: 11 additions & 0 deletions internal/templates/post/link.templ
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ templ Link(post *posts.Post) {
<span class="text-base">{ DaysSince(post.Date) }</span>
}

templ LinkWithTags(post *posts.Post) {
@Link(post)
&nbsp;<span class="text-base">in</span>
for i, t := range post.Tags {
if i >0 {
,
}
<a href={ templ.SafeURL(path.Join("/", "tag", slug.Make(t), "/")) } class="text-base">{ t }</a>
}
}

func DaysSince(dur time.Time) string {
days := math.Floor(time.Now().UTC().
Sub(dur.UTC()).
Expand Down
7 changes: 5 additions & 2 deletions internal/templates/tag/view.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ templ View(tag string, posts []*posts.Post) {
@layout.Base() {
if len(posts) > 0 {
<h1 class="text-xl">Posts tagged "{ tag }"</h1>
<ul class="list-disc list-inside">
<ul class="list-disc">
for _, p := range posts {
<li>
@post.Link(p)
@post.Link(p)
</li>
}
</ul>
}
<p>
<a href="/">Back</a>
</p>
}
}
83 changes: 63 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path"
"sort"
"sync"
"time"

"github.com/gosimple/slug"
"github.com/pnmcosta/csta.dev/internal/posts"
Expand All @@ -22,14 +23,15 @@ type Post = posts.Post
var devFlag = flag.Bool("dev", false, "if true public folder will be served")

func main() {
start := time.Now().UTC()
flag.Parse()

posts := posts.ParsePosts()

// Output path.
rootPath := "public"
if err := os.Mkdir(rootPath, 0755); err != nil && os.IsNotExist(err) {
log.Fatalf("failed to create output directory: %v", err)
log.Fatalf("failed to create %q: %v", rootPath, err)
}

tags := map[string][]*Post{}
Expand All @@ -44,15 +46,15 @@ func main() {
// 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)
log.Printf("failed to create %q: %v\n", 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)
log.Printf("failed to create %q: %v\n", name, err)
return
}

Expand All @@ -62,9 +64,11 @@ func main() {
// 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)
log.Printf("failed to write %q: %v\n", name, err)
return
}

log.Printf("%s created", name)
}()

// note failure to render will still generate tag ref
Expand All @@ -88,24 +92,26 @@ func main() {
// 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)
log.Printf("failed to create %q: %v\n", 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)
log.Printf("failed to create %q: %v\n", name, 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)
log.Printf("failed to write %q: %v\n", name, err)
return
}

log.Printf("%s created", name)
}()
}

Expand All @@ -115,27 +121,64 @@ func main() {
wg.Wait()

// Create an index page.
name := path.Join(rootPath, "index.html")
f, err := os.Create(name)
if err != nil {
log.Fatalf("failed to create output file: %v", err)
}
wg.Add(1)
go func() {
defer wg.Done()
name := path.Join(rootPath, "index.html")
f, err := os.Create(name)
if err != nil {
log.Printf("failed to create %q: %v\n", name, err)
return
}

// Write it out.
err = index.View(posts, tagKeys).Render(context.Background(), f)
if err != nil {
log.Fatalf("failed to write index page: %v", err)
}
// Write it out.
err = index.View(posts, tagKeys).Render(context.Background(), f)
if err != nil {
log.Printf("failed to write %q: %v\n", name, err)
return
}

log.Printf("%s created", name)
}()

// Create a posts page.
wg.Add(1)
go func() {
defer wg.Done()

if len(posts) == 0 {
return
}

name := path.Join(rootPath, "posts.html")
f, err := os.Create(name)
if err != nil {
log.Printf("failed to create %q: %v\n", name, err)
return
}

// Write it out.
err = index.Posts(posts).Render(context.Background(), f)
if err != nil {
log.Printf("failed to write %q: %v\n", name, err)
return
}

log.Printf("%s created", name)
}()

wg.Wait()

log.Printf("took %dms\n", time.Now().UTC().Sub(start).Milliseconds())

if !*devFlag {
return
}

// Static serve only on dev
http.Handle("/", http.FileServer(http.Dir("./public")))
log.Println("Listening on 127.0.0.1:3000")
err = http.ListenAndServe("127.0.0.1:3000", nil)
if err != nil {
log.Println("Listening on http://127.0.0.1:3000")
if err := http.ListenAndServe("127.0.0.1:3000", nil); err != nil {
log.Fatal(err)
}
}

0 comments on commit a641975

Please sign in to comment.