diff options
Diffstat (limited to 'internal/api/server.go')
| -rw-r--r-- | internal/api/server.go | 96 |
1 files changed, 55 insertions, 41 deletions
diff --git a/internal/api/server.go b/internal/api/server.go index 9895a65..284c2b2 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -479,43 +479,15 @@ func (s *Server) handlePLCCompatibility(w http.ResponseWriter, r *http.Request) } func (s *Server) handleGetDIDCompatibility(w http.ResponseWriter, r *http.Request, did string) { - var state types.StateV1 - if s.ingestor != nil { - resolvedState, err := s.ingestor.ResolveState(r.Context(), did) - if err != nil { - if errors.Is(err, ingest.ErrDIDNotFound) { - writeCompatibilityErr(w, http.StatusNotFound, "DID not registered: "+did) - - return - } - - if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { - writeCompatibilityErr(w, http.StatusGatewayTimeout, err.Error()) - - return - } - - writeCompatibilityErr(w, http.StatusInternalServerError, err.Error()) - - return - } - - state = resolvedState - } else { - stateVal, ok, err := s.store.GetState(did) - if err != nil { - writeCompatibilityErr(w, http.StatusInternalServerError, err.Error()) - - return - } - - if !ok { - writeCompatibilityErr(w, http.StatusNotFound, "DID not registered: "+did) + state, ok := s.resolveCompatibilityState(w, r, did) + if !ok { + return + } - return - } + if isTombstonedDIDDocument(state.DIDDocument) { + writeCompatibilityErr(w, http.StatusNotFound, "DID not available: "+did) - state = stateVal + return } data, err := s.ingestor.LoadCurrentPLCData(r.Context(), did) @@ -539,11 +511,7 @@ func (s *Server) handleGetDIDCompatibility(w http.ResponseWriter, r *http.Reques } status := http.StatusOK - deactivated := isTombstonedDIDDocument(state.DIDDocument) - - if deactivated { - status = http.StatusGone - } + deactivated := false writeJSONWithContentType(w, status, "application/did+ld+json", buildPLCDIDDocument(did, data, deactivated)) } @@ -666,6 +634,17 @@ func (s *Server) handleGetDIDDataCompatibility(w http.ResponseWriter, r *http.Re return } + state, ok := s.resolveCompatibilityState(w, r, did) + if !ok { + return + } + + if isTombstonedDIDDocument(state.DIDDocument) { + writeCompatibilityErr(w, http.StatusNotFound, "DID not available: "+did) + + return + } + data, err := s.ingestor.LoadCurrentPLCData(r.Context(), did) if err != nil { @@ -689,6 +668,41 @@ func (s *Server) handleGetDIDDataCompatibility(w http.ResponseWriter, r *http.Re writeJSONWithContentType(w, http.StatusOK, "application/json", data) } +func (s *Server) resolveCompatibilityState(w http.ResponseWriter, r *http.Request, did string) (types.StateV1, bool) { + if s.ingestor != nil { + state, err := s.ingestor.ResolveState(r.Context(), did) + if err != nil { + if errors.Is(err, ingest.ErrDIDNotFound) { + writeCompatibilityErr(w, http.StatusNotFound, "DID not registered: "+did) + return types.StateV1{}, false + } + + if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { + writeCompatibilityErr(w, http.StatusGatewayTimeout, err.Error()) + return types.StateV1{}, false + } + + writeCompatibilityErr(w, http.StatusInternalServerError, err.Error()) + return types.StateV1{}, false + } + + return state, true + } + + stateVal, found, err := s.store.GetState(did) + if err != nil { + writeCompatibilityErr(w, http.StatusInternalServerError, err.Error()) + return types.StateV1{}, false + } + + if !found { + writeCompatibilityErr(w, http.StatusNotFound, "DID not registered: "+did) + return types.StateV1{}, false + } + + return stateVal, true +} + func (s *Server) handleExportCompatibility(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { w.Header().Set("Allow", http.MethodGet) @@ -964,7 +978,7 @@ func extractServicesMap(v any) map[string]map[string]string { } func writeCompatibilityErr(w http.ResponseWriter, code int, message string) { - writeJSON(w, code, map[string]any{"message": message}) + writeJSONWithContentType(w, code, "application/json; charset=utf-8", map[string]any{"message": message}) } func (s *Server) withTimeout(next http.Handler) http.Handler { |