From fbf03b8fead904be538e956333a49ae0ad50f630 Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Sat, 4 Jun 2022 19:07:16 +0800 Subject: [PATCH] getting started with web server --- Cargo.toml | 1 + src/bin/cozo_rest/main.rs | 44 +++++++++++++++++++++++++++++++++++++++ src/runtime/instance.rs | 4 ++++ 3 files changed, 49 insertions(+) create mode 100644 src/bin/cozo_rest/main.rs diff --git a/Cargo.toml b/Cargo.toml index 76d0ae3f..6a2e9ab2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,5 @@ lazy_static = "1.4.0" thiserror = "1.0.30" log = "0.4.16" env_logger = "0.9.0" +actix-web = "4.0.1" cozorocks = { path = "cozorocks" } \ No newline at end of file diff --git a/src/bin/cozo_rest/main.rs b/src/bin/cozo_rest/main.rs new file mode 100644 index 00000000..7f717ece --- /dev/null +++ b/src/bin/cozo_rest/main.rs @@ -0,0 +1,44 @@ +use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder}; +use cozo::DbInstance; +use std::sync::Arc; + +struct AppStateWithDb { + db: DbInstance, +} + +#[get("/")] +async fn hello(data: web::Data) -> impl Responder { + // let sess = data.db.session().unwrap().start().unwrap(); + // let res = sess.get_next_main_table_id(); + HttpResponse::Ok().body(format!("Hello world! {:?}", None)) +} + +#[post("/echo")] +async fn echo(req_body: String) -> impl Responder { + HttpResponse::Ok().body(req_body) +} + +async fn manual_hello() -> impl Responder { + HttpResponse::Ok().body("Hey there!") +} + +#[actix_web::main] +async fn main() -> std::io::Result<()> { + let mut db = DbInstance::new("_test_rest", false).unwrap(); + db.set_destroy_on_close(true); + let db = web::Data::new(AppStateWithDb { + db + }); + + + HttpServer::new(move || { + App::new() + .app_data(db.clone()) + .service(hello) + .service(echo) + .route("/hey", web::get().to(manual_hello)) + }) + .bind(("127.0.0.1", 8080))? + .run() + .await +} diff --git a/src/runtime/instance.rs b/src/runtime/instance.rs index 09b17127..3be0f2cf 100644 --- a/src/runtime/instance.rs +++ b/src/runtime/instance.rs @@ -50,6 +50,10 @@ pub struct DbInstance { destroy_on_close: bool, } +unsafe impl Send for DbInstance {} + +unsafe impl Sync for DbInstance {} + impl DbInstance { pub fn new(path: &str, optimistic: bool) -> Result { let options = default_options().make_shared();