Skip to content

Commit

Permalink
Add html support
Browse files Browse the repository at this point in the history
  • Loading branch information
Toktar committed Jul 8, 2022
1 parent 31a8bdc commit 76e91cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func serve() {
requestedContentType := types.ContentType(accept)
if accept == "*/*" {
requestedContentType = types.DIDJSONLD
} else if strings.Contains(accept, string(types.HTML)) {
requestedContentType = types.HTML
}
log.Debug().Msgf("Requested content type: %s", requestedContentType)

Expand Down
17 changes: 14 additions & 3 deletions services/request_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,23 @@ func (rs RequestService) IsDidUrl(didUrl string) bool {
}

func (rs RequestService) ProcessDIDRequest(didUrl string, resolutionOptions types.ResolutionOption) (string, error) {
var result string
var err error
if rs.IsDidUrl(didUrl) {
log.Trace().Msgf("Dereferencing %s", didUrl)
return rs.prepareDereferencingResult(didUrl, types.DereferencingOption(resolutionOptions))
result, err = rs.prepareDereferencingResult(didUrl, types.DereferencingOption(resolutionOptions))
} else {
log.Trace().Msgf("Resolving %s", didUrl)
return rs.prepareResolutionResult(didUrl, resolutionOptions)
result, err = rs.prepareResolutionResult(didUrl, resolutionOptions)
}

if resolutionOptions.Accept == types.HTML {
return "<!DOCTYPE html><html><body><h1>Cheqd DID Resolver</h1><pre id=\"r\"></pre><script> var data = " +
result+ ";document.getElementById(\"r\").innerHTML = JSON.stringify(data, null, 4);" +
"</script></body></html>", err
}
return result, err

}

func (rs RequestService) prepareResolutionResult(did string, resolutionOptions types.ResolutionOption) (string, error) {
Expand Down Expand Up @@ -126,11 +136,12 @@ func (rs RequestService) Resolve(did string, resolutionOptions types.ResolutionO

if didResolutionMetadata.ContentType == types.DIDJSONLD || didResolutionMetadata.ContentType == types.JSONLD {
didDoc.Context = append(didDoc.Context, types.DIDSchemaJSONLD)
} else if didResolutionMetadata.ContentType == types.DIDJSON {
} else if didResolutionMetadata.ContentType == types.DIDJSON || didResolutionMetadata.ContentType == types.HTML {
didDoc.Context = []string{}
} else {
return types.DidResolution{}, fmt.Errorf("content type %s is not supported", didResolutionMetadata.ContentType)
}

return types.DidResolution{Did: didDoc, Metadata: metadata, ResolutionMetadata: didResolutionMetadata}, nil
}

Expand Down
1 change: 1 addition & 0 deletions types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
DIDJSON ContentType = "application/did+json"
DIDJSONLD ContentType = "application/did+ld+json"
JSONLD ContentType = "application/ld+json"
HTML ContentType = "text/html"
)

const (
Expand Down

0 comments on commit 76e91cb

Please sign in to comment.