chore: refactor
This commit is contained in:
7 files changed
+210
-179
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -10,14 +11,16 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"go.yarn.social/lextwt"
|
||||
"go.yarn.social/types"
|
||||
|
||||
"go.sour.is/xt/internal/otel"
|
||||
)
|
||||
|
||||
func httpServer(c *console, app *appState) error {
|
||||
ctx, span := otel.Span(c)
|
||||
func httpServer(ctx context.Context, app *appState) error {
|
||||
ctx, span := otel.Span(ctx)
|
||||
defer span.End()
|
||||
|
||||
span.AddEvent("start http server")
|
||||
@@ -110,11 +113,11 @@ func httpServer(c *console, app *appState) error {
|
||||
defer span.End()
|
||||
|
||||
args := make([]any, 0, 3)
|
||||
uriarg := ""
|
||||
uriarg := "1 = 1"
|
||||
uri := r.URL.Query().Get("uri")
|
||||
if uri != "" {
|
||||
feed_id := urlNS.UUID5(uri)
|
||||
uriarg = "and feed_id = ?"
|
||||
uriarg = "feed_id = ?"
|
||||
args = append(args, feed_id)
|
||||
}
|
||||
|
||||
@@ -123,11 +126,39 @@ func httpServer(c *console, app *appState) error {
|
||||
limit = v
|
||||
}
|
||||
|
||||
offset := 0
|
||||
if v, ok := strconv.Atoi(r.URL.Query().Get("offset")); ok == nil {
|
||||
var offset int64 = 0
|
||||
if v, ok := strconv.ParseInt(r.URL.Query().Get("offset"), 10, 64); ok == nil {
|
||||
offset = v
|
||||
}
|
||||
args = append(args, limit, offset)
|
||||
|
||||
var end int64
|
||||
err = db.QueryRowContext(ctx, `
|
||||
select count(*) n from twts
|
||||
JOIN (
|
||||
SELECT
|
||||
feed_id,
|
||||
nick,
|
||||
uri
|
||||
FROM feeds
|
||||
where state not in ('frozen', 'permanantly-dead') and `+uriarg+`
|
||||
) using (feed_id)
|
||||
where `+uriarg, args...).Scan(&end)
|
||||
span.RecordError(err)
|
||||
|
||||
if offset < 1 {
|
||||
offset += end
|
||||
}
|
||||
|
||||
limit = min(100, max(1, limit))
|
||||
offset = max(1, offset)
|
||||
|
||||
args = append(args, limit, offset-int64(limit))
|
||||
span.AddEvent("twts", trace.WithAttributes(
|
||||
attribute.Int("limit", limit),
|
||||
attribute.Int64("offset-end", offset),
|
||||
attribute.Int64("offset-start", offset-int64(limit)),
|
||||
attribute.Int64("max", end),
|
||||
))
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
rows, err := db.QueryContext(
|
||||
ctx,
|
||||
@@ -145,10 +176,9 @@ func httpServer(c *console, app *appState) error {
|
||||
nick,
|
||||
uri
|
||||
FROM feeds
|
||||
where state not in ('frozen', 'permanantly-dead')
|
||||
`+uriarg+`
|
||||
where state not in ('frozen', 'permanantly-dead') and `+uriarg+`
|
||||
) using (feed_id)
|
||||
order by ulid desc
|
||||
order by ulid asc
|
||||
limit ?
|
||||
offset ?
|
||||
`, args...,
|
||||
@@ -182,10 +212,14 @@ func httpServer(c *console, app *appState) error {
|
||||
}
|
||||
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 = append(preamble, lextwt.NewComment("# next = /api/plain/twts"+mkqry(uri, limit, offset+len(twts))))
|
||||
if offset > 0 {
|
||||
preamble = append(preamble, lextwt.NewComment("# prev = /api/plain/twts"+mkqry(uri, limit, offset-limit)))
|
||||
if next := offset + int64(len(twts)); next < end {
|
||||
preamble = append(preamble, lextwt.NewComment("# next = /api/plain/twts"+mkqry(uri, limit, next)))
|
||||
}
|
||||
if prev := offset - int64(limit); prev > 0 {
|
||||
preamble = append(preamble, lextwt.NewComment("# prev = /api/plain/twts"+mkqry(uri, limit, prev)))
|
||||
}
|
||||
|
||||
reg := lextwt.NewTwtRegistry(preamble, twts)
|
||||
@@ -221,7 +255,7 @@ func httpServer(c *console, app *appState) error {
|
||||
) using (feed_id)
|
||||
`+where+`
|
||||
order by nick, uri
|
||||
`,args...,
|
||||
`, args...,
|
||||
)
|
||||
if err != nil {
|
||||
span.RecordError(err)
|
||||
@@ -268,7 +302,7 @@ func httpServer(c *console, app *appState) error {
|
||||
Handler: http.DefaultServeMux,
|
||||
}
|
||||
|
||||
c.AddCancel(srv.Shutdown)
|
||||
app.AddCancel(srv.Shutdown)
|
||||
err = srv.ListenAndServe()
|
||||
if !errors.Is(err, http.ErrServerClosed) {
|
||||
span.RecordError(err)
|
||||
@@ -287,20 +321,17 @@ func notAny(s string, chars string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
func mkqry(uri string, limit, offset int) string {
|
||||
func mkqry(uri string, limit int, offset int64) string {
|
||||
qry := make([]string, 0, 3)
|
||||
|
||||
if uri != "" {
|
||||
qry = append(qry, "uri=" + uri)
|
||||
qry = append(qry, "uri="+uri)
|
||||
}
|
||||
|
||||
limit = min(100, max(1, limit))
|
||||
if limit != 100 {
|
||||
qry = append(qry, fmt.Sprint("limit=", limit))
|
||||
}
|
||||
|
||||
offset = max(0, offset)
|
||||
if offset != 0 {
|
||||
qry = append(qry, fmt.Sprint("offset=", offset))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user