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