summaryrefslogtreecommitdiffstats
path: root/adenosine-pds/src/lib.rs
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2022-11-10 19:05:16 -0800
committerBryan Newbold <bnewbold@robocracy.org>2022-11-10 19:05:16 -0800
commitf9ca910036cf9020c9fc36b57194d285a0396f9d (patch)
tree352ff752a2f8357148bae6811037826b1d7439dc /adenosine-pds/src/lib.rs
parent9bc88f57634af46d38ab811000ee9aa805ed82f6 (diff)
downloadadenosine-f9ca910036cf9020c9fc36b57194d285a0396f9d.tar.gz
adenosine-f9ca910036cf9020c9fc36b57194d285a0396f9d.zip
pds: implement homepage and did.json lookups
Diffstat (limited to 'adenosine-pds/src/lib.rs')
-rw-r--r--adenosine-pds/src/lib.rs47
1 files changed, 42 insertions, 5 deletions
diff --git a/adenosine-pds/src/lib.rs b/adenosine-pds/src/lib.rs
index 9a4aa8a..2f2c114 100644
--- a/adenosine-pds/src/lib.rs
+++ b/adenosine-pds/src/lib.rs
@@ -183,12 +183,16 @@ impl AtpService {
router!(request,
// ============= Web Interface
(GET) ["/"] => {
- let host = request.header("Host").unwrap_or("localhost");
- if Some(host.to_string()) == config.registration_domain {
- web_wrap(account_view_handler(&srv, &host, request))
+ if let Some(ref handle) = config.homepage_handle {
+ web_wrap(account_view_handler(&srv, handle, request))
} else {
- let view = GenericHomeView { domain: host.to_string() };
- Response::html(view.render().unwrap())
+ web_wrap(home_view_handler(&srv, request))
+ }
+ },
+ (GET) ["/.well-known/did.json"] => {
+ match did_doc_view_handler(&srv, request) {
+ Ok(resp) => resp,
+ Err(e) => web_wrap(Err(e)),
}
},
(GET) ["/about"] => {
@@ -675,6 +679,39 @@ fn xrpc_post_handler(
}
}
+fn home_view_handler(srv: &Mutex<AtpService>, request: &Request) -> Result<String> {
+ let host = request.header("Host").unwrap_or("localhost");
+
+ // check if the hostname resolves to a DID (account)
+ let did: Option<Did> = {
+ // this mutex lock should drop at the end of this block
+ let mut srv = srv.lock().or(Err(XrpcError::MutexPoisoned))?;
+ srv.atp_db.resolve_handle(host)?
+ };
+ if did.is_some() {
+ account_view_handler(&srv, &host, request)
+ } else {
+ let view = GenericHomeView {
+ domain: host.to_string(),
+ };
+ Ok(view.render()?)
+ }
+}
+
+fn did_doc_view_handler(srv: &Mutex<AtpService>, request: &Request) -> Result<Response> {
+ let host = request.header("Host").unwrap_or("localhost");
+ let mut srv = srv.lock().or(Err(XrpcError::MutexPoisoned))?;
+ if let Some(did) = srv.atp_db.resolve_handle(host)? {
+ if did.to_string().starts_with("did:web:") {
+ let did_doc = srv.atp_db.get_did_doc(&did)?;
+ return Ok(Response::json(&did_doc));
+ }
+ };
+ Err(XrpcError::NotFound(
+ "no did:web: account registered at this domain".to_string(),
+ ))?
+}
+
// TODO: did, collection, tid have already been parsed by this point
fn account_view_handler(
srv: &Mutex<AtpService>,