From 43ce8ecdc134d6ec4a4d23dbb2c15c1de41b3e19 Mon Sep 17 00:00:00 2001 From: thisisaaronland Date: Tue, 29 Jun 2021 23:49:07 -0700 Subject: [PATCH] rename things per issue #1 --- cmd/lookupd/main.go | 2 +- repo/cache.go | 94 ++++++++++++++++++++++++++++++++++++++++++ repo/cache/docstore.go | 1 - repo/resolver.go | 90 ---------------------------------------- 4 files changed, 95 insertions(+), 92 deletions(-) delete mode 100644 repo/cache/docstore.go delete mode 100644 repo/resolver.go diff --git a/cmd/lookupd/main.go b/cmd/lookupd/main.go index 962dff96..626890af 100644 --- a/cmd/lookupd/main.go +++ b/cmd/lookupd/main.go @@ -5,7 +5,7 @@ import ( ) import ( - _ "github.com/aaronland/gocloud-blob-s3" + _ "github.com/aaronland/gocloud-blob-s3" _ "github.com/whosonfirst/go-cache-blob" _ "github.com/whosonfirst/go-whosonfirst-findingaid/repo" ) diff --git a/repo/cache.go b/repo/cache.go index 707e99a1..7e6f5a87 100644 --- a/repo/cache.go +++ b/repo/cache.go @@ -1,12 +1,106 @@ package repo import ( + "context" + "encoding/json" + "errors" "fmt" + "github.com/whosonfirst/go-cache" + "github.com/whosonfirst/go-whosonfirst-findingaid" "github.com/whosonfirst/go-whosonfirst-uri" + _ "log" + "net/url" "path/filepath" "strings" ) +// CacheResolver is a struct that implements the findingaid.Resolver interface for information about Who's On First repositories. +type CacheResolver struct { + findingaid.Resolver + cache cache.Cache +} + +func init() { + + ctx := context.Background() + + schemes := []string{ + "repo", // deprecated + "repo-cache", + } + + for _, s := range schemes { + err := findingaid.RegisterResolver(ctx, s, NewCacheResolver) + + if err != nil { + panic(err) + } + } +} + +// NewCacheResolver returns a findingaid.Resolver instance for exposing information about Who's On First repositories +func NewCacheResolver(ctx context.Context, uri string) (findingaid.Resolver, error) { + + u, err := url.Parse(uri) + + if err != nil { + return nil, err + } + + q := u.Query() + + cache_uri := q.Get("cache") + + if cache_uri == "" { + return nil, errors.New("Missing cache URI") + } + + _, err = url.Parse(cache_uri) + + if err != nil { + return nil, err + } + + c, err := cache.NewCache(ctx, cache_uri) + + if err != nil { + return nil, err + } + + fa := &CacheResolver{ + cache: c, + } + + return fa, nil +} + +// ResolveURI will return 'repo.FindingAidResponse' for 'str_response' if it present in the finding aid. +func (fa *CacheResolver) ResolveURI(ctx context.Context, str_uri string) (interface{}, error) { + + key, err := cacheKeyFromURI(str_uri) + + if err != nil { + return nil, err + } + + fh, err := fa.cache.Get(ctx, key) + + if err != nil { + return nil, err + } + + var rsp *FindingAidResponse + + dec := json.NewDecoder(fh) + err = dec.Decode(&rsp) + + if err != nil { + return nil, err + } + + return rsp, nil +} + func cacheKeyFromURI(str_uri string) (string, error) { id, uri_args, err := uri.ParseURI(str_uri) diff --git a/repo/cache/docstore.go b/repo/cache/docstore.go deleted file mode 100644 index 08bf029d..00000000 --- a/repo/cache/docstore.go +++ /dev/null @@ -1 +0,0 @@ -package cache diff --git a/repo/resolver.go b/repo/resolver.go deleted file mode 100644 index 68cc59d6..00000000 --- a/repo/resolver.go +++ /dev/null @@ -1,90 +0,0 @@ -package repo - -import ( - "context" - "encoding/json" - "errors" - "github.com/whosonfirst/go-cache" - "github.com/whosonfirst/go-whosonfirst-findingaid" - _ "log" - "net/url" -) - -// RepoResolver is a struct that implements the findingaid.Resolver interface for information about Who's On First repositories. -type RepoResolver struct { - findingaid.Resolver - cache cache.Cache -} - -func init() { - - ctx := context.Background() - err := findingaid.RegisterResolver(ctx, "repo", NewRepoResolver) - - if err != nil { - panic(err) - } -} - -// NewRepoResolver returns a findingaid.Resolver instance for exposing information about Who's On First repositories -func NewRepoResolver(ctx context.Context, uri string) (findingaid.Resolver, error) { - - u, err := url.Parse(uri) - - if err != nil { - return nil, err - } - - q := u.Query() - - cache_uri := q.Get("cache") - - if cache_uri == "" { - return nil, errors.New("Missing cache URI") - } - - _, err = url.Parse(cache_uri) - - if err != nil { - return nil, err - } - - c, err := cache.NewCache(ctx, cache_uri) - - if err != nil { - return nil, err - } - - fa := &RepoResolver{ - cache: c, - } - - return fa, nil -} - -// ResolveURI will return 'repo.FindingAidResponse' for 'str_response' if it present in the finding aid. -func (fa *RepoResolver) ResolveURI(ctx context.Context, str_uri string) (interface{}, error) { - - key, err := cacheKeyFromURI(str_uri) - - if err != nil { - return nil, err - } - - fh, err := fa.cache.Get(ctx, key) - - if err != nil { - return nil, err - } - - var rsp *FindingAidResponse - - dec := json.NewDecoder(fh) - err = dec.Decode(&rsp) - - if err != nil { - return nil, err - } - - return rsp, nil -}