2024-02-15 17:10:39 +01:00
|
|
|
package resolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/bluesky-social/indigo/api"
|
|
|
|
"github.com/bluesky-social/indigo/did"
|
2024-02-18 16:34:14 +01:00
|
|
|
"github.com/rs/zerolog"
|
2024-02-15 17:10:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var Resolver did.Resolver
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
resolver := did.NewMultiResolver()
|
|
|
|
plcAddr := os.Getenv("ATP_PLC_ADDR")
|
|
|
|
if plcAddr == "" {
|
|
|
|
plcAddr = "https://plc.directory"
|
|
|
|
}
|
|
|
|
resolver.AddHandler("plc", &fallbackResolver{
|
|
|
|
resolvers: []did.Resolver{
|
|
|
|
&api.PLCServer{Host: plcAddr},
|
|
|
|
&api.PLCServer{Host: "https://plc.directory"},
|
|
|
|
}})
|
|
|
|
resolver.AddHandler("web", &did.WebResolver{})
|
|
|
|
|
|
|
|
Resolver = resolver
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetDocument(ctx context.Context, didstr string) (*did.Document, error) {
|
|
|
|
return Resolver.GetDocument(ctx, didstr)
|
|
|
|
}
|
|
|
|
|
|
|
|
type fallbackResolver struct {
|
|
|
|
resolvers []did.Resolver
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *fallbackResolver) GetDocument(ctx context.Context, didstr string) (*did.Document, error) {
|
2024-02-18 16:34:14 +01:00
|
|
|
log := zerolog.Ctx(ctx)
|
2024-02-15 17:10:39 +01:00
|
|
|
errs := []error{}
|
|
|
|
for _, res := range r.resolvers {
|
|
|
|
if d, err := res.GetDocument(ctx, didstr); err == nil {
|
|
|
|
return d, nil
|
2024-02-16 09:50:37 +01:00
|
|
|
} else {
|
2024-02-21 10:36:09 +01:00
|
|
|
log.Trace().Err(err).Str("plc", res.(*api.PLCServer).Host).
|
2024-02-18 16:34:14 +01:00
|
|
|
Msgf("Failed to resolve %q using %q: %s", didstr, res.(*api.PLCServer).Host, err)
|
2024-02-16 09:50:37 +01:00
|
|
|
errs = append(errs, err)
|
2024-02-15 17:10:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.Join(errs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *fallbackResolver) FlushCacheFor(did string) {
|
|
|
|
for _, res := range r.resolvers {
|
|
|
|
res.FlushCacheFor(did)
|
|
|
|
}
|
|
|
|
}
|