Skip to content

Commit

Permalink
implemented serve_rest_api(); still need to remove Supervisor async c…
Browse files Browse the repository at this point in the history
…hanges
  • Loading branch information
cool-ant committed Sep 5, 2024
1 parent 3d1b417 commit 7addf73
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 28 deletions.
15 changes: 15 additions & 0 deletions rust/psibase/src/services/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[crate::service(name = "events", dispatch = false, psibase_mod = "crate")]
#[allow(non_snake_case, unused_variables)]
mod service {
fn setSchema(schema: &crate::Schema) {
unimplemented!()
}
fn addIndex(
db_id: crate::DbId,
service: crate::AccountNumber,
event: crate::MethodNumber,
column: u8,
) {
unimplemented!()
}
}
10 changes: 10 additions & 0 deletions rust/psibase/src/services/r_events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[crate::service(name = "revents", dispatch = false, psibase_mod = "crate")]
#[allow(non_snake_case, unused_variables)]
pub mod revents_interface {
use crate::HttpRequest;

#[action]
fn serveSys(request: HttpRequest) -> Option<crate::http::HttpReply> {
unimplemented!()
}
}
16 changes: 8 additions & 8 deletions services/user/Webmail/plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
mod bindings;

use bindings::exports::webmail::plugin::api::{Error, Guest as API};
use bindings::exports::webmail::plugin::queries::Guest as Query;
use bindings::exports::webmail::plugin::types::Message;
use bindings::host::common::server::Error as CommonError;
// use bindings::exports::webmail::plugin::queries::Guest as Query;
// use bindings::exports::webmail::plugin::types::Message;
// use bindings::host::common::server::Error as CommonError;
use bindings::host::common::server::*;
use psibase::fracpack::Pack;
use psibase::services::webmail;
Expand Down Expand Up @@ -34,10 +34,10 @@ impl API for WebmailPlugin {
}
}

impl Query for WebmailPlugin {
fn get_messages(sender: String, receiver: String) -> Result<Vec<Message>, CommonError> {
Ok(vec![])
}
}
// impl Query for WebmailPlugin {
// fn get_messages(sender: String, receiver: String) -> Result<Vec<Message>, CommonError> {
// Ok(vec![])
// }
// }

bindings::export!(WebmailPlugin with_types_in bindings);
2 changes: 1 addition & 1 deletion services/user/Webmail/plugin/wit/impl.wit
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package webmail:plugin;
world impl {
include host:common/imports;
export api;
export queries;
// export queries;
export types;
}
126 changes: 107 additions & 19 deletions services/user/Webmail/service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use std::collections::HashMap;

use psibase::services::accounts::Wrapper as AccountsSvc;
use psibase::services::r_events::Wrapper as REventsSvc;
use psibase::AccountNumber;
use psibase::HttpReply;
use psibase::HttpRequest;

fn validate_user(user: String) -> bool {
let acc = AccountNumber::from(user.as_str());
Expand All @@ -10,17 +15,100 @@ fn validate_user(user: String) -> bool {
AccountsSvc::call().exists(acc)
}

fn make_query(req: &HttpRequest, sql: &str) -> HttpRequest {
return HttpRequest {
host: req.host.clone(),
rootHost: req.rootHost.clone(),
method: String::from("POST"),
target: String::from("/sql"),
contentType: String::from("application/sql"),
body: psibase::Hex(sql.as_bytes().to_vec()),
};
}

fn parse_query(query: String) -> HashMap<String, String> {
let mut params: HashMap<String, String> = HashMap::new();

let itr = query.split("&");
for p in itr {
let idx = p.find("=").unwrap();
let kv = p.split_at(idx);
params.insert(kv.0.to_string(), kv.1.to_string());
}
params
}

fn serve_rest_api(request: &HttpRequest) -> Option<HttpReply> {
if request.method == "GET" {
if !request.target.starts_with("/messages") {
return None;
}

let queryStart = request.target.find('?');
if queryStart.is_none() {
return None;
}
let queryStart = queryStart.unwrap();

let query = request.target.split_at(queryStart + 1).1;
let params = crate::parse_query(String::from(query));

let mut s_clause = String::new();
let s_opt = params.get(&String::from("sender"));
if let Some(s) = s_opt {
if !validate_user(String::from(s)) {
return None;
}
s_clause = format!("sender = '{}'", s);
}

let mut r_clause = String::new();
let r_opt = params.get(&String::from("receiver"));
if let Some(r) = r_opt {
if !validate_user(String::from(r)) {
return None;
}
r_clause = format!("receiver = '{}'", r);
}

if s_opt.is_none() && r_opt.is_none() {
return None;
}

let mut where_clause: String = String::from("WHERE ");
if s_opt.is_some() {
where_clause += s_clause.as_str();
}
if s_opt.is_some() && r_opt.is_some() {
where_clause += " AND WHERE ";
}
if r_opt.is_some() {
where_clause += r_clause.as_str();
}

return REventsSvc::call().serveSys(make_query(
request,
format!(
"SELECT * FROM \"history.webmail.sent\" {} ORDER BY ROWID",
where_clause
)
.as_str(),
));
}
return None;
}

#[psibase::service]
mod service {
use async_graphql::{Object, SimpleObject};
use psibase::{
anyhow, check, get_sender, get_service, serve_content, serve_graphiql, serve_graphql,
serve_simple_ui, store_content, AccountNumber, Fracpack, HexBytes, HttpReply, HttpRequest,
SingletonKey, Table, ToSchema, WebContentRow,
serve_simple_ui, services, store_content, AccountNumber, Fracpack, HexBytes, HttpReply,
HttpRequest, SingletonKey, Table, ToSchema, WebContentRow,
};
use serde::{Deserialize, Serialize};

use crate::validate_user;
use crate::{serve_rest_api, validate_user};

// #[table(name = "InitTable")]
// #[derive(Fracpack, ToSchema, SimpleObject, Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -58,28 +146,28 @@ mod service {
#[event(history)]
pub fn sent(sender: AccountNumber, receiver: AccountNumber, subject: String, body: String) {}

struct Query;

#[Object]
impl Query {
async fn getMessages(&self) -> async_graphql::Result<String, async_graphql::Error> {
// let curr_val = InitTable::new().get_index_pk().get(&SingletonKey {});
// Ok(match curr_val {
// Some(val) => val.name,
// None => String::from("psibase"),
// })
Ok(String::from("placeholder"))
}
}
// struct Query;

// #[Object]
// impl Query {
// async fn getMessages(&self) -> async_graphql::Result<Vec<Message>>, async_graphql::Error> {
// // let curr_val = InitTable::new().get_index_pk().get(&SingletonKey {});
// // Ok(match curr_val {
// // Some(val) => val.name,
// // None => String::from("psibase"),
// // })
// Ok(String::from("placeholder"))
// }
// }

#[action]
#[allow(non_snake_case)]
fn serveSys(request: HttpRequest) -> Option<HttpReply> {
None.or_else(|| serve_content(&request, &WebContentTable::new()))
.or_else(|| serve_graphql(&request, Query))
.or_else(|| serve_graphiql(&request))
.or_else(|| serve_rest_api(&request))
// .or_else(|| serve_graphql(&request, Query))
// .or_else(|| serve_graphiql(&request))
.or_else(|| serve_simple_ui::<Wrapper>(&request))
// .or_else(|| serve_rest_api)
}

#[action]
Expand Down

0 comments on commit 7addf73

Please sign in to comment.