Skip to content

Commit

Permalink
chore(prose): experimental ssg works
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Jan 17, 2025
1 parent 87407ab commit e2630f9
Show file tree
Hide file tree
Showing 6 changed files with 411 additions and 167 deletions.
108 changes: 105 additions & 3 deletions cmd/scripts/prose-ssg/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package main

import (
"bufio"
"context"
"encoding/json"
"log/slog"
"sync"
"time"

"github.com/picosh/pico/db/postgres"
"github.com/picosh/pico/filehandlers"
"github.com/picosh/pico/prose"
"github.com/picosh/pico/shared"
"github.com/picosh/pico/shared/storage"
"github.com/picosh/utils/pipe"
)

func bail(err error) {
Expand All @@ -12,11 +22,63 @@ func bail(err error) {
}
}

func render(ssg *prose.SSG, ch chan string) {
var pendingFlushes sync.Map
tick := time.Tick(10 * time.Second)
for {
select {
case userID := <-ch:
ssg.Logger.Info("received request to generate blog", "userId", userID)
pendingFlushes.Store(userID, userID)
case <-tick:
ssg.Logger.Info("flushing ssg requests")
go func() {
pendingFlushes.Range(func(key, value any) bool {
pendingFlushes.Delete(key)
user, err := ssg.DB.FindUser(value.(string))
if err != nil {
ssg.Logger.Error("cannot find user", "err", err)
return true
}

bucket, err := ssg.Storage.GetBucket(shared.GetAssetBucketName(user.ID))
if err != nil {
ssg.Logger.Error("cannot find bucket", "err", err)
return true
}

err = ssg.ProseBlog(user, bucket)
if err != nil {
ssg.Logger.Error("cannot generate blog", "err", err)
}
return true
})
}()
}
}
}

func createSubProseDrain(ctx context.Context, logger *slog.Logger) *pipe.ReconnectReadWriteCloser {
info := shared.NewPicoPipeClient()
send := pipe.NewReconnectReadWriteCloser(
ctx,
logger,
info,
"sub to prose-drain",
"sub prose-drain -k",
100,
-1,
)
return send
}

func main() {
cfg := prose.NewConfigSite()
picoDb := postgres.NewDB(cfg.DbURL, cfg.Logger)
st, err := storage.NewStorageFS(cfg.Logger, cfg.StorageDir)
logger := cfg.Logger
picoDb := postgres.NewDB(cfg.DbURL, logger)
st, err := storage.NewStorageMinio(logger, cfg.MinioURL, cfg.MinioUser, cfg.MinioPass)
bail(err)

ssg := &prose.SSG{
Cfg: cfg,
DB: picoDb,
Expand All @@ -25,5 +87,45 @@ func main() {
TmplDir: "./prose/html",
StaticDir: "./prose/public",
}
bail(ssg.Prose())

ctx := context.Background()
drain := createSubProseDrain(ctx, cfg.Logger)

ch := make(chan string)
go render(ssg, ch)

for {
scanner := bufio.NewScanner(drain)
for scanner.Scan() {
var data filehandlers.SuccesHook

err := json.Unmarshal(scanner.Bytes(), &data)
if err != nil {
logger.Error("json unmarshal", "err", err)
continue
}

logger = logger.With(
"userId", data.UserID,
"filename", data.Filename,
"action", data.Action,
)

if data.Action == "delete" {
bucket, err := ssg.Storage.GetBucket(shared.GetAssetBucketName(data.UserID))
if err != nil {
ssg.Logger.Error("cannot find bucket", "err", err)
continue
}
err = st.DeleteObject(bucket, data.Filename)
if err != nil {
logger.Error("cannot delete object", "err", err)
continue
}
ch <- data.UserID
} else if data.Action == "create" || data.Action == "update" {
ch <- data.UserID
}
}
}
}
3 changes: 3 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ type Post struct {
MimeType string `json:"mime_type"`
Data PostData `json:"data"`
Tags []string `json:"tags"`

// computed
IsVirtual bool
}

type Paginate[T any] struct {
Expand Down
2 changes: 2 additions & 0 deletions pgs/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ var imgRegex = regexp.MustCompile("(.+.(?:jpg|jpeg|png|gif|webp|svg))(/.+)")
func (web *WebRouter) AssetRequest(w http.ResponseWriter, r *http.Request) {
fname := r.PathValue("fname")
if imgRegex.MatchString(fname) {
fmt.Println("HIT")
web.ImageRequest(w, r)
return
}
Expand All @@ -414,6 +415,7 @@ func (web *WebRouter) ImageRequest(w http.ResponseWriter, r *http.Request) {
if len(matches) >= 3 {
imgOpts = matches[2]
}
fmt.Println("ZZZ", fname, imgOpts)

opts, err := storage.UriToImgProcessOpts(imgOpts)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion prose/html/blog-aside.partial.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</div>

{{if .HasFilter}}
<a href={{.URL}}>clear filters</a>
<a href="/">clear filters</a>
{{end}}

<div class="posts group mt-2">
Expand Down
Loading

0 comments on commit e2630f9

Please sign in to comment.