updates to ev and webfinger
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Jon Lundy 2023-05-29 09:48:20 -06:00
parent 7c4c1521fd
commit 5b9b436125
Signed by untrusted user who does not match committer: xuu
GPG Key ID: C63E6D61F3035024
19 changed files with 559 additions and 57 deletions

View File

@ -1,23 +1,23 @@
root = "./cmd/ev" root = "."
testdata_dir = "data" testdata_dir = "testdata"
tmp_dir = "tmp" tmp_dir = "tmp"
[build] [build]
args_bin = [] args_bin = []
bin = "./tmp/main" bin = "./tmp/ev"
cmd = "go build -o ./tmp/main ./cmd/ev" cmd = "go build -o ./tmp/ev ./cmd/ev"
delay = 1000 delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"] exclude_dir = ["assets", "tmp", "vendor", "testdata", "data", "build"]
exclude_file = [] exclude_file = []
exclude_regex = ["_test.go"] exclude_regex = ["_test.go"]
exclude_unchanged = false exclude_unchanged = false
follow_symlink = false follow_symlink = false
full_bin = "" full_bin = ""
include_dir = [] include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"] include_ext = ["go", "tpl", "tmpl", "html", "css"]
kill_delay = "1s" kill_delay = "0s"
log = "build-errors.log" log = "build-errors.log"
send_interrupt = true send_interrupt = false
stop_on_error = true stop_on_error = true
[color] [color]

View File

@ -1,10 +1,13 @@
export PATH:=$(shell go env GOPATH)/bin:$(PATH) export PATH:=$(shell go env GOPATH)/bin:$(PATH)
export EV_DATA=mem: export EV_DATA=mem:
export EV_HTTP=:8080 export EV_HTTP=:8080
#export EV_TRACE_SAMPLE=always export WEBFINGER_DOMAINS=localhost
#export EV_TRACE_ENDPOINT=localhost:4318
.DEFAULT_GOAL := air
-include local.mk -include local.mk
air: gen air: gen
ifeq (, $(shell which air)) ifeq (, $(shell which air))
go install github.com/cosmtrek/air@latest go install github.com/cosmtrek/air@latest

1
app/twtxt/twtxt.go Normal file
View File

@ -0,0 +1 @@
package twtxt

View File

@ -12,8 +12,6 @@ type SubjectSet struct {
event.IsEvent `json:"-"` event.IsEvent `json:"-"`
} }
var _ event.Event = (*SubjectSet)(nil)
type SubjectDeleted struct { type SubjectDeleted struct {
Subject string `json:"subject"` Subject string `json:"subject"`
@ -29,6 +27,7 @@ type LinkSet struct {
HRef string `json:"href,omitempty"` HRef string `json:"href,omitempty"`
Titles map[string]string `json:"titles,omitempty"` Titles map[string]string `json:"titles,omitempty"`
Properties map[string]*string `json:"properties,omitempty"` Properties map[string]*string `json:"properties,omitempty"`
Template string `json:"template,omitempty"`
event.IsEvent `json:"-"` event.IsEvent `json:"-"`
} }

View File

@ -54,6 +54,7 @@ type Link struct {
HRef string `json:"href,omitempty"` HRef string `json:"href,omitempty"`
Titles map[string]string `json:"titles,omitempty"` Titles map[string]string `json:"titles,omitempty"`
Properties map[string]*string `json:"properties,omitempty"` Properties map[string]*string `json:"properties,omitempty"`
Template string `json:"template,omitempty"`
} }
type Links []*Link type Links []*Link
@ -198,6 +199,7 @@ func (a *JRD) ApplyEvent(events ...event.Event) {
link.Type = e.Type link.Type = e.Type
link.Titles = e.Titles link.Titles = e.Titles
link.Properties = e.Properties link.Properties = e.Properties
link.Template = e.Template
case *LinkDeleted: case *LinkDeleted:
a.Links = slice.FilterFn(func(link *Link) bool { return link.Index != e.Index }, a.Links...) a.Links = slice.FilterFn(func(link *Link) bool { return link.Index != e.Index }, a.Links...)
@ -249,8 +251,8 @@ func (a *JRD) OnClaims(jrd *JRD) error {
} }
for _, z := range slice.Align( for _, z := range slice.Align(
jrd.Links, a.Links, // old
a.Links, jrd.Links, // new
func(l, r *Link) bool { return l.Index < r.Index }, func(l, r *Link) bool { return l.Index < r.Index },
) { ) {
// Not in new == delete // Not in new == delete
@ -270,6 +272,7 @@ func (a *JRD) OnClaims(jrd *JRD) error {
HRef: link.HRef, HRef: link.HRef,
Titles: link.Titles, Titles: link.Titles,
Properties: link.Properties, Properties: link.Properties,
Template: link.Template,
}) })
continue continue
} }
@ -336,23 +339,20 @@ func (a *JRD) OnLinkSet(o, n *Link) error {
HRef: n.HRef, HRef: n.HRef,
Titles: n.Titles, Titles: n.Titles,
Properties: n.Properties, Properties: n.Properties,
Template: n.Template,
} }
// if n.Index != o.Index {
// fmt.Println(342)
// modified = true
// }
if n.Rel != o.Rel { if n.Rel != o.Rel {
fmt.Println(346)
modified = true modified = true
} }
if n.Type != o.Type { if n.Type != o.Type {
fmt.Println(350)
modified = true modified = true
} }
if n.HRef != o.HRef { if n.HRef != o.HRef {
fmt.Println(355) modified = true
}
if n.Template != o.Template {
fmt.Println(360, n.Template, o.Template, e.Template)
modified = true modified = true
} }
@ -368,8 +368,6 @@ func (a *JRD) OnLinkSet(o, n *Link) error {
slice.Zip(oKeys, slice.FromMapValues(o.Titles, oKeys)), slice.Zip(oKeys, slice.FromMapValues(o.Titles, oKeys)),
) { ) {
if z.Key != z.Value { if z.Key != z.Value {
fmt.Println(365)
modified = true modified = true
break break
} }
@ -389,15 +387,11 @@ func (a *JRD) OnLinkSet(o, n *Link) error {
curValue := z.Value curValue := z.Value
if newValue.Key != curValue.Key { if newValue.Key != curValue.Key {
fmt.Println(380, newValue.Key, curValue.Key)
modified = true modified = true
break break
} }
if !cmpPtr(newValue.Value, curValue.Value) { if !cmpPtr(newValue.Value, curValue.Value) {
fmt.Println(387)
modified = true modified = true
break break
} }

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,95 @@
/* Space out content a bit */
body {
padding-top: 20px;
padding-bottom: 20px;
}
/* Everything but the jumbotron gets side spacing for mobile first views */
.header,
.footer {
padding-right: 15px;
padding-left: 15px;
}
/* Custom page header */
.header {
padding-bottom: 20px;
border-bottom: 1px solid #e5e5e5;
}
/* Make the masthead heading the same height as the navigation */
.header h3 {
margin-top: 0;
margin-bottom: 0;
line-height: 40px;
}
/* Custom page footer */
.footer {
padding-top: 19px;
color: #777;
border-top: 1px solid #e5e5e5;
}
.panel-heading a {
color: white;
font-weight: bold;
}
.container-narrow > hr {
margin: 30px 0;
}
@media (prefers-color-scheme: dark) {
body, .panel-body {
color: white;
background-color: #222;
}
nav.navbar-default {
background-color: rgb(35, 29, 71);
}
.navbar-default .navbar-brand {
color: white;
}
.panel-primary, .list-group, .list-group-item {
color: white;
background-color: #16181c;
}
.table > tbody > tr.active > th, .table > tbody > tr.active > td {
background-color: rgb(35, 29, 71);
}
.table-striped > tbody > tr:nth-of-type(2n+1) {
background-color: rgb(35, 29, 71);
}
.panel pre {
color: white;
background-color: #16181c;
}
.panel .panel-primary > .panel-heading {
background-color: rgb(35, 29, 71);
}
.panel a {
color: cornflowerblue;
}
code {
color: white;
background-color: #282b32;
}
}
@import url(https://cdn.jsdelivr.net/npm/firacode@6.2.0/distr/fira_code.css);
code { font-family: 'Fira Code', monospace; }
@media (min-width: 100) {
.truncate {
width: 750px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.container {
width: 750px;
}
}

View File

@ -0,0 +1,28 @@
{{define "main"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{{template "meta" .}}
<title>👉 Webfinger 👈</title>
<link href="/webfinger/assets/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href="/webfinger/assets/webfinger.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<a class="navbar-brand" href="/webfinger">👉 Webfinger 👈</a>
</div>
</div>
</nav>
<div class=container>
{{template "content" .}}
</div>
</body>
</html>
{{end}}

View File

@ -0,0 +1,131 @@
{{template "main" .}}
{{define "meta"}}{{end}}
{{define "content"}}
<form method="GET">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">resource</span>
<input name="resource" class="form-control" placeholder="acct:..."/>
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Go!</button>
</span>
</div>
</form>
<br/>
{{ if ne .Err nil }}
<div class="alert alert-danger" role="alert">
{{ .Err }}
</div>
{{ end }}
{{ if ne .JRD nil }}
<div class="panel panel-primary">
<div class="panel-heading">Webfinger Result</div>
<table class="table">
<tr>
<th style="width:98px">Subject</th>
<td>
<div class="media">
<div class="media-body">
{{ .JRD.Subject }}
</div>
{{ with .JRD.GetLinkByRel "http://webfinger.net/rel/avatar" }}
{{ if ne . nil }}
<div class="media-left media-middle">
<div class="panel panel-default">
<div class="panel-body">
<img src="{{ .HRef }}" />
</div>
</div>
</div>
{{ end }}
{{ end }}
</div>
</td>
</tr>
{{if ne (len .JRD.Aliases) 0}}
<tr>
<th>Aliases</th>
<td>
<ul class="list-group">
{{ range .JRD.Aliases }}<li class="list-group-item">{{ . }}</li>
{{ end }}
</ul>
</td>
</tr>
{{ end }}
{{ if ne (len .JRD.Properties) 0 }}
<tr>
<th>Properties</th>
<td>
<div class="list-group truncate">
{{ range $key, $value := .JRD.Properties }}<div class="list-group-item">
<h5 class="list-group-item-heading" title="{{ $key }}">{{ propName $key }}</h5>
<code class="list-group-item-text">{{ $value }}</code>
</div>
{{ end }}
</div>
</td>
</tr>
{{ end }}
{{ if ne (len .JRD.Links) 0 }}
{{ range .JRD.Links }}
<tr class="active">
{{ if ne (len .Template) 0 }}
<th> Template </th>
<td>{{ .Template }}</td>
{{ else }}
<th> Link </th>
<td>{{ if ne (len .HRef) 0 }}<a href="{{ .HRef }}" target="_blank">{{ .HRef }}</a>{{ end }}</td>
{{ end }}
<tr>
<tr>
<th> Properties </th>
<td>
<div class="list-group">
<div class="list-group-item truncate">
<h5 class="list-group-item-heading">rel<h5>
<code class="list-group-item-text">{{ .Rel }}</code>
</div>
{{ if ne (len .Type) 0 }}<div class="list-group-item truncate">
<h5 class="list-group-item-heading">type</h5>
<code class="list-group-item-text">{{ .Type }}</code>
</div>
{{ end }}
{{ range $key, $value := .Properties }}<div class="list-group-item truncate">
<h5 class="list-group-item-heading" title="{{ $key }}">{{ propName $key }}</h5>
<code class="list-group-item-text">{{ $value }}</code>
</div>
{{ end }}
</div>
</td>
</tr>
{{ end }}
{{ end }}
</table>
</div>
{{ end }}
<div class="panel panel-primary">
<div class="panel-heading">Raw JRD</div>
<pre style="height: 15em; overflow-y: auto; border: 0px">
Status: {{ .Status }}
{{ .Body | printf "%s" }}
</pre>
</div>
{{end}}

View File

@ -3,13 +3,20 @@ package webfinger
import ( import (
"context" "context"
"crypto/ed25519" "crypto/ed25519"
"embed"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"html"
"html/template"
"io" "io"
"io/fs"
"log"
"net/http" "net/http"
"net/http/httptest"
"net/url" "net/url"
"os"
"strings" "strings"
"github.com/golang-jwt/jwt/v4" "github.com/golang-jwt/jwt/v4"
@ -19,6 +26,12 @@ import (
"go.sour.is/ev/pkg/set" "go.sour.is/ev/pkg/set"
) )
var (
//go:embed ui/*/*
files embed.FS
templates map[string]*template.Template
)
type service struct { type service struct {
es *ev.EventStore es *ev.EventStore
self set.Set[string] self set.Set[string]
@ -63,7 +76,13 @@ func New(ctx context.Context, es *ev.EventStore, opts ...Option) (*service, erro
return svc, nil return svc, nil
} }
func (s *service) RegisterHTTP(mux *http.ServeMux) {} func (s *service) RegisterHTTP(mux *http.ServeMux) {
a, _ := fs.Sub(files, "ui/assets")
assets := http.StripPrefix("/webfinger/assets/", http.FileServer(http.FS(a)))
mux.Handle("/webfinger", s.ui())
mux.Handle("/webfinger/assets/", assets)
}
func (s *service) RegisterWellKnown(mux *http.ServeMux) { func (s *service) RegisterWellKnown(mux *http.ServeMux) {
mux.Handle("/webfinger", lg.Htrace(s, "webfinger")) mux.Handle("/webfinger", lg.Htrace(s, "webfinger"))
} }
@ -153,6 +172,8 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
json.NewEncoder(os.Stdout).Encode(c.JRD)
for i := range c.JRD.Links { for i := range c.JRD.Links {
c.JRD.Links[i].Index = uint64(i) c.JRD.Links[i].Index = uint64(i)
} }
@ -307,37 +328,88 @@ func (s *service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
span.AddEvent("method not allow: " + r.Method) span.AddEvent("method not allow: " + r.Method)
} }
} }
func (s *service) ui() http.HandlerFunc {
loadTemplates()
return func(w http.ResponseWriter, r *http.Request) {
args := struct {
Req *http.Request
Status int
Body []byte
JRD *JRD
Err error
}{Status: http.StatusOK}
if r.URL.Query().Has("resource") {
args.Req, args.Err = http.NewRequestWithContext(r.Context(), http.MethodGet, r.URL.String(), nil)
if args.Err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
wr := httptest.NewRecorder()
s.ServeHTTP(wr, args.Req)
args.Status = wr.Code
switch wr.Code {
case http.StatusSeeOther:
res, err := http.DefaultClient.Get(wr.Header().Get("location"))
args.Err = err
if err == nil {
args.Status = res.StatusCode
args.Body, args.Err = io.ReadAll(res.Body)
}
case http.StatusOK:
args.Body, args.Err = io.ReadAll(wr.Body)
if args.Err == nil {
args.JRD, args.Err = ParseJRD(args.Body)
}
}
if args.Err == nil && args.Body != nil {
args.JRD, args.Err = ParseJRD(args.Body)
}
}
t := templates["home.go.tpl"]
err := t.Execute(w, args)
if err != nil {
log.Println(err)
}
}
}
func dec(s string) ([]byte, error) { func dec(s string) ([]byte, error) {
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
return base64.RawURLEncoding.DecodeString(s) return base64.RawURLEncoding.DecodeString(s)
} }
// func splitHostPort(hostPort string) (host, port string) { var funcMap = map[string]any{
// host = hostPort "propName": func(in string) string { return in[strings.LastIndex(in, "/")+1:] },
"escape": html.EscapeString,
}
// colon := strings.LastIndexByte(host, ':') func loadTemplates() error {
// if colon != -1 && validOptionalPort(host[colon:]) { if templates != nil {
// host, port = host[:colon], host[colon+1:] return nil
// } }
templates = make(map[string]*template.Template)
tmplFiles, err := fs.ReadDir(files, "ui/pages")
if err != nil {
return err
}
// if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") { for _, tmpl := range tmplFiles {
// host = host[1 : len(host)-1] if tmpl.IsDir() {
// } continue
}
pt := template.New(tmpl.Name())
pt.Funcs(funcMap)
pt, err = pt.ParseFS(files, "ui/pages/"+tmpl.Name(), "ui/layouts/*.go.tpl")
if err != nil {
log.Println(err)
// return return err
// } }
// func validOptionalPort(port string) bool { templates[tmpl.Name()] = pt
// if port == "" { }
// return true return nil
// } }
// if port[0] != ':' {
// return false
// }
// for _, b := range port[1:] {
// if b < '0' || b > '9' {
// return false
// }
// }
// return true
// }

17
cmd/ev/app.twtxt.go Normal file
View File

@ -0,0 +1,17 @@
package main
import (
"context"
"go.sour.is/ev/internal/lg"
"go.sour.is/ev/pkg/service"
)
var _ = apps.Register(50, func(ctx context.Context, svc *service.Harness) error {
_, span := lg.Span(ctx)
defer span.End()
span.AddEvent("Enable Twtxt")
return nil
})

View File

@ -38,7 +38,7 @@ var _ = apps.Register(50, func(ctx context.Context, svc *service.Harness) error
cache.SetDefault(s, true) cache.SetDefault(s, true)
return false return false
}) })
var withHostnames webfinger.WithHostnames = strings.Fields(env.Default(" ", "sour.is")) var withHostnames webfinger.WithHostnames = strings.Fields(env.Default("WEBFINGER_DOMAINS", "sour.is"))
wf, err := webfinger.New(ctx, eventstore, withCache, withHostnames) wf, err := webfinger.New(ctx, eventstore, withCache, withHostnames)
if err != nil { if err != nil {

View File

@ -178,6 +178,8 @@ func run(opts opts) error {
break break
} }
fmt.Fprintln(os.Stderr, jrd)
token, err := webfinger.NewSignedRequest(jrd, key) token, err := webfinger.NewSignedRequest(jrd, key)
if err != nil { if err != nil {
return err return err

View File

@ -62,14 +62,14 @@ func TestMain(m *testing.M) {
} }
} }
func TestGetHTTP(t *testing.T) { func TestE2EGetHTTP(t *testing.T) {
is := is.New(t) is := is.New(t)
res, err := http.DefaultClient.Get("http://[::1]:61234/.well-known/webfinger") res, err := http.DefaultClient.Get("http://[::1]:61234/.well-known/webfinger")
is.NoErr(err) is.NoErr(err)
is.Equal(res.StatusCode, http.StatusBadRequest) is.Equal(res.StatusCode, http.StatusBadRequest)
} }
func TestCreateResource(t *testing.T) { func TestE2ECreateResource(t *testing.T) {
is := is.New(t) is := is.New(t)
_, priv, err := ed25519.GenerateKey(nil) _, priv, err := ed25519.GenerateKey(nil)

View File

@ -0,0 +1,60 @@
package clean
import "encoding"
type EventLog[T, K, C comparable, E any] interface {
EventLog(T) List[K, C, E]
}
type EventStore[T, K, C comparable, E, A any] interface {
Bus[T, K, E]
EventLog[T, K, C, E]
Load(T, A) error
Store(A) error
Truncate(T) error
}
type Event[T, C comparable, V any] struct {
Topic T
Position C
Payload V
}
type codec interface {
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}
type aggr = struct{}
type evvent = Event[string, uint64, codec]
type evvee = EventStore[string, string, uint64, evvent, aggr]
type evvesub = Subscription[Event[string, uint64, codec]]
type PAGE = Page[string, string]
type LOG struct{}
var _ List[string, string, evvee] = (*LOG)(nil)
func (*LOG) First(n uint64, after string) ([]PAGE, error) { panic("N/A") }
func (*LOG) Last(n uint64, before string) ([]PAGE, error) { panic("N/A") }
type SUB struct{}
var _ evvesub = (*SUB)(nil)
func (*SUB) Recv() error { return nil }
func (*SUB) Events() []evvent { return nil }
func (*SUB) Close() {}
type EV struct{}
var _ evvee = (*EV)(nil)
func (*EV) Emit(topic string, event evvent) error { panic("N/A") }
func (*EV) EventLog(topic string) List[string, uint64, evvent] { panic("N/A") }
func (*EV) Subscribe(topic string, after uint64) evvesub { panic("N/A") }
func (*EV) Load(topic string, a aggr) error { panic("N/A") }
func (*EV) Store(a aggr) error { panic("N/A") }
func (*EV) Truncate(topic string) error { panic("N/A") }

View File

@ -0,0 +1,38 @@
package clean
type GPD[K comparable, V any] interface {
Get(...K) ([]V, error)
Put(K, V) error
Delete(K) error
}
type Edge[C, K comparable] struct {
Key K
Kursor C
}
type Page[C, K comparable] struct {
Edges Edge[C, K]
Start C
End C
Next bool
Prev bool
}
type List[K, C comparable, V any] interface {
First(n uint64, after C) ([]Page[C, K], error)
Last(n uint64, before C) ([]Page[C, K], error)
}
type Emitter[T comparable, E any] interface {
Emit(T, E) error
}
type Subscription[E any] interface {
Recv() error
Events() []E
Close()
}
type Subscriber[T comparable, E any] interface {
Subscribe(T, uint64) Subscription[E]
}
type Bus[T, K comparable, E any] interface {
Emitter[T, E]
Subscriber[T, E]
}

View File

@ -233,3 +233,33 @@ func (p *property[T]) SetEventMeta(x T) {
p.v = x p.v = x
} }
} }
func AsEvent[T any](e T) Event {
return &asEvent[T]{payload: e}
}
type asEvent [T any] struct {
payload T
IsEvent
}
func (e asEvent[T]) Payload() T {
return e.payload
}
type AGG interface{ApplyEvent(...Event)}
func AsAggregate[T AGG](e T) Aggregate {
return &asAggregate[T]{payload: e}
}
type asAggregate [T AGG] struct {
payload T
IsAggregate
}
func (e *asAggregate[T]) Payload() T {
return e.payload
}
func (e *asAggregate[T]) ApplyEvent(lis ...Event) {
e.payload.ApplyEvent(lis...)
}

View File

@ -55,3 +55,28 @@ func TestEventEncode(t *testing.T) {
is.Equal(lis[i], chk[i]) is.Equal(lis[i], chk[i])
} }
} }
type exampleAgg struct{ value string }
func (a *exampleAgg) ApplyEvent(lis ...event.Event) {
for _, e := range lis {
switch e := e.(type) {
case interface{ Payload() exampleEvSetValue }:
a.value = e.Payload().value
}
}
}
type exampleEvSetValue struct{ value string }
func TestApplyEventGeneric(t *testing.T) {
payload := &exampleAgg{}
var agg = event.AsAggregate(payload)
agg.ApplyEvent(event.NewEvents(
event.AsEvent(exampleEvSetValue{"hello"}),
)...)
is := is.New(t)
is.Equal(payload.value, "hello")
}