chore: helper function for preamble
This commit is contained in:
parent
00c97eb011
commit
b863a2786e
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.23.2
|
||||
require (
|
||||
github.com/mattn/go-sqlite3 v1.14.24
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.6.0
|
||||
go.yarn.social/lextwt v0.1.5-0.20250327005027-02d9b44de4dd
|
||||
go.yarn.social/lextwt v0.1.5-0.20250328015012-42a9ffb46fea
|
||||
)
|
||||
|
||||
require (
|
||||
|
2
go.sum
2
go.sum
@ -112,6 +112,8 @@ go.yarn.social/lextwt v0.0.0-20250213063805-7adc6ca07564 h1:z+IAMtxNKWcLNm9nLzJw
|
||||
go.yarn.social/lextwt v0.0.0-20250213063805-7adc6ca07564/go.mod h1:JOPCOh+3bHv+BMaFZpKzw6soiXbIlZD5b2f7YKDDjqk=
|
||||
go.yarn.social/lextwt v0.1.5-0.20250327005027-02d9b44de4dd h1:Np3zWtQ0GB9WhRFCPblaItLVtdy8Y35QKL+PUvRR/t8=
|
||||
go.yarn.social/lextwt v0.1.5-0.20250327005027-02d9b44de4dd/go.mod h1:P36NPegLbhbFa1A0JOLsDyIQcdM0zdmx8kPKACXry4A=
|
||||
go.yarn.social/lextwt v0.1.5-0.20250328015012-42a9ffb46fea h1:sbWswg5s8VaXB1hBQDs4KOBRBN2jPKtfFlcCQ3EThZk=
|
||||
go.yarn.social/lextwt v0.1.5-0.20250328015012-42a9ffb46fea/go.mod h1:P36NPegLbhbFa1A0JOLsDyIQcdM0zdmx8kPKACXry4A=
|
||||
go.yarn.social/types v0.0.0-20250108134258-ed75fa653ede h1:XV9tuDQ605xxH4qIQPRHM1bOa7k0rJZ2RqA5kz2Nun4=
|
||||
go.yarn.social/types v0.0.0-20250108134258-ed75fa653ede/go.mod h1:+xnDkQ0T0S8emxWIsvxlCAoyF8gBaj0q81hr/VrKc0c=
|
||||
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||
|
75
http.go
75
http.go
@ -19,6 +19,8 @@ import (
|
||||
"go.sour.is/xt/internal/otel"
|
||||
)
|
||||
|
||||
const iAmTheWatcher = "I am the Watcher. I am your guide through this vast new twtiverse."
|
||||
|
||||
func httpServer(ctx context.Context, app *appState) error {
|
||||
ctx, span := otel.Span(ctx)
|
||||
defer span.End()
|
||||
@ -52,27 +54,26 @@ func httpServer(ctx context.Context, app *appState) error {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
|
||||
rows, err := db.QueryContext(
|
||||
ctx,
|
||||
`SELECT
|
||||
feed_id,
|
||||
hash,
|
||||
conv,
|
||||
ctx, `
|
||||
SELECT
|
||||
feed_id,
|
||||
hash,
|
||||
conv,
|
||||
nick,
|
||||
uri,
|
||||
text
|
||||
FROM twts
|
||||
JOIN (
|
||||
SELECT
|
||||
feed_id,
|
||||
nick,
|
||||
uri,
|
||||
text
|
||||
FROM twts
|
||||
JOIN (
|
||||
SELECT
|
||||
feed_id,
|
||||
nick,
|
||||
uri
|
||||
FROM feeds
|
||||
) using (feed_id)
|
||||
WHERE
|
||||
hash = $1 or
|
||||
conv = $1
|
||||
order by ulid asc`,
|
||||
hash,
|
||||
uri
|
||||
FROM feeds
|
||||
) using (feed_id)
|
||||
WHERE
|
||||
hash = $1 or
|
||||
conv = $1
|
||||
order by ulid asc`, hash,
|
||||
)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
@ -102,7 +103,7 @@ func httpServer(ctx context.Context, app *appState) error {
|
||||
twts = append(twts, twt)
|
||||
}
|
||||
var preamble lextwt.Comments
|
||||
preamble = append(preamble, lextwt.NewComment("# self = /conv/"+hash))
|
||||
preamble = add(preamble, "self = /conv/%s", hash)
|
||||
|
||||
reg := lextwt.NewTwtRegistry(preamble, twts)
|
||||
reg.WriteTo(w)
|
||||
@ -205,20 +206,19 @@ func httpServer(ctx context.Context, app *appState) error {
|
||||
return
|
||||
}
|
||||
twter := types.NewTwter(o.Nick, o.URI)
|
||||
o.Text = strings.ReplaceAll(o.Text, "\n", "\u2028")
|
||||
twt, _ := lextwt.ParseLine(o.Text, &twter)
|
||||
twts = append(twts, twt)
|
||||
}
|
||||
var preamble lextwt.Comments
|
||||
preamble = append(preamble, lextwt.NewComment("# I am the Watcher. I am your guide through this vast new twtiverse."))
|
||||
|
||||
preamble = append(preamble, lextwt.NewComment(fmt.Sprint("# range = 1 ", end)))
|
||||
preamble = append(preamble, lextwt.NewComment("# self = /api/plain/twts"+mkqry(uri, limit, offset)))
|
||||
preamble = add(preamble, iAmTheWatcher)
|
||||
preamble = add(preamble, "")
|
||||
preamble = add(preamble, "range = 1 %d", end)
|
||||
preamble = add(preamble, "self = /api/plain/twts%s", mkqry(uri, limit, offset))
|
||||
if next := offset + int64(len(twts)); next < end {
|
||||
preamble = append(preamble, lextwt.NewComment("# next = /api/plain/twts"+mkqry(uri, limit, next)))
|
||||
preamble = add(preamble, "next = /api/plain/twts%s", mkqry(uri, limit, next))
|
||||
}
|
||||
if prev := offset - int64(limit); prev > 0 {
|
||||
preamble = append(preamble, lextwt.NewComment("# prev = /api/plain/twts"+mkqry(uri, limit, prev)))
|
||||
preamble = add(preamble, "prev = /api/plain/twts%s", mkqry(uri, limit, prev))
|
||||
}
|
||||
|
||||
reg := lextwt.NewTwtRegistry(preamble, twts)
|
||||
@ -249,8 +249,10 @@ func httpServer(ctx context.Context, app *appState) error {
|
||||
last_twt_on
|
||||
FROM feeds
|
||||
left join (
|
||||
select feed_id, max(strftime('%Y-%m-%dT%H:%M:%fZ', (substring(text, 1, instr(text, ' ')-1)))) last_twt_on
|
||||
from twts group by feed_id
|
||||
select
|
||||
feed_id,
|
||||
max(strftime('%Y-%m-%dT%H:%M:%fZ', (substring(text, 1, instr(text, ' ')-1)))) last_twt_on
|
||||
from twts group by feed_id
|
||||
) using (feed_id)
|
||||
`+where+`
|
||||
order by nick, uri
|
||||
@ -282,7 +284,11 @@ func httpServer(ctx context.Context, app *appState) error {
|
||||
nil,
|
||||
))
|
||||
}
|
||||
reg := lextwt.NewTwtRegistry(nil, twts)
|
||||
|
||||
var preamble lextwt.Comments
|
||||
preamble = add(preamble, iAmTheWatcher)
|
||||
|
||||
reg := lextwt.NewTwtRegistry(preamble, twts)
|
||||
reg.WriteTo(w)
|
||||
})
|
||||
|
||||
@ -341,3 +347,10 @@ func mkqry(uri string, limit int, offset int64) string {
|
||||
|
||||
return "?" + strings.Join(qry, "&")
|
||||
}
|
||||
|
||||
func add(preamble lextwt.Comments, text string, v ...any) lextwt.Comments {
|
||||
if len(v) > 0 {
|
||||
text = fmt.Sprintf(text, v...)
|
||||
}
|
||||
return append(preamble, lextwt.NewComment("# "+text))
|
||||
}
|
||||
|
2
internal/env/env.go
vendored
2
internal/env/env.go
vendored
@ -6,7 +6,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
func Default(key, def string) string {
|
||||
if v, ok := os.LookupEnv(key); ok {
|
||||
return v
|
||||
@ -17,6 +16,7 @@ func Default(key, def string) string {
|
||||
type secret struct {
|
||||
value string
|
||||
}
|
||||
|
||||
func (s secret) Secret() string {
|
||||
return s.value
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user