use mimalloc::MiMalloc;
#[global_allocator]
static MEMALLOC: MiMalloc = MiMalloc;
use axum::{
extract::{Extension, Form, Multipart, Path, Query},
routing::get,
AddExtensionLayer, Router,
};
use rustdb::{
c_value, check_types, standard_builtins, AccessPagedData, AtomicFile, Block, BuiltinMap, CExp,
CExpPtr, CompileFunc, DataKind, Database, EvalEnv, Expr, GenTransaction, Part, SharedPagedData,
SimpleFileStorage, Value, INITSQL,
};
use std::{collections::BTreeMap, rc::Rc, sync::Arc, thread};
use tokio::sync::{mpsc, oneshot};
use tower::ServiceBuilder;
use tower_cookies::{CookieManagerLayer, Cookies};
struct ServerTrans {
pub x: Box<GenTransaction>,
}
impl ServerTrans {
pub fn new() -> Self {
Self {
x: Box::new(GenTransaction::new()),
}
}
}
struct ServerMessage {
pub st: ServerTrans,
pub tx: oneshot::Sender<ServerTrans>,
}
struct SharedState {
tx: mpsc::Sender<ServerMessage>,
spd: Arc<SharedPagedData>,
bmap: Arc<BuiltinMap>,
}
#[tokio::main]
async fn main() {
let file = Box::new(SimpleFileStorage::new("..\\test.rustdb"));
let upd = Box::new(SimpleFileStorage::new("..\\test.upd"));
let stg = Box::new(AtomicFile::new(file, upd));
let spd = Arc::new(SharedPagedData::new(stg));
let mut bmap = BuiltinMap::new();
standard_builtins(&mut bmap);
let list = [("ARGON", DataKind::Binary, CompileFunc::Value(c_argon))];
for (name, typ, cf) in list {
bmap.insert(name.to_string(), (typ, cf));
}
let bmap = Arc::new(bmap);
let wapd = AccessPagedData::new_writer(spd.clone());
let (tx, mut rx) = mpsc::channel::<ServerMessage>(1);
let (log_tx, log_rx) = std::sync::mpsc::channel::<String>();
let ss = Arc::new(SharedState {
tx,
spd,
bmap: bmap.clone(),
});
thread::spawn(move || {
log_loop(log_rx);
});
thread::spawn(move || {
let db = Database::new(wapd, INITSQL, bmap);
loop {
let mut sm = rx.blocking_recv().unwrap();
db.run_timed("EXEC web.Main()", &mut *sm.st.x);
let updates = db.save();
if updates > 0 {
println!("Pages updated={}", updates);
let ser = serde_json::to_string(&sm.st.x.qy).unwrap();
let _err = log_tx.send(ser);
}
let _x = sm.tx.send(sm.st);
}
});
let app = Router::new().route("/*key", get(h_get).post(h_post)).layer(
ServiceBuilder::new()
.layer(CookieManagerLayer::new())
.layer(AddExtensionLayer::new(ss)),
);
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
async fn h_get(
state: Extension<Arc<SharedState>>,
path: Path<String>,
params: Query<BTreeMap<String, String>>,
cookies: Cookies,
) -> ServerTrans {
let mut sq = ServerTrans::new();
sq.x.qy.path = path.0;
sq.x.qy.params = params.0;
sq.x.qy.cookies = map_cookies(cookies);
let blocking_task = tokio::task::spawn_blocking(move || {
let apd = AccessPagedData::new_reader(state.spd.clone());
let db = Database::new(apd, "", state.bmap.clone());
db.run_timed("EXEC web.Main()", &mut *sq.x);
sq
});
blocking_task.await.unwrap()
}
async fn h_post(
state: Extension<Arc<SharedState>>,
path: Path<String>,
params: Query<BTreeMap<String, String>>,
cookies: Cookies,
form: Option<Form<BTreeMap<String, String>>>,
multipart: Option<Multipart>,
) -> ServerTrans {
let mut st = ServerTrans::new();
st.x.qy.path = path.0;
st.x.qy.params = params.0;
st.x.qy.cookies = map_cookies(cookies);
if let Some(Form(form)) = form {
st.x.qy.form = form;
} else {
st.x.qy.parts = map_parts(multipart).await;
}
let (tx, rx) = oneshot::channel::<ServerTrans>();
let _err = state.tx.send(ServerMessage { st, tx }).await;
let result = rx.await.unwrap();
result
}
use axum::{
body::{boxed, BoxBody, Full},
http::{header::HeaderName, status::StatusCode, HeaderValue, Response},
response::IntoResponse,
};
impl IntoResponse for ServerTrans {
fn into_response(self) -> Response<BoxBody> {
let mybody = boxed(Full::from(self.x.rp.output));
let mut res = Response::builder().body(mybody).unwrap();
*res.status_mut() = StatusCode::from_u16(self.x.rp.status_code).unwrap();
for (name, value) in &self.x.rp.headers {
res.headers_mut().insert(
HeaderName::from_lowercase(name.as_bytes()).unwrap(),
HeaderValue::from_str(value).unwrap(),
);
}
res
}
}
fn map_cookies(cookies: Cookies) -> BTreeMap<String, String> {
let mut result = BTreeMap::new();
for cookie in cookies.list() {
let (name, value) = cookie.name_value();
result.insert(name.to_string(), value.to_string());
}
result
}
async fn map_parts(mp: Option<Multipart>) -> Vec<Part> {
let mut result = Vec::new();
if let Some(mut mp) = mp {
while let Some(field) = mp.next_field().await.unwrap() {
let name = field.name().unwrap().to_string();
let file_name = match field.file_name() {
Some(s) => s.to_string(),
None => "".to_string(),
};
let content_type = match field.content_type() {
Some(s) => s.to_string(),
None => "".to_string(),
};
let mut data = Vec::new();
let mut text = "".to_string();
if content_type.is_empty() {
if let Ok(s) = field.text().await {
text = s;
}
} else if let Ok(bytes) = field.bytes().await {
data = bytes.to_vec()
}
result.push(Part {
name,
file_name,
content_type,
data: Arc::new(data),
text,
});
}
}
result
}
use argon2rs::argon2i_simple;
fn c_argon(b: &Block, args: &mut [Expr]) -> CExpPtr<Value> {
check_types(b, args, &[DataKind::String, DataKind::String]);
let password = c_value(b, &mut args[0]);
let salt = c_value(b, &mut args[1]);
Box::new(Argon { password, salt })
}
struct Argon {
password: CExpPtr<Value>,
salt: CExpPtr<Value>,
}
impl CExp<Value> for Argon {
fn eval(&self, ee: &mut EvalEnv, d: &[u8]) -> Value {
let pw = self.password.eval(ee, d).str();
let salt = self.salt.eval(ee, d).str();
let result = argon2i_simple(&pw, &salt).to_vec();
Value::RcBinary(Rc::new(result))
}
}
use std::{fs::OpenOptions, io::Write};
fn log_loop(log_rx: std::sync::mpsc::Receiver<String>) {
let filename = "../test.logfile";
let mut logfile = OpenOptions::new()
.append(true)
.create(true)
.open(filename)
.unwrap();
loop {
let logstr = log_rx.recv().unwrap();
let _err = logfile.write_all(logstr.as_bytes());
let _err = logfile.write_all(b"\r\n");
}
}