1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"log/slog"
"net/http"
"github.com/a-h/templ"
_ "github.com/mattn/go-sqlite3"
"within.website/x/htmx"
"within.website/x/internal"
"within.website/x/xess"
)
//go:generate go tool templ generate
var (
bind = flag.String("bind", ":8069", "http port to bind on")
dbURL = flag.String("database-url", "", "path to sqlite database")
)
func main() {
internal.HandleStartup()
slog.Info("opening database", "database_url", *dbURL)
db, err := sql.Open("sqlite3", *dbURL)
if err != nil {
log.Fatal(err)
}
defer db.Close()
s := &Server{db: db}
mux := http.NewServeMux()
xess.Mount(mux)
htmx.Mount(mux)
mux.Handle("/{$}", templ.Handler(
Layout("Asbestos", Index()),
))
mux.HandleFunc("POST /search", s.searchHTMXPage)
fmt.Printf("http://localhost%s\n", *bind)
log.Fatal(http.ListenAndServe(*bind, mux))
}
type Server struct {
db *sql.DB
}
type Post struct {
ID int
Text string
CreatedAt string
Author string
URI string
HasImages bool
ReplyTo sql.NullString
BlueskyURL string
}
func (s *Server) getPostsByAuthor(author string) ([]Post, error) {
rows, err := s.db.Query("SELECT id, text, created_at, author, uri, has_images, reply_to, bluesky_url FROM posts WHERE author = ?", author)
if err != nil {
return nil, err
}
defer rows.Close()
var posts []Post
for rows.Next() {
var post Post
if err := rows.Scan(&post.ID, &post.Text, &post.CreatedAt, &post.Author, &post.URI, &post.HasImages, &post.ReplyTo, &post.BlueskyURL); err != nil {
return nil, err
}
posts = append(posts, post)
}
return posts, nil
}
func (s *Server) searchHTMXPage(w http.ResponseWriter, r *http.Request) {
query := r.FormValue("search")
posts, err := s.getPostsByAuthor(query)
if err != nil {
slog.Error("can't find posts", "author", query, "err", err)
templ.Handler(Error(err.Error())).ServeHTTP(w, r)
return
}
if len(posts) == 0 {
templ.Handler(allClear()).ServeHTTP(w, r)
return
}
templ.Handler(searchPage(query, posts)).ServeHTTPStreamed(w, r)
}
|