this repo has no description

fix resolver caching

Akshay a506c9ff 56dc2ac0

Changed files
+66 -50
appview
cmd
repoguard
knotserver
-11
appview/auth/auth.go
··· 8 8 9 9 comatproto "github.com/bluesky-social/indigo/api/atproto" 10 10 "github.com/bluesky-social/indigo/atproto/identity" 11 - "github.com/bluesky-social/indigo/atproto/syntax" 12 11 "github.com/bluesky-social/indigo/xrpc" 13 12 "github.com/gorilla/sessions" 14 13 "github.com/sotangled/tangled/appview" ··· 31 30 func Make() (*Auth, error) { 32 31 store := sessions.NewCookieStore([]byte(appview.SessionCookieSecret)) 33 32 return &Auth{store}, nil 34 - } 35 - 36 - func ResolveIdent(ctx context.Context, arg string) (*identity.Identity, error) { 37 - id, err := syntax.ParseAtIdentifier(arg) 38 - if err != nil { 39 - return nil, err 40 - } 41 - 42 - dir := identity.DefaultDirectory() 43 - return dir.Lookup(ctx, *id) 44 33 } 45 34 46 35 func (a *Auth) CreateInitialSession(ctx context.Context, resolved *identity.Identity, appPassword string) (*comatproto.ServerCreateSession_Output, error) {
+27
appview/resolver.go
··· 1 + package appview 2 + 3 + import ( 4 + "context" 5 + 6 + "github.com/bluesky-social/indigo/atproto/identity" 7 + "github.com/bluesky-social/indigo/atproto/syntax" 8 + ) 9 + 10 + type Resolver struct { 11 + directory identity.Directory 12 + } 13 + 14 + func NewResolver() *Resolver { 15 + return &Resolver{ 16 + directory: identity.DefaultDirectory(), 17 + } 18 + } 19 + 20 + func (r *Resolver) ResolveIdent(ctx context.Context, arg string) (*identity.Identity, error) { 21 + id, err := syntax.ParseAtIdentifier(arg) 22 + if err != nil { 23 + return nil, err 24 + } 25 + 26 + return r.directory.Lookup(ctx, *id) 27 + }
+20 -14
appview/state/middleware.go
··· 113 113 }) 114 114 } 115 115 116 - func ResolveIdent(next http.Handler) http.Handler { 117 - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 118 - didOrHandle := chi.URLParam(req, "user") 116 + func ResolveIdent(s *State) Middleware { 117 + return func(next http.Handler) http.Handler { 118 + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { 119 + start := time.Now() 120 + didOrHandle := chi.URLParam(req, "user") 119 121 120 - log.Println(didOrHandle) 121 - id, err := auth.ResolveIdent(req.Context(), didOrHandle) 122 - if err != nil { 123 - // invalid did or handle 124 - log.Println("failed to resolve did/handle") 125 - w.WriteHeader(http.StatusNotFound) 126 - return 127 - } 122 + log.Println(didOrHandle) 123 + id, err := s.resolver.ResolveIdent(req.Context(), didOrHandle) 124 + if err != nil { 125 + // invalid did or handle 126 + log.Println("failed to resolve did/handle") 127 + w.WriteHeader(http.StatusNotFound) 128 + return 129 + } 130 + 131 + ctx := context.WithValue(req.Context(), "resolvedId", *id) 128 132 129 - ctx := context.WithValue(req.Context(), "resolvedId", *id) 130 - next.ServeHTTP(w, req.WithContext(ctx)) 131 - }) 133 + elapsed := time.Since(start) 134 + log.Println("Execution time:", elapsed) 135 + next.ServeHTTP(w, req.WithContext(ctx)) 136 + }) 137 + } 132 138 } 133 139 134 140 func ResolveRepoKnot(s *State) Middleware {
+14 -6
appview/state/state.go
··· 30 30 enforcer *rbac.Enforcer 31 31 tidClock *syntax.TIDClock 32 32 pages *pages.Pages 33 + resolver *appview.Resolver 33 34 } 34 35 35 36 func Make() (*State, error) { ··· 52 53 53 54 pgs := pages.NewPages() 54 55 55 - return &State{db, auth, enforcer, clock, pgs}, nil 56 + resolver := appview.NewResolver() 57 + 58 + state := &State{ 59 + db, 60 + auth, enforcer, clock, pgs, resolver, 61 + } 62 + 63 + return state, nil 56 64 } 57 65 58 66 func (s *State) TID() string { ··· 76 84 fmt.Println("handle", handle) 77 85 fmt.Println("app_password", appPassword) 78 86 79 - resolved, err := auth.ResolveIdent(ctx, handle) 87 + resolved, err := s.resolver.ResolveIdent(ctx, handle) 80 88 if err != nil { 81 89 log.Printf("resolving identity: %s", err) 82 90 http.Redirect(w, r, "/login", http.StatusSeeOther) ··· 169 177 return 170 178 } 171 179 172 - id, err := auth.ResolveIdent(r.Context(), user) 180 + id, err := s.resolver.ResolveIdent(r.Context(), user) 173 181 if err != nil { 174 182 w.WriteHeader(http.StatusInternalServerError) 175 183 return ··· 415 423 return 416 424 } 417 425 418 - memberIdent, err := auth.ResolveIdent(r.Context(), memberDid) 426 + memberIdent, err := s.resolver.ResolveIdent(r.Context(), memberDid) 419 427 if err != nil { 420 428 w.Write([]byte("failed to resolve member did to a handle")) 421 429 return ··· 556 564 return 557 565 } 558 566 559 - ident, err := auth.ResolveIdent(r.Context(), didOrHandle) 567 + ident, err := s.resolver.ResolveIdent(r.Context(), didOrHandle) 560 568 if err != nil { 561 569 log.Printf("resolving identity: %s", err) 562 570 w.WriteHeader(http.StatusNotFound) ··· 597 605 // strip @ from user 598 606 r.Use(StripLeadingAt) 599 607 600 - r.With(ResolveIdent).Route("/{user}", func(r chi.Router) { 608 + r.With(ResolveIdent(s)).Route("/{user}", func(r chi.Router) { 601 609 r.Get("/", s.ProfilePage) 602 610 r.With(ResolveRepoKnot(s)).Route("/{repo}", func(r chi.Router) { 603 611 r.Get("/", s.RepoIndex)
+3 -17
cmd/repoguard/main.go
··· 9 9 "net/url" 10 10 "os" 11 11 "os/exec" 12 - "path" 13 12 "path/filepath" 14 13 "strings" 15 14 "time" 16 15 17 - "github.com/sotangled/tangled/appview/auth" 16 + "github.com/sotangled/tangled/appview" 18 17 ) 19 18 20 19 var ( ··· 136 135 } 137 136 138 137 func resolveToDid(didOrHandle string) string { 139 - ident, err := auth.ResolveIdent(context.Background(), didOrHandle) 138 + resolver := appview.NewResolver() 139 + ident, err := resolver.ResolveIdent(context.Background(), didOrHandle) 140 140 if err != nil { 141 141 exitWithLog(fmt.Sprintf("error resolving handle: %v", err)) 142 142 } 143 143 144 144 // did:plc:foobarbaz/repo 145 145 return ident.DID.String() 146 - } 147 - 148 - func handleToDid(handlePath string) string { 149 - handle := path.Dir(handlePath) 150 - 151 - ident, err := auth.ResolveIdent(context.Background(), handle) 152 - if err != nil { 153 - exitWithLog(fmt.Sprintf("error resolving handle: %v", err)) 154 - } 155 - 156 - // did:plc:foobarbaz/repo 157 - didPath := filepath.Join(ident.DID.String(), path.Base(handlePath)) 158 - 159 - return didPath 160 146 } 161 147 162 148 func initLogger() {
+2 -2
knotserver/file.go
··· 7 7 "net/http" 8 8 "strings" 9 9 10 - "github.com/sotangled/tangled/knotserver/git" 10 + "github.com/sotangled/tangled/types" 11 11 ) 12 12 13 - func (h *Handle) listFiles(files []git.NiceTree, data map[string]any, w http.ResponseWriter) { 13 + func (h *Handle) listFiles(files []types.NiceTree, data map[string]any, w http.ResponseWriter) { 14 14 data["files"] = files 15 15 16 16 writeJSON(w, data)