getting started with web server

main
Ziyang Hu 2 years ago
parent 350c3884a9
commit fbf03b8fea

@ -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" }

@ -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<AppStateWithDb>) -> 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
}

@ -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<Self> {
let options = default_options().make_shared();

Loading…
Cancel
Save