fork of indigo with slightly nicer lexgen
at main 1.2 kB view raw
1package did 2 3import ( 4 "context" 5 "fmt" 6 "time" 7 8 "github.com/whyrusleeping/go-did" 9) 10 11type Resolver interface { 12 GetDocument(ctx context.Context, didstr string) (*did.Document, error) 13 FlushCacheFor(did string) 14} 15 16type MultiResolver struct { 17 handlers map[string]Resolver 18} 19 20func NewMultiResolver() *MultiResolver { 21 return &MultiResolver{ 22 handlers: make(map[string]Resolver), 23 } 24} 25 26func (mr *MultiResolver) AddHandler(method string, res Resolver) { 27 mr.handlers[method] = res 28} 29 30func (mr *MultiResolver) FlushCacheFor(didstr string) { 31 pdid, err := did.ParseDID(didstr) 32 if err != nil { 33 return 34 } 35 36 method := pdid.Protocol() 37 38 res, ok := mr.handlers[method] 39 if !ok { 40 return 41 } 42 43 res.FlushCacheFor(didstr) 44} 45 46func (mr *MultiResolver) GetDocument(ctx context.Context, didstr string) (*did.Document, error) { 47 s := time.Now() 48 49 pdid, err := did.ParseDID(didstr) 50 if err != nil { 51 return nil, err 52 } 53 54 method := pdid.Protocol() 55 defer func() { 56 mrResolveDuration.WithLabelValues(method).Observe(time.Since(s).Seconds()) 57 }() 58 59 res, ok := mr.handlers[method] 60 if !ok { 61 return nil, fmt.Errorf("unknown did method: %q", method) 62 } 63 64 mrResolvedDidsTotal.WithLabelValues(method).Inc() 65 66 return res.GetDocument(ctx, didstr) 67}